Winfrom 中如何实现combox 的列表自动显示ToolTip提示
上边是实现后的结果,找了好长时间,才找到,做个记录。
实现代码如下:
ToolTip tt = null; ComboBox cb = null; private void Form1_Load(object sender, EventArgs e) { cb = new ComboBox(); cb.Items.Insert(0,"第一"); cb.Items.Insert(1,"第二"); cb.Items.Insert(2,"第三"); cb.Items.Insert(3,"第四"); cb.DrawMode = DrawMode.OwnerDrawFixed; cb.DrawItem+=new DrawItemEventHandler(cb_DrawItem); cb.DropDownClosed+=new EventHandler(cb_DropDownClosed); this.Controls.Add(cb); cb.SelectedIndex = 1; tt = new ToolTip(); tt.SetToolTip(cb, "zj"); } void cb_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { // 绘制背景 e.DrawBackground(); //绘制列表项目 e.Graphics.DrawString(cb.Items[e.Index].ToString(), e.Font, System.Drawing.Brushes.Black, e.Bounds); //将高亮的列表项目的文字传递到toolTip1(之前建立ToolTip的一个实例) if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) tt.Show(cb.Items[e.Index].ToString(), cb, e.Bounds.X + e.Bounds.Width, e.Bounds.Y + e.Bounds.Height); e.DrawFocusRectangle(); } void cb_DropDownClosed(object sender, EventArgs e) { tt.Hide(cb); }