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

时间:2017-05-28 14:12:45

标签: c# winforms combobox selectedindexchanged

我有了combobox并将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");
}

请告诉我我的错误或更好的建议。

1 个答案:

答案 0 :(得分:0)

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