Results 1 to 38 of 38

Thread: Load to textbox:[resolved]

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Resolved Load to textbox:[resolved]

    Heres my code for a little text editor:
    VB Code:
    1. Option Explicit
    2. Dim ff As Integer
    3. Dim sInput As String
    4. Dim stRfile As String
    5. Dim foundpos As Integer
    6. Dim strLine As String
    7.  
    8. Private Sub Load_Click()
    9.  
    10.    sInput = InputBox("Load file name:")
    11.    
    12.    If StrPtr(sInput) = 0 Then
    13.       Print vbNullString
    14.    ElseIf StrPtr(sInput) <> 0 And sInput = "" Then
    15.       MsgBox "Please type a filename!"
    16.    Else
    17.       stRfile = sInput & ".txt"
    18.        ff = FreeFile()
    19.             Open stRfile For Input As #ff 'open text
    20.                    
    21. End If
    22. End Sub
    23.  
    24. Private Sub Save_As_Click()
    25. ff = FreeFile()
    26.  
    27.    sInput = InputBox("Save file as:")
    28.    
    29.    If StrPtr(sInput) = 0 Then
    30.       Print vbNullString
    31.    ElseIf StrPtr(sInput) <> 0 And sInput = "" Then
    32.       MsgBox "Please type a filename!"
    33.    Else
    34.       Open sInput & ".txt" For Output As #ff
    35.       Print #ff, Text1.Text
    36.       Close #ff
    37.    End If
    38. End Sub

    how do i load the file i just found into my text1.text?
    Last edited by |2eM!x; Feb 4th, 2005 at 01:05 AM.

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Load to textbox:

    VB Code:
    1. text1.text = input(lof(ff),ff)

  3. #3
    Addicted Member Luke K's Avatar
    Join Date
    Jun 2004
    Location
    Perth, Australia
    Posts
    183

    Re: Load to textbox:

    Use the following. But, instead of appending the .txt in your code to the string you used to specify the file name, type it in manually.

    VB Code:
    1. Private Sub Load_Click()
    2. Dim stringDataL As String
    3. sInput = InputBox("Load file name:")
    4.    
    5. If StrPtr(sInput) = 0 Then
    6.       Exit Sub
    7. ElseIf StrPtr(sInput) <> 0 And sInput = "" Then
    8.       MsgBox "Please type a filename!"
    9. Else
    10.     ff = FreeFile()
    11.     Open sInput For Input Lock Write As #ff 'open text
    12.         While Not EOF(ff)
    13.             Line Input #ff, stringDataL
    14.             With Text1
    15.                 .Text = .Text & (stringDataL) & vbCrLf
    16.             End With
    17.         Wend
    18.     Close #1
    19. End If
    20. End Sub
    Artificial Intelligence At War! - The best game of its genre
    Program your own robot and watch it fight in 3d!
    Droidarena 3

    If I have been useful, please Rate My Post

    Support FireFox -
    Microsoft Visual Studio .NET Professional 2003
    Microsoft Visual Studio 6, Enterprise Edition
    Microsoft Windows XP Professional, Service Pack 2

  4. #4

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Load to textbox:

    yay! thanks, what do i need to let the user select what position they want to save to?
    i know i need the drive list, dir list, and drive list box, or whatever..but i dont want those ugly squares coming up.
    is there a way to let them choose where to save like in word or anything?
    Last edited by |2eM!x; Feb 2nd, 2005 at 10:11 PM.

  5. #5
    Addicted Member Luke K's Avatar
    Join Date
    Jun 2004
    Location
    Perth, Australia
    Posts
    183

    Re: Load to textbox:

    Use the CommonDialog control, then use the following code in a savebutton.

    VB Code:
    1. With CommonDialog1
    2.     .InitDir = "C:"
    3.     .FileName = "MyFile.txt"
    4.     .Filter = "Text File (*.txt)|*.txt"
    5.     .DialogTitle = "Locate where you want to save your text file"
    6.     .ShowSave
    7.     Open .FileName For Output Lock Read As #1
    8.         Print #1, Text1.Text & vbNullChar
    9.     Close #1
    10. End With
    Artificial Intelligence At War! - The best game of its genre
    Program your own robot and watch it fight in 3d!
    Droidarena 3

    If I have been useful, please Rate My Post

    Support FireFox -
    Microsoft Visual Studio .NET Professional 2003
    Microsoft Visual Studio 6, Enterprise Edition
    Microsoft Windows XP Professional, Service Pack 2

  6. #6

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Load to textbox:

    how would i do loadbutton then?
    thanks alot for that one ^^

    edit*
    heres what i got:
    Code:
    Private Sub Load_Click()
     With CommonDialog1
        .InitDir = "C:"
        .FileName = ""
        .Filter = "Text File (*.txt)|*.txt"
        .DialogTitle = "Load file:"
        .Showload
        Open .FileName For Input As #1
            Print #1, Text1.Text & vbNullChar
            Text1.Text = Input(LOF(ff), #ff)
        Close #1
    End With
    End Sub
    Last edited by |2eM!x; Feb 2nd, 2005 at 10:19 PM.

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Load to textbox:

    so that's what lock read and lock write do!

  8. #8

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Load to textbox:

    hehe, i could tell that was a hint : )

    but what do i put instead of .ShowOPEN!!

    okay..
    got it,

    now:
    VB Code:
    1. Private Sub Load_Click()
    2.  With CommonDialog1
    3.     .InitDir = "C:"
    4.     .FileName = ""
    5.     .Filter = "Text File (*.txt)|*.txt"
    6.     .DialogTitle = "Load file:"
    7.     .ShowOpen
    8.       Open .FileName For Input Lock Write As #1
    9.         For Input #1 = Text1.Text
    10.         Text1.Text = Input(LOF(ff), #ff)
    11.     Close #1
    12. End With
    13. End Sub
    Last edited by |2eM!x; Feb 2nd, 2005 at 10:37 PM.

  9. #9
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Load to textbox:

    .showopen will show the open dialog.

    VB Code:
    1. Private Sub txtBackUp_Click()
    2.   Dim oCDBForm As New frmCommonDialog
    3.   x = lblSetup(1).Left + lblSetup(1).Width
    4.   y = lblSetup(1).Top
    5.   oCDBForm.Move x, y
    6.   With oCDBForm.CommonDialog1
    7.     .DialogTitle = "Select BackUp File to use"
    8.     .InitDir = RealPath
    9.     .Filter = "BackUp Files (*.old)|*.old|All " & _
    10.         "Files (*.*)|*.*"
    11.     .Flags = _
    12.         cdlOFNFileMustExist + _
    13.         cdlOFNHideReadOnly + _
    14.         cdlOFNLongNames + _
    15.         cdlOFNExplorer
    16.     .CancelError = False  ' Change to trap cancel True
    17.     .ShowOpen
    18.     txtBackUp.Text = .FileName
    19.   End With
    20.   Unload oCDBForm
    21.   Set oCDBForm = Nothing
    22. End Sub

    This is what I use. I have a Common Dialog Control by itself on a form that is small. I use it for all the dialogs in my app. I subclass it like shown. This is when clicking on a textbox, it puts the selected filename in the textbox.
    The same dialog works for printing, saving, and everything else.

  10. #10

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Load to textbox:

    userdefined type not defined
    VB Code:
    1. Dim oCDBForm As New frmCommonDialog

  11. #11
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Load to textbox:

    never mind. I'll upload it. the label is where I move it to. you could substitute the button, or a label or whatever you want. even 0,0
    it just moves the new form.

  12. #12

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Load to textbox:

    i dont understand waht the heck that is..or what im supposed to do

  13. #13
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Load to textbox:

    unzip it to your program directory, and you will have the frmCommonDialog.frm
    Just right click in the properties area (where the forms are) and click ADD FORM, then click existing, then double-click on my form. Then it will be in your program. Then save it so that it will be in there in the future.

  14. #14

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Load to textbox:

    done:
    now it wont make the .exe and says " sub or function not defined"

    can you tell me how to do it with the common dialog above?
    that way was easier..

  15. #15
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Load to textbox:

    Don't you have Option Explicit in all modules? And Compie on Demand?
    That would show the error. Do you have all variables defined? It sounds like you misspelled a module name (function or subroutine) they should be easy to check. btw - you are using the Common Dialog.

  16. #16

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Load to textbox:

    then whys it look so different? and have a picture box?
    ill report back with results--i dont know what to do!



    sub or function not defined over lblsetup..
    i dont have labels...

    and i have option explicit on..

    why cant i use this?
    VB Code:
    1. With CommonDialog1
    2.     .InitDir = "C:"
    3.     .FileName = ""
    4.     .Filter = "Text File (*.txt)|*.txt"
    5.     .DialogTitle = "Save file to:"
    6.     .ShowOpen
    7.     Open .FileName For Output Lock Read As #1 '}
    8.         Print #1, Text1.Text & vbNullChar ' |- These three i have no idea what to do to change to load it..
    9.     Close #1  ' }
    10. End With
    Last edited by |2eM!x; Feb 3rd, 2005 at 09:01 PM.

  17. #17
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Load to textbox:

    Oh. in my form, it is going to move the common dialog control right next to the label! Oops. forgot about that. Just pick a control on your form that you want the Common Dialog to appear next to. Then set the x and y of the new form to someone close to the control
    (use any control that you want)

    x = mycontrol.Left + mycontrol.Width
    y = mycontrol.Top

  18. #18
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Load to textbox:

    why do you even have a picturebox? maybe you should post some code.

  19. #19

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Load to textbox:

    got it: heres my code for opening/loading!

    VB Code:
    1. Private Sub load_Click()
    2.  With CommonDialog1
    3.         .Filter = "Text (*.txt)|*.txt"
    4.         .InitDir = "c:\"
    5.         .ShowOpen
    6.     End With
    7.        Open CommonDialog1.FileName For Input As #1
    8.     Text1.Text = Input$(LOF(1), #1)
    9.     Close #1
    10. End Sub
    11.  
    12. Private Sub Save_As_Click()
    13.  With CommonDialog1
    14.     .InitDir = "C:"
    15.     .FileName = ""
    16.     .Filter = "Text File (*.txt)|*.txt"
    17.     .DialogTitle = "Save file to:"
    18.     .ShowSave
    19.     Open .FileName For Output Lock Read As #1
    20.         Print #1, Text1.Text & vbNullChar
    21.     Close #1
    22. End With
    23. End Sub

    now the cancel button gives errors, so im gonna look up top at that trap cancel button shizat

    edit** cant get it to work..
    also i get this with a file i edit in my text editor(if it has spaces) "input past end of file error 62"
    Last edited by |2eM!x; Feb 3rd, 2005 at 09:18 PM.

  20. #20
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Load to textbox:

    You could use On Error statements.

  21. #21

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Load to textbox:

    what would that do/how do i use them..(if its something besides the obvious <<)

    edit**
    figured that out..
    but it seems what happends is this:
    it saves the file and adds 1 line of {enter} then a few spaces
    that gives the error..
    once i delete all the spaces it loads it and stuff..
    anyone know whats wrong?
    Last edited by |2eM!x; Feb 3rd, 2005 at 09:35 PM.

  22. #22
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Post Re: Load to textbox:

    Private Sub Save_As_Click()
    VB Code:
    1. With CommonDialog1
    2.     .InitDir = "C:"
    3.     .FileName = ""
    4.     .Filter = "Text File (*.txt)|*.txt"
    5.     .DialogTitle = "Save file to:"
    6.     .ShowSave
    7.     on error goto CD_Error ' won't happen ever!
    8.     Open .FileName For Output Lock Read As #1
    9.         Print #1, Text1.Text & vbNullChar
    10.     Close #1
    11.  End With
    12.    exit sub  ' normal exit
    13. CD_Error:
    14.    on error goto 0 ' reset error trap
    15.    end with  ' close with
    16.    debug.print "Error in Save File!"
    17. End Sub     ' error exit

    You can also use ON ERROR RESUME NEXT, but those are the hardest to debug properly.

  23. #23

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Load to textbox:

    now nothing happends...(doesnt show errors = good),(not knowing what im doing=bad)

    why does it add like 5 spaces afterwards??

    figured it out..
    Print #1, Text1.Text & vbNullChar
    should be
    Print #1, Text1.Text

    what does vbnullchar mean?
    edit**
    now just to figure out how to make bold/italics..know what dll does it?

    edit2**
    how do i make the text1.text resize with the form?

  24. #24
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Load to textbox:

    take out vbnullchar. its for strings that have to have the null character (like some API's must have)

    to resize, just use the form.width and form.height. I'm not good at doing it. If the for is 400, the text box is 325 to 350 but if it is
    only 200, then textbox is only 125 to 140 or so.

    put commans in the resize event.


    here is an example.
    Attached Files Attached Files

  25. #25

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Load to textbox:

    thx, it works good enough..you know about that dll thing?

  26. #26
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Load to textbox:

    a little here and there. what do you need to know?

  27. #27

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Load to textbox:

    VB Code:
    1. now just to figure out how to make bold/italics..know what dll does it?

    ps, how do i check this on my own

  28. #28
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Load to textbox:

    I think you have to use a Rich Text Box to get any fancy formatting. You can choose the font, but that's about it for the listbox. Even MSN is a RTB. Word is also a RTB. It's really the only way to do this. Pretty heavy stuff.

  29. #29

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Load to textbox:

    didnt know there was one of those : )

    okay, 2 questions
    how do i make the scrollbar? the scrollbars thing doesnt do squat..
    also, it saves to the text box, but say i type this:
    Yay im happy.aoscnmd a sdf.

    this will come up upon load:
    {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 MS Sans Serif;}}
    \viewkind4\uc1\pard\f0\fs17 Yay im happy.aoscnmd a sdf.
    \par }


    anyway to delete all that garbage/not show it?

  30. #30
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Load to textbox:

    you open it in wordpad, which know about the files, and can show the formatting that you do. you can also create forms in word in rtf format.

    scrollbars should be automatic when the data gets too big for the view. I've never had to do anything except turn them on if I wanted them to appear.

  31. #31

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Load to textbox:

    okay, got everything working..saving as an rtb and all..if i change the color, then save, then load it--it wont be in color itll look like this:
    Code:
    {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 MS Sans Serif;}}
    {\colortbl ;\red0\green0\blue255;\red255\green0\blue0;\red0\green255\blue0;\red0\green0\blue0;}
    \viewkind4\uc1\pard\cf1\f0\fs17 sdgsagafe\cf2 fsagasfgadsg\cf3 afgfagafdgad\cf4 afgafsdgadf\cf0 
    \par }
    any ideas?
    should i have it search it, and if a color is found then the letters after it = vbcolor?
    Last edited by |2eM!x; Feb 4th, 2005 at 12:05 AM.

  32. #32
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Load to textbox:

    you can't read the file that way. you have to use the rtf command to read the file. I use this:

    rtbReceipt.LoadFile RealPath & "ReceiptFile.rtf"

  33. #33

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Load to textbox:

    hmm..you didnt tell me what your variabled were so i did this:

    Code:
    Private Sub load_Click()
    Dim realpath As Variant
    Dim rtbReceipt As Variant
     With CommonDialog1
        On Error Resume Next
            .Filter = "Rich Text File (*.rtf)|*.rtf"
            .InitDir = "c:\"
            .ShowOpen
            .CancelError = False
        End With
           Open CommonDialog1.FileName For Input As #1
       rtbReceipt.LoadFile realpath & "ReceiptFile.rtf" = Input$(LOF(1), #1)
        Close #1
    End Sub
    i dont understand how to get the rtbReceipt.LoadFile realpath & "ReceiptFile.rtf" into my textbox

  34. #34
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Load to textbox:

    sorry. you over-complicated things. the rtbreceipt is my richtextbox. change it to your own. i don't load the filename, it is supplied by my program. you would do the same thing that you had before, but change the file extension to rtf.

    rtbReceipt.LoadFile CommonDialog1.FileName

  35. #35

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Load to textbox:

    RichTextBox1.LoadFile = Input$(LOF(1), #1)

    argument not optional

  36. #36
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Load to textbox:

    RichTextBox1.LoadFile CommonDialog1.FileName

  37. #37

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Load to textbox:

    YES.YOU ROCK

    if i ever become better than you, and you need help, you can always ask me..k?

  38. #38
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Load to textbox:[resolved]

    OK. Will do.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width