I want to relate 2 listBoxes. Although during the filling of the first
listBox1; the event SelectedIndexChanged already fires (this is not the
purpose). The event should only fire when a select an item in listBox1.
How can i avoid that the event fires too early?
...
private void button3_Click(object sender, System.EventArgs e)
{
DataSet ds = new DataSet();
DataAdapter da = null;
string SQLcom = "SELECT toto, id FROM table";
da = new DataAdapter(SQLcom, conn);
da.Fill(ds, "table");
listBox1.DataSource = ds.Tables["table"];
listBox1.ValueMember = "id";
listBox1.DisplayMember = "toto";
listBox1.SelectedIndex = -1;
}
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs
e)
{
DataSet ds = new DataSet();
DataAdapter da = null;
string SQLcom = "SELECT tata, id FROM table2";
da = new DataAdapter(SQLcom, conn);
da.Fill(ds, "table2");
listBox2.DataSource = ds.Tables["table2"];
listBox2.ValueMember = "id";
listBox2.DisplayMember = "tata";
listBox2.SelectedIndex = -1;
}
Claes Bergefall - 06 Dec 2004 08:48 GMT
Turn of the eventhandler during loading and add it back
when done,
listBox1.SelectedIndexChanged -= new
EventHandler(listBox1_SelectedIndexChanged)
...
listBox1.SelectedIndexChanged += new
EventHandler(listBox1_SelectedIndexChanged)
or use a flag that you check at the beginning
of listBox1_SelectedIndexChanged
/claes
> I want to relate 2 listBoxes. Although during the filling of the first
> listBox1; the event SelectedIndexChanged already fires (this is not the
[quoted text clipped - 49 lines]
>
> }
Guy Dillen - 06 Dec 2004 11:56 GMT
Ok thx for the info.
I suppose this is the correct way to achive this (=relating/linking 2
listBoxes)? Or is this way not the correct one?
Guy
> Turn of the eventhandler during loading and add it back
> when done,
[quoted text clipped - 64 lines]
>>
>> }
Guy Dillen - 06 Dec 2004 13:05 GMT
Thanks for your help, it works.
One more question:
is this the correct way to achive this (realting/linking listBoxes)? Or are
there other more used methods to achieve this?
Guy
> Turn of the eventhandler during loading and add it back
> when done,
[quoted text clipped - 64 lines]
>>
>> }