ListBox question

G

Guest

I have two single selection list boxes on a windows form. Both forms get
bound to data in an ArrayList (IList). I want the data in the first list box
to be visible when the form loads. The data that appears in the second list
box is dependent on the slelection of the item in the first list box. So when
an item is selected in listbox 1, the ListBox2_SelectedValueChanged method is
called. This method will then load an ArrayList that is bound to the list box.

My problem is that the ListBox2_SelectedValueChanged gets called multiple
times when the form loads and causes it to crash. This has something to do
with an item being selected in listbox1. I've tried various methods to turn
off the ListBox1 selection, however, nothing seems to work. Should I go about
this some other way? It seems like this should be easy.

My code looks like this:

public Form1()
{

InitializeComponent();

ArrayList A1 = GetA1Data();
ListBx1.DataSource = A1;
ListBx1.DisplayMember = "Description";
ListBx1.ValueMember = "Code";

// niether of these will clear the selection
ListBx1.ClearSelected();
ListBx1.SelectedIndex = -1;
}

private void ListBx2_SelectedValueChanged(object sender, System.EventArgs e)
{
ArrayList AL2= GetAL2Data(ListBx1.SelectedValue.ToString());
ListBx2.DisplayMember = "Long_Desc";
ListBx2.ValueMember = "NDB_No";
ListBx2.DataSource = AL2;
}
 
M

Mr. Arnold

Jerry J said:
I have two single selection list boxes on a windows form. Both forms get
bound to data in an ArrayList (IList). I want the data in the first list
box
to be visible when the form loads. The data that appears in the second
list
box is dependent on the slelection of the item in the first list box. So
when
an item is selected in listbox 1, the ListBox2_SelectedValueChanged method
is
called. This method will then load an ArrayList that is bound to the list
box.

My problem is that the ListBox2_SelectedValueChanged gets called multiple
times when the form loads and causes it to crash. This has something to do
with an item being selected in listbox1. I've tried various methods to
turn
off the ListBox1 selection, however, nothing seems to work. Should I go
about
this some other way? It seems like this should be easy.

My code looks like this:

public Form1()
{

InitializeComponent();

ArrayList A1 = GetA1Data();
ListBx1.DataSource = A1;
ListBx1.DisplayMember = "Description";
ListBx1.ValueMember = "Code";

// niether of these will clear the selection
ListBx1.ClearSelected();
ListBx1.SelectedIndex = -1;
}


It seems to me that your code needs to be in Listbx1_SelectedValueChanged.
private void ListBx1_SelectedValueChanged(object sender, System.EventArgs
e)
{
ArrayList AL2= GetAL2Data(ListBx1.SelectedValue.ToString());
ListBx2.DisplayMember = "Long_Desc";
ListBx2.ValueMember = "NDB_No";
ListBx2.DataSource = AL2;
}

The way you have the code. What would happen if the user made a selection
from ListBx2? All that would seem to happen if left the way you have it is
that the ListBx2 would be loaded again, because of the code you have in
ListBx2_SelectedValueChanged
that would load ListBx2 again, instead of getting ListBx2.SelectedValue and
doing something with it.
 
A

AlexS

First, you don't show how GetAL2Data gets data. Hopefully not interacting
with first listbox.

Second, bracket listbox 2 datasource assignment in BeginUpdate/EndUpdate
calls to switch off intermediate events.

Most probably, you have event handler for listbox 2, which is called
multiple times when datasource is assigned.

HTH
Alex
 
G

Guest

Alexs, the first and second arraylist have no connection with each other.

Arnold, you are correct. I made a mistake writing this text, it is like you
said.

Can anyone tell me why I can't clear the selection before it is posted?

public Form1()
{

InitializeComponent();

ArrayList A1 = GetA1Data();
ListBx1.DataSource = A1;
ListBx1.DisplayMember = "Description";
ListBx1.ValueMember = "Code";

// niether of these will clear the selection
ListBx1.ClearSelected();
ListBx1.SelectedIndex = -1;
}

private void ListBx1_SelectedValueChanged(object sender, System.EventArgs e)
{
ArrayList AL2= GetAL2Data(ListBx1.SelectedValue.ToString());
ListBx2.DisplayMember = "Long_Desc";
ListBx2.ValueMember = "NDB_No";
ListBx2.DataSource = AL2;
}
 
M

Mr. Arnold

Jerry J said:
Alexs, the first and second arraylist have no connection with each other.

Arnold, you are correct. I made a mistake writing this text, it is like
you
said.

Can anyone tell me why I can't clear the selection before it is posted?
Class Whatever

Public Boolean bBypass; // <-------- LOOOOOOK
public Form1()
{

InitializeComponent();

You're going to have to set bBypass.

bBypass = true; // you have to do it here <------- LOOOOOK
ArrayList A1 = GetA1Data();
ListBx1.DataSource = A1;
ListBx1.DisplayMember = "Description";
ListBx1.ValueMember = "Code";

// niether of these will clear the selection
ListBx1.ClearSelected();
ListBx1.SelectedIndex = -1;

bBypass = false; // You have to do it here. <---------LOOOOOOK
}

private void ListBx1_SelectedValueChanged(object sender, System.EventArgs
e)
{
if (bBypass != true) // <----- LOOOOOK
{
ArrayList AL2= GetAL2Data(ListBx1.SelectedValue.ToString());
ListBx2.DisplayMember = "Long_Desc";
ListBx2.ValueMember = "NDB_No";
ListBx2.DataSource = AL2;
}

On the Form load you have to bypass the code in SelectedValueChanged,
because of you binding the Arraylist to the control and every entry from the
Arraylist is making the SelectedValueChange event firer, even
ClearSelected() and SelectedIndex = -1 is making the event firer.

I think if you make the changes, then everything will be ok and all entries
in the box will be non-selected.

You're going to have to do the same thing when you load Listbx2 of bypassing
the code in its SelectedValueChanged event, otherwise, you got the same
problem of code executing during the load of the listbox when it should not
be happening.
 
G

Guest

Mr. Arnold,

Your solution works. Thank you for that!

I wouldn't have expected that every entry from the Arraylist would make the
SelectedValueChange event fire. I would have expected it to fire one time
after the load?
 
M

Mr. Arnold

Jerry J said:
Mr. Arnold,

Your solution works. Thank you for that!

I wouldn't have expected that every entry from the Arraylist would make
the
SelectedValueChange event fire. I would have expected it to fire one time
after the load?

I did a simple test with 3 entries in an arraylist and it fired all 3 times,
even on the Clear and Index = -1.

You have to set a condition on a form load to bypass events for a control if
code is in those events and they firer, when you're trying to load/set a
control with data.
 

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