Henri Koppen

I have a form with two comboboxes, only one is allowed to be filled when saving the form.

So I have an on index changed handler on both the comboboxes.

When combobox1 is changed Combobox 2 -> combobox2.SelectedIndex = -1

When combobox2 is changed Combobox 1 -> combobox1.SelectedIndex = -1

However, this creates a loop which results in both comboboxes beiing empty, because changing one combobox fires the event to empty the other as well.

How to solve this handler loop



Re: Windows Forms Designer How to avoid loops with handlers?

Drydo

Declare a boolean at the class level, e.g.

Code Snippet

Private mIsChanging As Boolean = False



then in your 'SelectedIndexChanged' routine for each combo (if you've broken it into two routine) implement something like...

Code Snippet

If Not mIsChanging Then
' Swap the flag
mIsChanging = True

' Make the change

Else
' Reverse the flag
mIsChanging = False
End If






Re: Windows Forms Designer How to avoid loops with handlers?

Henri Koppen

Thx for your reply. However, I don't get it to work. This may not have anything to do with your example, but there's some more happening on the form.

On of the things is that all my comboboxes are self made controls because of some bug with comboxes (when choosing from a list and making a selection empty and then switching focus to another control sets the Text property on empty but leaves the selectedindex intact.), so I made some Leave events as well and some logic to make forms "Dirty".

All these events work perfect except in the case I just descripted in my original topic.

Is there some more elegant solution to disable handlers or cancel them





Re: Windows Forms Designer How to avoid loops with handlers?

Rong-Chun Zhang - MSFT

Hi Koppen,

Try something like this:

Code Snippet

namespace CBO

{

public partial class Form4 : Form

{

public Form4()

{

InitializeComponent();

}

private void Form4_Load(object sender, EventArgs e)

{

DataTable dt = new DataTable();

dt.Columns.Add("aa");

dt.Columns.Add("bb");

for (int i = 0; i < 20; i++)

{

dt.Rows.Add(i.ToString("00"), "bb" + i.ToString("00"));

}

this.comboBox1.DisplayMember = "bb";

this.comboBox1.ValueMember = "aa";

this.comboBox1.DataSource = dt;

this.comboBox1.SelectedIndex = -1;

this.comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);

this.comboBox2.DisplayMember = "bb";

this.comboBox2.ValueMember = "aa";

this.comboBox2.DataSource = dt.Copy();

this.comboBox2.SelectedIndex = -1;

this.comboBox2.SelectedIndexChanged += new EventHandler(comboBox2_SelectedIndexChanged);

}

void comboBox2_SelectedIndexChanged(object sender, EventArgs e)

{

if (this.comboBox2.SelectedIndex != -1)

this.comboBox1.SelectedIndex = -1;

}

void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

{

if (this.comboBox1.SelectedIndex != -1)

this.comboBox2.SelectedIndex = -1;

}

}

}

Hope this helps.

Regards






Re: Windows Forms Designer How to avoid loops with handlers?

Henri Koppen

Thx, for your reply. Indeed with #C it works.

It does not work in vb.net, I cannot pinpoint the difference.

Can you explain how the logic works And why there's no circulair update





Re: Windows Forms Designer How to avoid loops with handlers?

Rong-Chun Zhang - MSFT

Hi Henri,

Below is my VB sample. It works fine on my machine.

Code Snippet

Public Class Form1

Private Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

Dim dt As New DataTable()

dt.Columns.Add("aa")

dt.Columns.Add("bb")

For i As Integer = 0 To 19

dt.Rows.Add(i.ToString("00"), "bb" + i.ToString("00"))

Next

Me.ComboBox1.DisplayMember = "bb"

Me.ComboBox1.ValueMember = "aa"

Me.ComboBox1.DataSource = dt

Me.ComboBox1.SelectedIndex = -1

AddHandler Me.ComboBox1.SelectedIndexChanged, AddressOf comboBox1_SelectedIndexChanged

Me.ComboBox2.DisplayMember = "bb"

Me.ComboBox2.ValueMember = "aa"

Me.ComboBox2.DataSource = dt.Copy()

Me.ComboBox2.SelectedIndex = -1

AddHandler Me.ComboBox2.SelectedIndexChanged, AddressOf comboBox2_SelectedIndexChanged

End Sub

Private Sub comboBox2_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)

If Me.ComboBox2.SelectedIndex <> -1 Then

Me.ComboBox1.SelectedIndex = -1

End If

End Sub

Private Sub comboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)

If Me.ComboBox1.SelectedIndex <> -1 Then

Me.ComboBox2.SelectedIndex = -1

End If

End Sub

End Class

Hope this helps.

Regards