Results 1 to 18 of 18

Thread: [RESOLVED] Listboxes & Txt Files

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Location
     
    Posts
    453

    Resolved [RESOLVED] Listboxes & Txt Files

    Alright, I have a listbox,and I was wondering how to get it to load a txt document when a certain item is clicked, and display the text in a textbox, is that possible?
    Haikus are easy.
    But sometimes they don't make sense.
    Refrigerator.

  2. #2
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Listboxes & Txt Files

    ..of course its possible.

    A little more information would be helpful. What are the listbox items? 1 click or 2? How much experience do you have with VB?

    The below will load a file into a textBox based on the filename in the listbox.
    vb Code:
    1. Private Sub ListBox_DblClick()
    2. Open ListBox.List(ListBox.Index) For Input As #1
    3.     textBox.Text = Input(LOF(1), 1)
    4. Close #1
    5. End Sub
    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Location
     
    Posts
    453

    Re: Listboxes & Txt Files

    Well, I have some good experience with vb6, but, I very, very rarely use listboxes, so... 2 click is what I need I think, but then how do I add the index for the list items?
    Haikus are easy.
    But sometimes they don't make sense.
    Refrigerator.

  4. #4
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    Re: Listboxes & Txt Files

    Quote Originally Posted by Bobalandi
    Alright, I have a listbox,and I was wondering how to get it to load a txt document when a certain item is clicked,
    What item?

    To load a text file into a ListBox you need to use the Line Input Statement when reading the file and add that to the ListBox with AddItem.
    Keith

    I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Location
     
    Posts
    453

    Re: Listboxes & Txt Files

    Sorry, I want the textbox to load a txt document when a certain listitem is clicked... sorry...
    Haikus are easy.
    But sometimes they don't make sense.
    Refrigerator.

  6. #6
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Listboxes & Txt Files

    The code I gave you does that. It assumes that the list item is the direct path to the file.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Location
     
    Posts
    453

    Re: Listboxes & Txt Files

    but how do I edit it for each different item clicked?
    Haikus are easy.
    But sometimes they don't make sense.
    Refrigerator.

  8. #8
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Listboxes & Txt Files

    It already does that. The code I gave you loads in the file thats selected in the listbox.. no matter which one it is.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Location
     
    Posts
    453

    Re: Listboxes & Txt Files

    Wait, so I specify the file in the list box with the additem?
    Haikus are easy.
    But sometimes they don't make sense.
    Refrigerator.

  10. #10
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Listboxes & Txt Files

    Yes. As I said.. the code assumes the text in the listbox item, is a direct path to the file. For Example:
    Code:
    ListBox.AddItem "C:\file.txt"
    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Location
     
    Posts
    453

    Re: Listboxes & Txt Files

    Ok, thank you very much, but can you just tell me one more thing, is there a way to show it as something like "Main" and the file url?
    Haikus are easy.
    But sometimes they don't make sense.
    Refrigerator.

  12. #12
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Listboxes & Txt Files

    Expand on that a bit more.. what do you want exactly? The listbox item to read "Main - C:\file.txt" ?

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Location
     
    Posts
    453

    Re: Listboxes & Txt Files

    No, I want it to read main, but the file be C:\file.txt
    Haikus are easy.
    But sometimes they don't make sense.
    Refrigerator.

  14. #14
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Listboxes & Txt Files

    You can either have a separate invisible listbox for the proper filenames, or use a string array. For example:
    Code:
    ListBox1.AddItem "C:\file.txt"
    ListBox2.AddItem "Main"
    
    Private Sub ListBox2_DblClick()
    Open ListBox1.List(ListBox2.Index) For Input As #1
        textBox.Text = Input(LOF(1), 1)
    Close #1
    End Sub
    So, you click the visible listbox, but it loads the file thats in the invisible one.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Location
     
    Posts
    453

    Re: Listboxes & Txt Files

    Ok, thank you... I appreciate it...
    Haikus are easy.
    But sometimes they don't make sense.
    Refrigerator.

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Location
     
    Posts
    453

    Re: [RESOLVED] Listboxes & Txt Files

    Whoops, I just got an error, it says the object is not in an array...
    Code:
    Open lstfiles.List(lsttopics.Index) For Input As #1
    Haikus are easy.
    But sometimes they don't make sense.
    Refrigerator.

  17. #17
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: [RESOLVED] Listboxes & Txt Files

    Do you have any items in your listbox when it reaches that code?

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Location
     
    Posts
    453

    Re: [RESOLVED] Listboxes & Txt Files

    Yup, I have 1 at the time...
    Haikus are easy.
    But sometimes they don't make sense.
    Refrigerator.

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