c# comboBox Initialization problem
I have two comboBox that are dependend on each other. If one changes the other changes as well. Now that is not a problem there what I can see, the problem is when this windowsform gets loaded and for some reason the selectedindex stands on 0 instead of -1 for a second before displayed where it gets to -1 as told. Now my problem here is how it can be selectedindex 0, this problem leads to the DialogResult being set to OK where it not should be.
Help would be appreciated.
The code:
private void Initialize_comboBox_FindCustomerByName()
{
ArrayList cutomerIdList = customerLogic.GetCustomers();
if (0 < cutomerIdList.Count)
{
ArrayList customers = new ArrayList();
customers.Add(new Customer("Ny Kund"));
foreach (int i in cutomerIdList)
{
customers.Add(new Customer(i));
}
comboBox_FindCustomerByName.DataSource = customers;
comboBox_FindCustomerByName.DisplayMember = "Name";
comboBox_FindCustomerByName.ValueMember = "Id";
}
comboBox_FindCustomerByName.SelectedIndex = -1;
comboBox_FindCustomerByName.Text = "Sök kund";
}
private void Initialize_comboBox_FindCustomerByNumber()
{
ArrayList cutomerIdList = customerLogic.GetCustomers_SortedByCustomerNumber( );
if (0 < cutomerIdList.Count)
{
ArrayList customers = new ArrayList();
customers.Add(new Customer("Ny Kund"));
foreach (int i in cutomerIdList)
{
customers.Add(new Customer(i));
}
comboBox_FindCustomerByNumber.DataSource = customers;
comboBox_FindCustomerByNumber.DisplayMember = "CustomerNumber";
comboBox_FindCustomerByNumber.ValueMember = "Id";
}
comboBox_FindCustomerByNumber.SelectedIndex = -1;
comboBox_FindCustomerByNumber.Text = "Sök kund";
}
private void comboBox_FindCustomerByName_SelectedIndexChanged(o bject sender, System.EventArgs e)
{
if (!comboBox_FindCustomerByName.SelectedIndex.Equals (-1))
{
button_Save.Enabled = true;
if (comboBox_FindCustomerByName.SelectedValue.Equals(-2))
{
this.DialogResult = DialogResult.No;
currentCustomer = null;
}
else
{
comboBox_FindCustomerByNumber.SelectedValue = comboBox_FindCustomerByName.SelectedValue;
currentCustomer = (Customer)comboBox_FindCustomerByNumber.SelectedIt em;
this.DialogResult = DialogResult.OK;
}
}
else
{
button_Save.Enabled = false;
this.DialogResult = DialogResult.No;
}
}
private void comboBox_FindCustomerByNumber_SelectedIndexChanged (object sender, System.EventArgs e)
{
if (!comboBox_FindCustomerByNumber.SelectedIndex.Equa ls(-1))
{
button_Save.Enabled = true;
if (comboBox_FindCustomerByNumber.SelectedValue.Equal s(-2))
{
this.DialogResult = DialogResult.No;
currentCustomer = null;
}
else
{
comboBox_FindCustomerByName.SelectedValue = comboBox_FindCustomerByNumber.SelectedValue;
currentCustomer = (Customer)comboBox_FindCustomerByName.SelectedItem ;
this.DialogResult = DialogResult.OK;
}
}
else
{
button_Save.Enabled = false;
this.DialogResult = DialogResult.No;
}
}
|