INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How-to

Syncronize Multiple ListBoxes by SBendBuckeye
Posted: 15 Dec 05 (Edited 19 Dec 05)

NOTE: As listed below, the code will Select OK but does NOT deselect the controlled listboxes. I am working on a way to resolve it but left the code here in case it would be helpful to someone in its current state.

For purposes of our discussion, create a Windows Form solution and add 5 ListBoxes to it, named lstFirstNames, lstLastNames, lstAges, lstCities and lstStates respectively. Add several items to each item in the Designer or else add items in code like this:

CODE

Me.lstFirstNames.Items.Add("Gary")
Me.lstFirstNames.Items.Add("Sam")
NOTE: Code assumes that ALL ListBoxes have the same number of items.

The DataBinding syntax used in this example is as follows:

CODE

Control.DataBinding.Add( _
    "ControlPropertyToBind", Source, "SourceProperty")
Designate one of the ListBoxes to be the controlling ListBox. In the example below, lstFirstNames was so chosen. In the Form load event, bind all of the remaining ListBoxes to the controlling ListBox like this:

CODE


' Choose a ListBox to be the master and bind all remaining ListBoxes to the master SelectedIndex property.
Me.lstAges.DataBindings.Add( _
    "SelectedIndex", Me.lstFirstNames, "SelectedIndex")
Me.lstCities.DataBindings.Add( _
    "SelectedIndex", Me.lstFirstNames, "SelectedIndex")
Me.lstStates.DataBindings.Add( _
    "SelectedIndex", Me.lstFirstNames, "SelectedIndex")
Me.lstLastNames.DataBindings.Add( _
    "SelectedIndex", Me.lstFirstNames, "SelectedIndex")
In a similar manner, add bindings for TopIndex also:

CODE


' Add bindings for the TopIndex property also.
Me.lstAges.DataBindings.Add( _
    "TopIndex", Me.lstFirstNames, "TopIndex")
Me.lstCities.DataBindings.Add( _
    "TopIndex", Me.lstFirstNames, "TopIndex")
Me.lstStates.DataBindings.Add( _
    "TopIndex", Me.lstFirstNames, "TopIndex")
Me.lstLastNames.DataBindings.Add( _
    "TopIndex", Me.lstFirstNames, "TopIndex")
Now we need to setup a handler for all of the non Controlling ListBoxes to change the Controlling ListBox SelectedIndex in case the user clicks on one of them:

CODE


' Add handler for all ListBoxes but the master ListBox.
AddHandler lstAges.SelectedIndexChanged, _
    AddressOf IndexChanged
AddHandler lstCities.SelectedIndexChanged, _
    AddressOf IndexChanged
AddHandler lstStates.SelectedIndexChanged, _
    AddressOf IndexChanged
AddHandler lstLastNames.SelectedIndexChanged, _
    AddressOf IndexChanged
The only piece left is to write the SelectedIndexChanged method. This method will actually be called 4 times each time a SelectedIndex is changed as the DataBindings do their work. To avoid unneccessary code execution, a static variable is defined to limit setting lstFirstNames.SelectedIndex to once per each set of updates. If you are not familiar with static variables, they retain their values between method calls. I could have accomplished the same thing by using a Module level variable, but since it will not be used outside of the method itself, I chose to use the static variable because it helps to encapsulate all of the code in the method itself.

CODE

Private Sub IndexChanged( _
    ByVal sender As Object, ByVal e As System.EventArgs)

    ' Static variables are only initialized once.
    Static staticSelectedIndex As Integer = -1

    ' MsgBox allows us to see intermediate results.
    ' Note that Sender is CTyped to a couple different types.
    MsgBox("Static: " & staticSelectedIndex.ToString & vbNewLine & "Sender: " & CType(sender, Control).Name & vbNewLine & "Index: " & CType(sender, ListBox).SelectedIndex.ToString, MsgBoxStyle.Information)

    ' Set master ListBox SelectedIndex once per change.
    If CType(sender, ListBox).SelectedIndex <> _
        staticSelectedIndex Then
            staticSelectedIndex = _
                CType(sender, ListBox).SelectedIndex
            Me.lstFirstNames.SelectedIndex = _
                staticSelectedIndex
    End If
End Sub 'IndexChanged
Instead of adding a handler to SelectedIndexChanged, you can also use the regular SelectedIndexChanged event and then add all of the remaining ListBoxes to the Handles clause like this:

CODE


Private Sub lstAges_SelectedIndexChanged( _
   ByVal sender As Object, ByVal e As System.EventArgs) _
        Handles lstAges.SelectedIndexChanged, _
                lstCities.SelectedIndexChanged, _
                lstLastNames.SelectedIndexChanged,  _
                lstStates.SelectedIndexChanged

    ' The code would be identical to the above method
    ' so I did not repeat any of it here.

End Sub 'lstAges_SelectedIndexChanged
Syncronizing ComboBoxes would be similar to the above.

Enjoy...

Back to Visual Basic (Microsoft) VB.NET FAQ Index
Back to Visual Basic (Microsoft) VB.NET Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close