简体   繁体   中英

SelectedIndexChanged event is not firing when OwnerDrawFixed mode selected for Combobox in Winform

I have combobox and changed DrawMode to OwnerDrawFixed and handled DrawItem event, but now when i tried to change selectedIndex to -1 as there is no value in database for this. so SelectedIndexChanged is not working.

I have set DropDownStyle to DropDownList DrawMode to OwnerDrawFixed

Draw Item Method:

private void cmbDetTechnician_DrawItem(object sender, DrawItemEventArgs e)
{
    try
    {
         int index = e.Index >= 0 ? e.Index : 0;
         var brush = Brushes.Black;
         e.DrawBackground();
         e.Graphics.DrawString(lstTechnician[index].DisplayName.ToString(), e.Font, brush, e.Bounds, StringFormat.GenericDefault);
         e.DrawFocusRectangle();
    }
    catch
    {
    }
}

Now i don't have value to Employee ID then combobox should be set to SelectedIndex to -1 but it is not working:

if(_EmployeeID == -1){cmbDetTechnician.SelectedIndex =  -1; } else { cmbDetTechnician.SelectedValue = _EmployeeID; }

I have also tried to handle SelectedIndexChanged of this combobox. but event is not raised after above statement.

private void cmbDetTechnician_SelectedIndexChanged(object sender, EventArgs e)
{
   CustomMessageBox.Show("HI");
}

Please let me know what i am doing wrong or any better suggestion.

The problem seems to be in the drawing, not the SelectedIndex...

if(e.Index >= 0)
{
    int index = e.Index;
    var brush = Brushes.Black;
    e.DrawBackground();
    e.Graphics.DrawString(lstTechnician[index].DisplayName.ToString(), e.Font, brush, e.Bounds, StringFormat.GenericDefault);
    e.DrawFocusRectangle();
}
else
{
    e.DrawBackground();
    e.DrawFocusRectangle();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM