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.
larrysyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

athomsfereCommented:
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
}
0
CodeCruiserCommented:
I have tested the following code which uses some flags



Public Class Form1
    Dim 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
            ComboBox2.SelectedIndex = ComboBox1.SelectedIndex
            IsSynchingA = False
            MsgBox("CB1")
        End If
    End Sub

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

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
larrysyAuthor Commented:
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

0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

CodeCruiserCommented:
Did you try my approach?
0
larrysyAuthor Commented:
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
...
0
larrysyAuthor Commented:
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

0
CodeCruiserCommented:
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
0
larrysyAuthor Commented:
It works, but only if the comboboxes are blank on startup.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.