how to disable the event of index_change of ComboBox
There are two ComboBoxes in my application with the same items and should be changed synchronized. However, whenever there is a change to the index, the programe should write data to the Serial Port...So the problem now is, when I change the index of one ComboBox, the other one will also change, however the data will be written to the Serial Port twice...
private void cbFreq_SelectedIndexChanged(object sender, EventArgs e)
{
comboFrequency.Text =cbFreq.Text;
decimal freq = Convert.ToDecimal(cbFreq.Text);
if(registers.GetFrequency().ToString()!=cbFreq.Text)
registers.SetFrequency(freq);
}
private void comboFrequency_SelectedIndexChanged(object sender, EventArgs e)
{
cbFreq.Text =comboFrequency.Text;
decimal freq = Convert.ToDecimal(comboFrequency.Text);
if(registers.GetFrequency().ToString()!=comboFrequency.Text)
registers.SetFrequency(freq);
}
How to make the two comboBoxes changes synchronized with only writing data once to the Serial Port?
Thanks!
[1327 byte] By [
Pockey] at [2007-12-24]
Not sure how your updating your combos...but you could unsubscribe from the events until the serial work is done...and then resubscribe:
namespace WindowsApplication1
{
public partial class Form1 : Form
{
private int test = -1;
private int worker;
public Form1()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox2.SelectedIndexChanged -= new EventHandler(comboBox2_SelectedIndexChanged);
comboBox1.SelectedIndexChanged -= new EventHandler(comboBox1_SelectedIndexChanged);
DoWork();
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox2.SelectedIndexChanged -= new EventHandler(comboBox2_SelectedIndexChanged);
comboBox1.SelectedIndexChanged -= new EventHandler(comboBox1_SelectedIndexChanged);
DoWork();
}
private void Form1_Click(object sender, EventArgs e)
{
test++;
comboBox1.Items.Add(test.ToString());
comboBox2.Items.Add(test.ToString());
}
private void DoWork()
{
for (int i = 0; i < 10; i++)
{
worker++;
this.Text = worker.ToString();
}
comboBox2.SelectedIndexChanged += new EventHandler(comboBox2_SelectedIndexChanged);
comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
}
}
}
namespace
you shouldnt really have to do that - unsubscribe then resubscribe....
you should only use 1 combobox but can I ask why are you using 2 when they do the same thing or hold the same contents?
you could make 1 combobox a "master" one when does everything and writes data to the serial port instead of both of them doing it
Sounds like he does not know which combo will be selected...or when.
Thanks cablehead!
It is OK now with unsubscribe first and then resubscribe again...
I am using two comboboxes for same function because I am not allowed to change the original programe. I am working on someone other's programe. And I should add one more tabpage to it with some usually used functions. So that user can achieve their goal more directly and quickly. I can not modified the original programe...So I have to add one more comboxbox which will do the same function as the original one.
BTW, I am she not he:)
But why it is not the same for the CheckBox?
private void WriteRegister0(object sender, uint data )
{
Send( strReg0 + data.ToString("x4") );
cbFre.CheckedChanged -= new EventHandler(cbFre_CheckedChanged);
cbAout.CheckedChanged -= new EventHandler(cbAout_CheckedChanged);
this.cbFre.Checked = registers.GetCheckRD0State();
this.cbAout.Checked = registers.GetCheckAOUTState();
}
private void cbFre_CheckedChanged(object sender, EventArgs e)
{
cbFre.CheckedChanged += new EventHandler(cbFre_CheckedChanged);
registers.SetCheckRD0(cbFre.Checked);
if (cbFre.Checked)
cbFre.Text = "12.288 MHz";
else
cbFre.Text = "6.144 MHz";
}
private void cbAout_CheckedChanged(object sender, EventArgs e)
{
cbAout.CheckedChanged += new EventHandler(cbAout_CheckedChanged);
registers.SetCheckAOUT(cbAout.Checked);
if (cbAout.Checked)
cbAout.Text = "AOUT ON";
else
cbAout.Text = "AOUT OFF";
}
Although I resubscribe the event, when I change the checked state of the checkbox, nothing will happen...It seems the event is still unsubscribed?
Or it is different with ComboBox and CheckBox?
The Thread.Sleep is just to "see" that the title text is changing in this example.
I used the CheckStateChanged event.
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void checkBox1_CheckStateChanged(object sender, EventArgs e)
{
this.Text = string.Empty;
Thread.Sleep(1000);
checkBox1.CheckStateChanged -=new EventHandler(checkBox1_CheckStateChanged);
checkBox2.CheckStateChanged -= new EventHandler(checkBox2_CheckStateChanged);
if (checkBox1.Checked)
{
checkBox2.Checked = false;
DoWork();
}
checkBox1.CheckStateChanged += new EventHandler(checkBox1_CheckStateChanged);
checkBox2.CheckStateChanged += new EventHandler(checkBox2_CheckStateChanged);
}
private void checkBox2_CheckStateChanged(object sender, EventArgs e)
{
this.Text = string.Empty;
Thread.Sleep(1000);
checkBox1.CheckStateChanged -= new EventHandler(checkBox1_CheckStateChanged);
checkBox2.CheckStateChanged -= new EventHandler(checkBox2_CheckStateChanged);
if (checkBox2.Checked)
{
checkBox1.Checked = false;
DoWork();
}
checkBox1.CheckStateChanged += new EventHandler(checkBox1_CheckStateChanged);
checkBox2.CheckStateChanged += new EventHandler(checkBox2_CheckStateChanged);
}
private void DoWork()
{
for (int i = 0; i < 10; i++)
{
this.Text = i.ToString();
}
}
}
}
Oh, it is my problem.
OK Now!
Thanks!
I just want to add a point that working with subscribing and unsubscribing events is not that perfect and good, and also always produce unpredicted behaviour. If you need such work it's better to solve this with some bool variable instead.
Thanks boban!
I will try to see whether I can solve it by other means.