Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two list boxs in a form
one contains students name
another contains ages of students
if i scroll first listbox then second listbox should scroll
automatically
how to do?

What I have tried:

tried scroll event of listbox in different way but not successful
VB.NET
Private Sub ListBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox2.SelectedIndexChanged
   If ListBox2.TopIndex <> ListBox1.TopIndex Then
      ListBox2.TopIndex = ListBox1.TopIndex
   End If

   If ListBox2.SelectedIndex <> ListBox1.SelectedIndex Then
      ListBox2.SelectedIndex = ListBox1.SelectedIndex
   End If
End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
   If ListBox1.TopIndex <> ListBox2.TopIndex Then
      ListBox1.TopIndex = ListBox2.TopIndex
   End If

   If ListBox1.SelectedIndex <> ListBox2.SelectedIndex Then
      ListBox1.SelectedIndex = ListBox2.SelectedIndex
   End If
End Sub
Posted
Updated 6-May-20 22:57pm
v2
Comments
phil.o 5-May-20 8:19am    
Please tag which UI you're interested in: Windows Forms, WPF, ASP.NET?
Please also detail what you have tried exactly which was unsuccessful.
CodeMine 5-May-20 10:02am    
Windows Forms VB.NET
CodeMine 5-May-20 10:20am    
Private Sub ListBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox2.SelectedIndexChanged
If ListBox2.TopIndex <> ListBox1.TopIndex Then
ListBox2.TopIndex = ListBox1.TopIndex
End If

If ListBox2.SelectedIndex <> ListBox1.SelectedIndex Then
ListBox2.SelectedIndex = ListBox1.SelectedIndex
End If
End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
If ListBox1.TopIndex <> ListBox2.TopIndex Then
ListBox1.TopIndex = ListBox2.TopIndex
End If

If ListBox1.SelectedIndex <> ListBox2.SelectedIndex Then
ListBox1.SelectedIndex = ListBox2.SelectedIndex
End If
End Sub
phil.o 5-May-20 10:23am    
Please do not put code in comments; better use the green "Improve Question" widget and qualify your question instead. I do it for you this time, next time please follow this guideline.
CodeMine 5-May-20 20:48pm    
thank u

Your own Solution doesn't work because the effect is wrong programmed. Change it like this :
VB
Private Sub ListBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox2.SelectedIndexChanged
   If ListBox1.TopIndex <> ListBox2.TopIndex Then
      ListBox1.TopIndex = ListBox2.TopIndex
   End If

   If ListBox1.SelectedIndex <> ListBox2.SelectedIndex Then
      ListBox1.SelectedIndex = ListBox2.SelectedIndex
   End If
End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
   If ListBox2.TopIndex <> ListBox1.TopIndex Then
      ListBox2.TopIndex = ListBox1.TopIndex
   End If

   If ListBox2.SelectedIndex <> ListBox1.SelectedIndex Then
      ListBox2.SelectedIndex = ListBox1.SelectedIndex
   End If
End Sub


The Change appears at (for example) Listbox1 - so you should modify Listbox2. You did it the other way round ...
 
Share this answer
 
 
Share this answer
 
Comments
CodeMine 5-May-20 20:59pm    
this is in c#
it is is vb then it will be helpful for me.
ZurdoDev 5-May-20 22:34pm    
They are very similar. Most people use C# which is why most examples online will be in C#.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900