0
votes

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.

1
Do not hide programming bugs with try/catch-em-all/do-nothing.Hans Passant

1 Answers

0
votes

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();
}