I have a listbox that I load items into. Each line starts with time in HH:mm:ss format. I want a button such that on the click of it, the listbox selected item would navigate to a line that starts with a particular time, as entered into a textbox by the user. Secondly, I have a textbox that copies the first 5 characters of the selecteditem, using selecteditem.text.tostring.substring(0,5). Now, I need to capture the first 5 characters of the line RIGHT BELOW the selected item. Thanks for helping.
Asked
Active
Viewed 4,000 times
2 Answers
0
ListBox1.SelectedIndex = 2
0 equal first line
1 equal second line
2 equal third line
and finish like that
or you can add -1 in the end like this below
ListBox1.SelectedIndex = 2 - 1
so you can select the actual line number two

IFON26
- 8
- 3
-
This does not select anything, it just gets the first item. – Visual Vincent Mar 20 '16 at 09:13
-
i told him 0 equal the item he wants to select -_- – IFON26 Mar 20 '16 at 09:18
-
This still doesn't actually select anything, just gets the item at index 0. Nor does it help him much with his question. I know you cannot comment yet, but this should otherwise have been one. It's correct what you say, but an answer should be more thorough. – Visual Vincent Mar 20 '16 at 09:30
-
works for me, i dont know who is the master of vb here, try put 1 instead of 0 you will see – IFON26 Mar 20 '16 at 09:48
-
_Yes it works_, I didn't say that it wouldn't. But it doesn't _physically select_ the item. Which is what he wanted if I understood his question right. Either way the answer is still not very thorough, and doesn't help him much with his question. Your code _gets_ the item, it doesn't _select_ it. – Visual Vincent Mar 20 '16 at 09:51
-
Mmm, it's better. :) – Visual Vincent Mar 20 '16 at 10:00
-
I think it's good that you pointed out that one can take the item you want - 1 to get the index. It makes it easier to remember. – Visual Vincent Mar 20 '16 at 13:15
0
Using the ListBox's FindString()
method you can find the index of the first item starting with the specified string. You can then use that to set the SelectedIndex
property which will select the item at the specified index.
To get the item below the currenty selected one you just have to get SelectedIndex + 1
from the Items
collection.
Public Sub DoSomething()
Dim Index As Integer = ListBox1.FindString(TextBox1.Text) 'Find the index of the item starting with whatever is in TextBox1.
If Index > -1 Then 'Check if the item exists/was found.
ListBox1.SelectedIndex = Index
TextBox2.Text = ListBox1.Items(Index).ToString().Substring(0, 5)
If Index < ListBox1.Items.Count - 1 Then 'Check if the found item is the last item or not.
TextBox3.Text = ListBox1.Items(Index + 1).ToString().SubString(0, 5)
Else 'This was the last item.
MessageBox.Show("You've reached the end of the list.", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Else 'No item was found.
MessageBox.Show("No item found starting with the specified text!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub

Visual Vincent
- 18,045
- 5
- 28
- 75
-
Thank you Visual Vincent and @IFON26 too. I have not implemented the code but reading it, I have strong hope that it will work. – MordC Mar 20 '16 at 12:25
-
@MordC : I actually haven't tested it myself as I have no access to a computer at the moment, but the coding is so basic that there should not be any problems with it. – Visual Vincent Mar 20 '16 at 13:12
-
I tried the code today but it didn't work PERFECTLY. I am however grateful because your answer gave me some clues on solving some related problems. The part that DID NOT WORK is navigating to a line I the listbox that contains characters as entered by user into a textbox. I would be glad if you could implement and see. Thanks for helping. – MordC Mar 21 '16 at 22:04
-
-
@MordC : I don't understand, it works perfectly for me. Do you get any errors? I did a quick test, see my screenshots: http://www.mydoomsite.com/Bilder/ListBoxNavigationTest.png _(I tried it only with letters too, works as perfect)._ – Visual Vincent Mar 21 '16 at 23:18
-
So unless there's something strange going on, I'd say you've done something wrong with the code :) – Visual Vincent Mar 21 '16 at 23:24
-
It worked and what I needed in addition was provided according to this Microsoft. MSDN l[Navigation Link](https://msdn.microsoft.com/en-us/library/e5et1818(v=vs.110).aspx) . The Setselected property was used. Thank ps much. – MordC Mar 22 '16 at 08:37
-
@MordC : But, doing `SetSelected()` or `SelectedIndex = ...` does the same thing :). I just copy-pasted my code when testing and it worked completely. Anyway, glad you got it working! – Visual Vincent Mar 22 '16 at 16:43
-
My file is a circular one and that may explain why I used to have the problem. Thanks, anyway. – MordC Mar 22 '16 at 22:46