当在 Winform 中为 Combobox 选择 OwnerDrawFixed 模式时,SelectedIndexChanged 事件未触发

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

我有组合框并将 DrawMode 更改为 OwnerDrawFixed 并处理了 DrawItem 事件,但现在当我尝试将 selectedIndex 更改为 -1 时,因为数据库中没有此值。所以 SelectedIndexChanged 不起作用。

我已将 DropDownStyle 设置为 DropDownList DrawModeOwnerDrawFixed

抽取物品方法:

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
    {
    }
}

现在我没有员工 ID 的值,那么组合框应该设置为 SelectedIndex 到 -1 但它不起作用:

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

我也尝试过处理这个组合框的 SelectedIndexChanged。但是在上述语句之后没有引发事件。

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

请让我知道我做错了什么或有更好的建议。

问题似乎出在绘图中,而不是 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();
}