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;

}

Re: Two related listBoxes question by Claes

Claes
Mon Dec 06 02:48:35 CST 2004

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

"Guy Dillen" <guy_dillen@nospam.hotmail.com> wrote in message
news:41b220ad$0$7834$ba620e4c@news.skynet.be...
> 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;
>
> }
>
>



Re: Two related listBoxes question by Guy

Guy
Mon Dec 06 05:56:43 CST 2004

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



"Claes Bergefall" <claes.bergefall@online.nospam> wrote in message
news:eDGtsB32EHA.2316@TK2MSFTNGP15.phx.gbl...
> 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
>
> "Guy Dillen" <guy_dillen@nospam.hotmail.com> wrote in message
> news:41b220ad$0$7834$ba620e4c@news.skynet.be...
>> 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;
>>
>> }
>>
>>
>
>



Re: Two related listBoxes question by Guy

Guy
Mon Dec 06 07:05:05 CST 2004

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


"Claes Bergefall" <claes.bergefall@online.nospam> wrote in message
news:eDGtsB32EHA.2316@TK2MSFTNGP15.phx.gbl...
> 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
>
> "Guy Dillen" <guy_dillen@nospam.hotmail.com> wrote in message
> news:41b220ad$0$7834$ba620e4c@news.skynet.be...
>> 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;
>>
>> }
>>
>>
>
>