[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

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.
0
larrysy
Asked:
larrysy
  • 4
  • 3
1 Solution
 
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
 
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
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
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

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now