【问题标题】:Can ListBox items span multiple lines? C# [duplicate]ListBox 项目可以跨越多行吗? C# [重复]
【发布时间】:2013-08-21 14:58:24
【问题描述】:

我想要一个 ListBox 控件包含跨越多行的项目。

基本上我想要的是每个项目跨越多行并且可以作为一个项目选择。有没有办法做到这一点?

【问题讨论】:

标签: c# .net winforms


【解决方案1】:

正如LarsTech 在他的评论中所建议的那样,所有其他 cmets 都会导致某种完全编码的示例,这可能会让您感到困惑。我只用几行代码制作了这个演示,让您可以轻松上手:

 listBox1.DrawMode = DrawMode.OwnerDrawVariable;
 //First add some items to your listBox1.Items     
 //MeasureItem event handler for your ListBox
 private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
 {
   if (e.Index == 2) e.ItemHeight = 50;//Set the Height of the item at index 2 to 50
 }
 //DrawItem event handler for your ListBox
 private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
 {
   e.DrawBackground();
   e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds);
 }

【讨论】:

  • 这很好,只是它没有显示如何将事件绑定到列表框。我在这里找到了一个例子,它显示了它。 msdn.microsoft.com/en-us/library/…
  • @CPS 注册事件只是一个刚开始的工作,我想你已经知道了。
  • 我知道他们需要,但我不知道我特别需要的事件的名称。为了完整起见,我包含了该链接,以便其他遇到此问题的人可以获得完整的答案。
  • 只是为了他人的利益而指出:当DrawMode 设置为OwnerDrawFixedOwnerDrawVariable 时,双缓冲ListBox 无法正常工作。如果这成为一个问题,创建一个ListBox 的子类并自己实现OnPaint 方法似乎可以解决这个问题。
猜你喜欢
  • 1970-01-01
  • 2018-10-20
  • 2023-02-02
  • 2021-12-03
  • 2022-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多