G
Gene Wirchenko
Dear C-Sharpies:
I am now digging into forms. Combobox gave me some trouble, but
I have mostly handled it. How well did I do though?
I have a combobox with Canadian provinces. When the selected
index changes, I want a message box to be displayed. Here is my code:
***** Start of Code Segment *****
private void cboProvince_SelectedIndexChanged(object
sender,EventArgs e)
{
if (!fInit)
MessageBox.Show(
"Index: "+cboProvince.SelectedIndex.ToString()+
"\nName: "+
((Province)cboProvince.SelectedItem).LongName.ToString()+
"\nAbbr: "+cboProvince.SelectedValue.ToString(),
"Province Selected");
}
***** End of Code Segment *****
Note the if. It seems that when a combobox is initialising,
SelectedIndexChanged fires with SelectedIndex being 0 (I think once
for each displayed item) then once with a SelectedIndex of -1. I do
not want a message box then.
To get rid of the extraneous message boxes, I declared a form
field
bool fInit=true;
At the end of the form's constructor, I set fInit to false. This does
work to suppress the extraneous message boxes. It does have the smell
of a kludge though.
Is there a better way to do it?
Sincerely,
Gene Wirchenko
I am now digging into forms. Combobox gave me some trouble, but
I have mostly handled it. How well did I do though?
I have a combobox with Canadian provinces. When the selected
index changes, I want a message box to be displayed. Here is my code:
***** Start of Code Segment *****
private void cboProvince_SelectedIndexChanged(object
sender,EventArgs e)
{
if (!fInit)
MessageBox.Show(
"Index: "+cboProvince.SelectedIndex.ToString()+
"\nName: "+
((Province)cboProvince.SelectedItem).LongName.ToString()+
"\nAbbr: "+cboProvince.SelectedValue.ToString(),
"Province Selected");
}
***** End of Code Segment *****
Note the if. It seems that when a combobox is initialising,
SelectedIndexChanged fires with SelectedIndex being 0 (I think once
for each displayed item) then once with a SelectedIndex of -1. I do
not want a message box then.
To get rid of the extraneous message boxes, I declared a form
field
bool fInit=true;
At the end of the form's constructor, I set fInit to false. This does
work to suppress the extraneous message boxes. It does have the smell
of a kludge though.
Is there a better way to do it?
Sincerely,
Gene Wirchenko