Results 1 to 3 of 3

Thread: [RESOLVED] Open a common dialog box to choose a txt file to load to a listbox

  1. #1
    Frenzied Member
    Join Date
    May 06
    Posts
    1,948

    Resolved [RESOLVED] Open a common dialog box to choose a txt file to load to a listbox

    Hello everyone. I am updating a web scrapper and right now I keep all the links I scrap in a txt file, but for various websites I have various txt files, so I would like to modify if possible, a block of code from an earlier project where a common dialog box is opened and the user picks a txt file and the txt file is loaded into an array of textboxes. I would like to modify it so instead of being loaded into text boxes, it is loaded into a listbox.

    Here is the old code.

    VB Code:
    1. Private Sub Open_Click()
    2. '
    3. ' Requires a Microsoft Common Dialog Control drawn on the form
    4. ' Named cd
    5. '
    6. Dim strData As String
    7. Dim strQ() As String
    8. Dim intI As Integer
    9. Dim intFile As Integer
    10. '
    11. ' Set up the Common Dialog initial Directory
    12. ' to the application path. Filter for Text and All files
    13. ' Prompt the user to select a file
    14. '
    15. cd.InitDir = App.Path
    16. cd.Filter = "Text Files(*.txt)|*.txt|All Files(*.*)|*.*"
    17. cd.DialogTitle = "Select Question File"
    18. cd.ShowOpen
    19. If cd.FileName <> "" Then
    20.     '
    21.     ' If the user selected a file
    22.     ' open and read the entire contents
    23.     ' then split it into records
    24.     '
    25.     intFile = FreeFile
    26.     Open cd.FileName For Input As intFile
    27.     strData = Input(LOF(intFile), intFile)
    28.     Close intFile
    29.     strQ = Split(strData, vbNewLine)
    30.     '
    31.     ' Populate the texbox array with the questions
    32.     ' (either the number of textboxes in the control array or
    33.     ' number of questions, which ever is the smaller)
    34.     '
    35.     Do
    36.         CustomQ(intI).Text = strQ(intI)
    37.         intI = intI + 1
    38.     Loop Until intI > CustomQ.UBound Or intI > UBound(strQ)
    39. End If
    40. End Sub

    As you can see the CustomQ is the name of the boxes, but the name of the list is called WebsiteList. Would this just be a matter of loading a textfile to a listbox where the Do Loop begins? or would it end up loading it over and over in the same listbox? I don't need it to load more than once. lol Thanks!

  2. #2
    PowerPoster
    Join Date
    Jun 01
    Location
    Trafalgar, IN
    Posts
    3,441

    Re: Open a common dialog box to choose a txt file to load to a listbox

    Replace lines 35 to 38 with

    Code:
    For intI = 0 To UBound(strQ)
        WebsiteList.AddItem strQ(intI)
    Next intI

  3. #3
    Frenzied Member
    Join Date
    May 06
    Posts
    1,948

    Re: Open a common dialog box to choose a txt file to load to a listbox

    That works fantastic. Thank you!

Posting Permissions

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