Winform ComboBox控件高亮显示

时间:2023-01-22 05:39:17
            //重绘下拉表单窗口,需要在窗口设计代码中加入下面这一句
this.cmdChannelName.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
//下拉表单重绘事件
this.cmdChannelName.DrawItem += new System.Windows.Forms.DrawItemEventHandler(kineticFileComboBox_DrawItem);
this.cmdChannelName.DropDownClosed += new System.EventHandler(kineticFileComboBox_DropDownClosed); #region ComboBox控件高亮显示
//[主程序中的代码]
private void kineticFileComboBox_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
// 绘制背景
e.DrawBackground();
//绘制列表项目
e.Graphics.DrawString(cmdChannelName.Items[e.Index].ToString(), e.Font, System.Drawing.Brushes.Black, e.Bounds);
//将高亮的列表项目的文字传递到toolTip1(之前建立ToolTip的一个实例)
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
toolTip1.Show(cmdChannelName.Items[e.Index].ToString(), cmdChannelName, e.Bounds.X + e.Bounds.Width, e.Bounds.Y + e.Bounds.Height);
e.DrawFocusRectangle();
} //关闭列表时,同时关闭toolTip1的显示
private void kineticFileComboBox_DropDownClosed(object sender, System.EventArgs e)
{ toolTip1.Hide(cmdChannelName); }
#endregion

效果图:

Winform ComboBox控件高亮显示