Windows Develop Bookmark and Share   
 index > Windows Forms Designer > How to avoid loops with handlers?
 

How to avoid loops with handlers?

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 Combobox1-> 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?

Henri Koppen  Thursday, July 12, 2007 8:08 AM

Hi Koppen,

Try something like this:

Code Snippet

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;

}

}

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


Drydo  Thursday, July 12, 2007 1:12 PM

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?

Henri Koppen  Friday, July 13, 2007 7:06 AM

Hi Koppen,

Try something like this:

Code Snippet

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;

}

}

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?

Henri Koppen  Sunday, July 15, 2007 8:31 AM

Hi Henri,

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

Code Snippet

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

Class

Hope this helps.

Regards

Rong-Chun Zhang  Tuesday, July 17, 2007 1:01 PM

You can use google to search for other answers

Custom Search

More Threads

• How do I disable the MenuStrip's ALT-key action in C#?
• Design-time functionality at run-time
• Switching between languages on a localized form resets bindingsource on form
• Using DirectX to render non-game Windows.Forms controls
• IPersistComponentSettings
• class Constructor help - passing arguements
• Embeded WindowsMediaPlayer has bugs
• Please help with Phone Book Application
• IDesignerHost CreateComponent Method
• ZOrder