List Box Sudden Exit On Selection
-
Gentleman, I am using a list box to list some string information gathered from an MSSQL database. I noticed that if I select the box and should "miss" the string line (selecting empty space), a null exception is thrown. The problem is that it also closes the application. I have tried several different ways to escape this by showing the exception (try, catch) and trying to return to the application, but they have all failed. Ideas? Thank You, Pat
-
Gentleman, I am using a list box to list some string information gathered from an MSSQL database. I noticed that if I select the box and should "miss" the string line (selecting empty space), a null exception is thrown. The problem is that it also closes the application. I have tried several different ways to escape this by showing the exception (try, catch) and trying to return to the application, but they have all failed. Ideas? Thank You, Pat
You say that an exception causes your program to close. This is normal. You must show what code you're using in the selection event handler to get any real help with this.
The difficult we do right away... ...the impossible takes slightly longer.
-
Gentleman, I am using a list box to list some string information gathered from an MSSQL database. I noticed that if I select the box and should "miss" the string line (selecting empty space), a null exception is thrown. The problem is that it also closes the application. I have tried several different ways to escape this by showing the exception (try, catch) and trying to return to the application, but they have all failed. Ideas? Thank You, Pat
A lot of Controls, including
System.Windows.Forms.ListBox
will fire unexpected events, forcing you to code defensively. As an example, a SelectionChanged event (or whatever it is called) may be fired when an item gets deselected, which you can check by looking for a negative SelectedIndex, or a SelectedItem being null. If you don't, you're bound to run into trouble. Also don't be surprised it you use OwnerDraw mode and get MeasureItem/DrawItem events with an index of -1. IMO all these can be avoided by proper testing before acting; and all can be caught with proper try-catch constructs. Finally, without actual code, we can't provide more specific help. I have noticed you seldom provide code when in fact you should. :)Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Gentleman, I am using a list box to list some string information gathered from an MSSQL database. I noticed that if I select the box and should "miss" the string line (selecting empty space), a null exception is thrown. The problem is that it also closes the application. I have tried several different ways to escape this by showing the exception (try, catch) and trying to return to the application, but they have all failed. Ideas? Thank You, Pat
My guess is you're doing SelectedItem.something in the event handler. If you select nothing, SelectedItem will be null, and SelectedIndex will be -1. You should always check for these conditions in any event handler that uses one of these properties; I typically put a guard at the top, since you usually don't want to do anything in that case:
void ListSelectionChanged(object sender, EventArgs e){
ListControl list = (ListControl)sender;
if(list.SelectedIndex < 0) return;
... // do stuff with list.SelectedItem
} -
A lot of Controls, including
System.Windows.Forms.ListBox
will fire unexpected events, forcing you to code defensively. As an example, a SelectionChanged event (or whatever it is called) may be fired when an item gets deselected, which you can check by looking for a negative SelectedIndex, or a SelectedItem being null. If you don't, you're bound to run into trouble. Also don't be surprised it you use OwnerDraw mode and get MeasureItem/DrawItem events with an index of -1. IMO all these can be avoided by proper testing before acting; and all can be caught with proper try-catch constructs. Finally, without actual code, we can't provide more specific help. I have noticed you seldom provide code when in fact you should. :)Luc Pattyn [My Articles] Nil Volentibus Arduum
Thank you Luc (nice of you to reply to me again) and to all of you in fact for your excellent replies. All are good advice and I intent to use it all. Re: The Issue; most of the time, I usually find myself embarrassed when I finally solve some of these things. I employed the following simple one line of code in the SelectedIndexChanged event to resolve the issue:
private void listBoxXxx_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBoxXxx.Items.Count < 1)
{
string msg = "There are no Items in the list to select from. ";
MessageBox.Show(msg, "Required Information Missing", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}if (listBoxXxx.SelectedIndex != -1) { labelXxx.Text = listBoxXxx.SelectedItem.ToString(); } }
To my friend and mentor Luc...actually, I looked up my history and I usually do supply the code when the issue is code failure but not when (I don't know how) which is sometimes the case. In this case, I should have supplied the following, but it was so basic I could not understand the possible link, so please accept my apology.
//Load the Main page frmMain m = new frmMain(fname, title, firm, firmId, permission, theDate, userId, chal, CONNSTR); this.Hide(); m.ShowDialog(); } } catch (SqlException Sqlex) //Catch Sql Errors { MessageBox.Show("Error: " + Sqlex, "SQL Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception ex) //Catch All Errors { MessageBox.Show("Error: " + ex, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; }
Most important...thank you all for your interest and expertise. Luc, your answer was closest to what I used and I am marking it as the correct solution. Best Regards, Pat :) :)