Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

listbox item to textbox

Status
Not open for further replies.

hugh999

MIS
Nov 29, 2001
129
IE
Hi

I have the following code behind a button that will select the next item in a listbox and add it to a text box. The problem is that when i clik the button to go to the second item the code skips the second item and goes to the third item


For i As Integer = ListBox1.Items.Count - 1 To 1 Step -1
ListBox1.SetSelected(i, ListBox1.GetSelected(i - 1))
Dim lstitem as string
lstitem = (ListBox1.SelectedItem) '.ToString
If lstitem = "" Then
Exit Sub
End If
TextBox1.Text = lstitem
Next i
ListBox1.SetSelected(0, False)
[/color red]

For the Form Load i have the following code

ListBox1.SetSelected("0", True)
TextBox1.Text = ListBox1.Items.Item(0)[/color red]

Where am i going wrong
Thanks

 
Do something like this.

Code:
Dim CurrentItem As Object

For Each CurrentItem in ListBox1.Items
     TextBox1.Text = TextBox1.Text & CurrentItem
Next

This will add all of the items in the ListBox1 to the TextBox1. You could put a & " " at the end to put a space after each word or if you did a MultiLine TextBox you could use & vbCrLf instead to put each on it's own line. Not sure what you want to do with the end result though.

-I hate Microsoft!
-Forever and always forward.
 
What a sec I think I misuderstood what you are wanting to do. If that wasn't want you wanted try:

Code:
        Dim CurrentIndex As Integer

        CurrentIndex = ListBox1.SelectedIndex

        If CurrentIndex < ListBox1.Items.Count - 1 Then
            ListBox1.SelectedIndex = CurrentIndex + 1
        Else
            ListBox1.SelectedIndex = 0
        End If

        TextBox4.Text = ListBox1.SelectedItem

-I hate Microsoft!
-Forever and always forward.
 
OOPs change TextBox4.Text to TextBox1.Text ofcourse.

-I hate Microsoft!
-Forever and always forward.
 
What I am trying to achieve is a scrolling effect. When the users clicks on the Next button the next item in the listbox is selected and the item is displayed in a textbox and if the user selects the Back button the previous item in the listbox ix selected and displayed in the textbox.

I played around with your code, but no luck.

Thanks
 
I think this is what you're looking for:

Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
Dim intIndex As Integer
intIndex = ListBox1.SelectedIndex
ListBox1.ClearSelected()
ListBox1.SelectedIndex = intIndex + 1
TextBox1.Text = ListBox1.SelectedItem
End Sub

Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
Dim intIndex As Integer
intIndex = ListBox1.SelectedIndex
ListBox1.ClearSelected()
ListBox1.SelectedIndex = intIndex - 1
TextBox1.Text = ListBox1.SelectedItem
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.SelectedIndex = 0
TextBox1.Text = ListBox1.SelectedItem
End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
TextBox1.Text = ListBox1.SelectedItem
End Sub

You'll have to add code to check limits. when clicking next you don't what intIndex to go beyond the number of items in the list; similarily you don't want to go below 0 when clicking previous.

Also, be sure to have the SelectionMode of the list box set to One.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top