Two related listBoxes question

G

Guy Dillen

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;

}
 
C

Claes Bergefall

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
 
G

Guy Dillen

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 said:
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 said:
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;

}
 
G

Guy Dillen

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 said:
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 said:
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;

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top