Question comparing listbox selected item.

Sythe

Member
Joined
Jul 18, 2012
Messages
13
Programming Experience
Beginner
Hello, can you help me guys with this little problem..


So i have a listbox, and whenever a new data comes in, its already the selected item. Then i need to compare that data if i have that already in my textboxes, else it will be put into textbox that has no data in it.


Here is my code so far..


VB.NET:
        For x = 0 To listBox3.Items.Count
            a = listBox3.SelectedItem
            TextBox6.Text = a


            If listBox3.SelectedItem <> a Then
                b = listBox3.SelectedItem
                TextBox7.Text = b
                
            ElseIf listBox3.SelectedItem <> a & b Then
                c = listBox3.SelectedItem
                TextBox8.Text = c
            End If
        Next
 
This makes no sense:

For x = 0 To listBox3.Items.Count - 1
a = listBox3.SelectedItem
TextBox6.Text = a


You are making Textbox6 the same as the selected item in listbox3, so the first If block will always be False. Your b variable is exactly the same as a.


If listBox3.SelectedItem <> a Then
b = listBox3.SelectedItem
TextBox7.Text = b

The & symbol is used to concatenate strings, so it will simply glue together 2 strings. Besides, there is no value for b since the first part of the If block will never execute, so b is still empty. The result is exactly the same as for the first part of the If block, which means it is still False.

ElseIf listBox3.SelectedItem <> a & b Then
c = listBox3.SelectedItem
TextBox8.Text = c
End If
Next

Try again. You really need to compare the selected item in one listbox with all the items in another listbox, not a textbox.
 
Back
Top