loading a notepad file (.txt) to a listbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • richy176
    New Member
    • May 2007
    • 8

    loading a notepad file (.txt) to a listbox

    i have a notepad file which is C:/Students.txt and i want the file to open up into a listbox on VB. is there any way i can do that?

    This is what i have so far, but i recieve an error saying "Type Mismatch" does anyone know what im doing wrong? if so please help me correct it!

    ---------------------------
    Dim List As String <<Declaration
    -----------------------------
    Open "C:\students.tx t" For Input As #1
    Input #1, List
    StudentList.Lis tIndex = List
  • Microchip
    New Member
    • Jun 2007
    • 4

    #2
    Originally posted by richy176
    i have a notepad file which is C:/Students.txt and i want the file to open up into a listbox on VB. is there any way i can do that?

    This is what i have so far, but i recieve an error saying "Type Mismatch" does anyone know what im doing wrong? if so please help me correct it!

    ---------------------------
    Dim List As String <<Declaration
    -----------------------------
    Open "C:\students.tx t" For Input As #1
    Input #1, List
    StudentList.Lis tIndex = List
    ListIndex is a NUMBER. List is defined a s a string. Hence your type mismatch.

    You may wish to use the AddItem method.

    Comment

    • debasisdas
      Recognized Expert Expert
      • Dec 2006
      • 8119

      #3
      Read the file line by line and add it to the listbox using ADDITEM method.

      But if u want to displat tthe content of text file in a control , rich textbox control would be a better option for you.

      Comment

      • ansumansahu
        New Member
        • Mar 2007
        • 149

        #4
        Originally posted by debasisdas
        Read the file line by line and add it to the listbox using ADDITEM method.

        But if u want to displat tthe content of text file in a control , rich textbox control would be a better option for you.
        Hi ,

        Yes debasis is correct. If you just want to display the content of the file go for a rich text box control. What is the purpose of using the list box?? Do you have a specific requirement.

        -ansuman sahu

        Comment

        • ajusingh222
          New Member
          • Jun 2007
          • 3

          #5
          Option Explicit

          Private Sub Form_Load()
          dlgOpenFile.Ini tDir = App.Path
          dlgOpenFile.Fil ter = "Text Files (*.txt)|*.txt|A ll Files (*.*)|*.*"
          dlgOpenFile.Fil terIndex = 1
          dlgOpenFile.Dia logTitle = "Open File"
          dlgOpenFile.Fla gs = _
          cdlOFNFileMustE xist + _
          cdlOFNHideReadO nly + _
          cdlOFNLongNames + _
          cdlOFNExplorer
          dlgOpenFile.Can celError = True
          End Sub

          Private Sub Form_Resize()
          txtFile.Move 0, 0, ScaleWidth, ScaleHeight
          End Sub


          Private Sub mnuFileExit_Cli ck()
          Unload Me
          End Sub

          Private Sub mnuFileOpen_Cli ck()
          Dim fnum As Integer

          On Error Resume Next
          dlgOpenFile.Sho wOpen
          If Err.Number = cdlCancel Then
          ' The user canceled.
          Exit Sub
          ElseIf Err.Number <> 0 Then
          ' Unknown error.
          MsgBox "Error " & Format$(Err.Num ber) & _
          " selecting file." & vbCrLf & _
          Err.Description
          Exit Sub
          End If
          On Error GoTo 0

          ' Read the file.
          fnum = FreeFile
          Open dlgOpenFile.Fil eName For Input As #fnum
          txtFile.Text = Input$(LOF(fnum ), fnum)
          Close #fnum
          End Sub
          Last edited by ajusingh222; Jun 29 '07, 05:30 AM. Reason: Not According To reqiurement

          Comment

          Working...