loading text from txt file from combobox to textbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gobblegob
    New Member
    • Dec 2007
    • 133

    loading text from txt file from combobox to textbox

    hi guys,

    here is my problem
    if the user selects first from combo box i want text from a txt file to load into
    textbox 1, but i cant see what i am doing wrong.

    Code:
    Private Sub Command1_Click()
        Dim what As String
        Dim temp As String
        Combo1.Text = what$
        If what = "first" Then
        Open "c:\first.txt" For Input As #1
        While Not EOF(1)
        Line Input #1, temp$
        alltext$ = alltext$ & temp$ & vbCrLf
        Text1.Text = alltext$
        Wend
        Close #1
        End If
    End Sub
    
    Private Sub Form_Load()
    
    Combo1.AddItem "first"
    Combo1.AddItem "second"
    
    End Sub
    Thanks in advanced
    Gobble.
  • jamesd0142
    Contributor
    • Sep 2007
    • 471

    #2
    try this:

    I have two files in "c:\a\" called "File1.txt" and "File2.txt"

    [code=vbnet]
    Private Sub ComboBox1_Selec tedIndexChanged (ByVal sender As System.Object, ByVal e As System.EventArg s) Handles ComboBox1.Selec tedIndexChanged
    Dim strtext As String
    TextBox1.Text = Readfile(strtex t)

    End Sub

    Function Readfile(ByVal abc)
    Dim EntireLine As String
    Dim oFile As System.IO.File
    Dim oRead As System.IO.Strea mReader
    oRead = oFile.OpenText( "C:\a\" & ComboBox1.Text)
    EntireLine = oRead.ReadToEnd
    oRead.Close() 'test line
    Return EntireLine
    End Function
    [/code]

    Comment

    • gobblegob
      New Member
      • Dec 2007
      • 133

      #3
      Sorry i should of mentioned that i am using VB6
      and that code is giving me errors on:

      Code:
      Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
      and
      Code:
              oRead.Close() 'test line
              Return EntireLine

      Comment

      • jamesd0142
        Contributor
        • Sep 2007
        • 471

        #4
        Originally posted by gobblegob
        Sorry i should of mentioned that i am using VB6
        and that code is giving me errors on:

        Code:
        Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        and
        Code:
                oRead.Close() 'test line
                Return EntireLine
        fix the first problem by double clicking your combo box and adding the code there, minus
        [code=vbnet]
        Private Sub ComboBox1_Selec tedIndexChanged (ByVal sender As System.Object, ByVal e As System.EventArg s) Handles ComboBox1.Selec tedIndexChanged

        and the..

        end sub
        [/code]

        try typing this above your entire code (top line)

        imports system.io

        as for the second problem just wait fro someone to advise you, as i dont use vb6

        O AND THE FUNCTION MUST GO OUTSIDE ANY OTHER SUB...RIGHT?

        Comment

        • WinblowsME
          New Member
          • Jan 2008
          • 58

          #5
          Since I don't have VB 6.0 on this computer, I coded the following in VBA. Change UserForm_Activa te to Form_Load.

          [CODE=vb]
          ' VBA

          Private Sub UserForm_Activa te()
          Text1.MultiLine = True

          Combo1.AddItem "first"
          Combo1.AddItem "second"
          End Sub

          Private Sub Combo1_Click()
          Dim file_name As String, curr_line As String

          On Error GoTo ERROR_HANDLER

          file_name = "C:\" & Combo1.List(Com bo1.ListIndex) & ".txt"

          Open file_name For Input As #1
          While Not EOF(1)
          Line Input #1, curr_line
          Text1.Text = Text1.Text & curr_line & vbCrLf
          Wend
          Close #1

          Exit Sub
          ERROR_HANDLER:
          MsgBox Err.Description & ": " & file_name
          End Sub[/CODE]

          Comment

          • jamesd0142
            Contributor
            • Sep 2007
            • 471

            #6
            is this the type of thing you where looking for?
            Attached Files

            Comment

            • gobblegob
              New Member
              • Dec 2007
              • 133

              #7
              Thanks for posts guys , and this is exacly what i needed to learn WinblowsMe
              Much appreciated :)



              Originally posted by WinblowsME
              Since I don't have VB 6.0 on this computer, I coded the following in VBA. Change UserForm_Activa te to Form_Load.

              [CODE=vb]
              ' VBA

              Private Sub UserForm_Activa te()
              Text1.MultiLine = True

              Combo1.AddItem "first"
              Combo1.AddItem "second"
              End Sub

              Private Sub Combo1_Click()
              Dim file_name As String, curr_line As String

              On Error GoTo ERROR_HANDLER

              file_name = "C:\" & Combo1.List(Com bo1.ListIndex) & ".txt"

              Open file_name For Input As #1
              While Not EOF(1)
              Line Input #1, curr_line
              Text1.Text = Text1.Text & curr_line & vbCrLf
              Wend
              Close #1

              Exit Sub
              ERROR_HANDLER:
              MsgBox Err.Description & ": " & file_name
              End Sub[/CODE]

              Comment

              Working...