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?
Printable View
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?
..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.
chemvb Code:
Private Sub ListBox_DblClick() Open ListBox.List(ListBox.Index) For Input As #1 textBox.Text = Input(LOF(1), 1) Close #1 End Sub
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?
What item?Quote:
Originally Posted by Bobalandi
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. ;)
Sorry, I want the textbox to load a txt document when a certain listitem is clicked... sorry... :blush:
The code I gave you does that. It assumes that the list item is the direct path to the file.
chem
but how do I edit it for each different item clicked?
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
Wait, so I specify the file in the list box with the additem?
Yes. As I said.. the code assumes the text in the listbox item, is a direct path to the file. For Example:
chemCode:ListBox.AddItem "C:\file.txt"
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?
Expand on that a bit more.. what do you want exactly? The listbox item to read "Main - C:\file.txt" ?
chem
No, I want it to read main, but the file be C:\file.txt
You can either have a separate invisible listbox for the proper filenames, or use a string array. For example:
So, you click the visible listbox, but it loads the file thats in the invisible one.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
chem
Ok, thank you... I appreciate it...
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
Do you have any items in your listbox when it reaches that code?
chem
Yup, I have 1 at the time...