Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / June 2007

Tip: Looking for answers? Try searching our database.

ListBox question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jerry J - 09 Jun 2007 21:28 GMT
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;
}

Signature

Jerry J

Mr. Arnold - 09 Jun 2007 22:54 GMT
>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
[quoted text clipped - 32 lines]
>  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)
[quoted text clipped - 4 lines]
>  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.
Jerry J - 09 Jun 2007 23:33 GMT
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;
}

Signature

Jerry J

> >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
[quoted text clipped - 50 lines]
> that would load ListBx2 again, instead of getting ListBx2.SelectedValue and
> doing something with it.
Mr. Arnold - 10 Jun 2007 01:40 GMT
> Alexs, the first and second arraylist have no connection with each other.
>
[quoted text clipped - 3 lines]
>
> 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;
[quoted text clipped - 4 lines]
> 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.
Jerry J - 10 Jun 2007 15:10 GMT
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?

Signature

Jerry J

> > Alexs, the first and second arraylist have no connection with each other.
> >
[quoted text clipped - 55 lines]
> problem of code executing during the load of the listbox when it should not
> be happening.
Mr. Arnold - 10 Jun 2007 17:21 GMT
> Mr. Arnold,
>
[quoted text clipped - 4 lines]
> 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.
AlexS - 09 Jun 2007 23:01 GMT
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

>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
[quoted text clipped - 41 lines]
>  ListBx2.DataSource = AL2;
> }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.