Testing ListBox.SelectedValue after index changed event

  • Thread starter Thread starter RichGK
  • Start date Start date
R

RichGK

The first time I run my code the list box sends a selected index
changed event even though at this point there has been no activity
with the mouse. The value returned from SelectedValue according to the
debugger is System.Data.DataRowView.

How can I either stop this initial selected index changed event from
happening automatically or alternatively what can I check for so that
I can avoid executing further code if the selected value is not an
int? The problem with this is that I have to cast the returned object
before I can check it so cannot really check for a specific type.

Thanks,
Rich.

lbxTraining.DataSource = empTraining;
lbxTraining.DisplayMember = "display";
lbxTraining.ValueMember = "value";

private void lbxTraining_SelectedIndexChanged(object sender, EventArgs
e)
{
int i = int.Parse(lbxTraining.SelectedValue.ToString());
empTraining = dbEmployeeCom.getEmployeeTraining(i, employee);
}
 
One way is adding a variable during startup or change of datasource

bool startup = true;
lbxTraining.DataSource = empTraining;
lbxTraining.DisplayMember = "display";
lbxTraining.ValueMember = "value"; startup = false;

private void lbxTraining_SelectedIndexChanged(object sender, EventArgs
e)
{
if (!startup)
{
int i = int.Parse(lbxTraining.SelectedValue.ToString());
empTraining = dbEmployeeCom.getEmployeeTraining(i, employee); }
 

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

Back
Top