larrysy
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I still get an infinte loop,
code in form1
code in form2
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
code in form2
If Form1.ComboBox1.SelectedIndex = Me.ComboBox1.SelectedIndex Then
Console.WriteLine("same2")
Else
Form1.ComboBox1.SelectedIndex = Me.ComboBox1.SelectedIndex
End If
Did you try my approach?
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.InvalidOperationExc eption 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
...
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.InvalidOperationExc
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
...
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?
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
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
Does it work if you comment out the below line?
Me.ComboBox1.SelectedIndex
ASKER
It works, but only if the comboboxes are blank on startup.
if (combox1 == combox2)
{
// do nothing
}
else
{
combox2 = combox1
}