c# - 绘制的线不显示在组合框中

标签 c# winforms combobox system.drawing

我需要您帮助解决以下问题(使用 .Net 3.5 和 Windows 窗体):

我只是想在位于窗体上的组合框(Windows 窗体)的中间画一条线。 我使用的代码是:

void comboBox1_Paint(object sender, PaintEventArgs e)
{
  e.Graphics.DrawLine(new Pen(Brushes.DarkBlue),
                      this.comboBox1.Location.X,
                      this.comboBox1.Location.Y + (this.comboBox1.Size.Height / 2),
                      this.comboBox1.Location.X + this.comboBox1.Size.Width,
                      this.comboBox1.Location.Y + (this.comboBox1.Size.Height / 2));
}

要触发绘制事件:

private void button1_Click(object sender, EventArgs e)
{
  comboBox1.Refresh();
}

当我执行代码并按下按钮时,不会绘制线条。在调试中,绘画处理程序处的断点没有被命中。奇怪的是MSDN上ComBox 的事件列表中有一个绘制事件,但在 VS 2010 IntelliSense 中没有在 ComboBox 的成员中找到此类事件

谢谢。

最佳答案

public Form1() 
{
  InitializeComponent();
  comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
  comboBox1.DrawItem += new DrawItemEventHandler(comboBox1_DrawItem);
}

void comboBox1_DrawItem(object sender, DrawItemEventArgs e) {
  e.DrawBackground();
  e.Graphics.DrawLine(Pens.Black, new Point(e.Bounds.Left, e.Bounds.Bottom-1),
    new Point(e.Bounds.Right, e.Bounds.Bottom-1));
  TextRenderer.DrawText(e.Graphics, comboBox1.Items[e.Index].ToString(), 
    comboBox1.Font, e.Bounds, comboBox1.ForeColor, TextFormatFlags.Left);
  e.DrawFocusRectangle();
}

关于c# - 绘制的线不显示在组合框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11862252/

相关文章:

c# - 如果枚举值相同,我将获得哪个枚举常量

c# - 从向量列表创建所有可能组合的算法函数

c# - C# Parent 和 Child 中的 MDI 窗体

c# - 在 DataGridView 的特定行中隐藏网格线

vb.net - 将 MS Access 数据库中的列值填充到 VB .Net 组合框下拉值中?

c# - 如何正确使用 ValueGeneratedOnUpdate()?

c# - WebResponse 不返回任何 cookie

c# - 类似于 jQuery UI Draggables 的免费 Windows 窗体组件

c# - 为什么ComboBox在WPF MVVM中保留不同的值?

c# - 使用 aspxgridview 的值填充 GridViewDataComboBoxColumn