Link to home
Start Free TrialLog in
Avatar of larrysy
larrysyFlag for Canada

asked on

syncing combo box selected item

I have a combo box in 2 separate forms.  I want both to be in sync bidirectionally - a change in one updates the other. How can I do that without resulting in an infinite loop?  I'm using VB.net.  Thanks.
Avatar of athomsfere
athomsfere
Flag of United States of America image

You could put it in the change event to change the other, and use an If statement: (psuedocode below)

if (combox1 == combox2)
{
// do nothing
}

else
{
combox2 = combox1
}
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of larrysy

ASKER

I still get an infinte loop,

code in form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Form2.Show()
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        If Form2.ComboBox1.SelectedIndex = Me.ComboBox1.SelectedIndex Then
            Console.WriteLine("same1")
        Else
            Form2.ComboBox1.SelectedIndex = Me.ComboBox1.SelectedIndex
        End If
    End Sub

Open in new window


code in form2
 
If Form1.ComboBox1.SelectedIndex = Me.ComboBox1.SelectedIndex Then
            Console.WriteLine("same2")
        Else
            Form1.ComboBox1.SelectedIndex = Me.ComboBox1.SelectedIndex
        End If

Open in new window

Did you try my approach?
Avatar of larrysy

ASKER

Hi CodeCruiser,

I get the following error if I have items in the combobox at design time.  
If I clear out the items and add them at run time, the code works.


System.InvalidOperationException was unhandled
  Message=An error occurred creating the form. See Exception.InnerException for details.  The error is: InvalidArgument=Value of '0' is not valid for 'SelectedIndex'.
Parameter name: SelectedIndex
...
Avatar of larrysy

ASKER

Hi CodeCruiser,

I have the combobox in 2 separate forms and I was able to get the following code to work based on your method..  Any  other suggestions?

Public Class Form1
    Public IsSynchingA, IsSynchingB As Boolean
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        If Not IsSynchingB Then
            IsSynchingA = True
            Form2.ComboBox1.SelectedIndex = Me.ComboBox1.SelectedIndex
            IsSynchingA = False
            MsgBox("CB1")
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Form2.Show()
        Me.ComboBox1.Items.Clear()
        Form2.ComboBox1.Items.Clear()
        For i As Integer = 1 To 5
            Me.ComboBox1.Items.Add(i)
            Form2.ComboBox1.Items.Add(i)
        Next
        Me.ComboBox1.SelectedIndex = 0
    End Sub
End Class

Public Class Form2
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        If Not Form1.IsSynchingA Then
            Form1.IsSynchingB = True
            Form1.ComboBox1.SelectedIndex = Me.ComboBox1.SelectedIndex
            Form1.IsSynchingB = False
            MsgBox("CB2")
        End If
    End Sub
End Class

Open in new window

Do you mean you are unable to get this to work?

Does it work if you comment out the below line?

Me.ComboBox1.SelectedIndex = 0
Avatar of larrysy

ASKER

It works, but only if the comboboxes are blank on startup.