一个自动调整大小的 C# Listbox(已更新)






3.74/5 (13投票s)
2004年6月22日

180357

4747
一个可以自动调整文本大小的 Listbox。
引言
这通常是 Christian Tratz 的 自动调整大小的 C# Listbox 的扩展版本。 他没有时间发布这个版本,所以我写了这篇文章。 改进包括对 '\n'
的支持,之前有些单词会到达右边缘,行(整数)被更改为字符串以允许在标题中使用自定义值。 就这样,玩得开心!
使用代码
请参阅 Christian Tratz 的文章,没有变化。 但是,如果您使用 Item.Insert
代替 Item.Add
,则选定的项目现在将移动到底部(当在上方插入内容时,当然)。 另请参阅上面的示例。 简而言之
// Create the Listbox..
listbox = new ListBox.MessageListBox();
listbox.Size = new Size( this.ClientSize.Width, this.ClientSize.Height);
listbox.Dock = DockStyle.Top;
// .. and add it to the controls.
this.Controls.Add( listbox);
然后,您可以开始将消息添加到 Listbox 的末尾
listbox.Items.Add(
new ListBox.ParseMessageEventArgs(
ListBox.ParseMessageType.Info,
"Info1",
"Some information.. there should be enough text in this area
to see that the resizing works! The text is wrapped in between
words as much as possible, since this control has no knowledge
of words. However, when not possible the words are split."));
或者,在索引处插入它们
listbox.Items.Insert(
1,
new ListBox.ParseMessageEventArgs(
ListBox.ParseMessageType.None,
"Some more info",
"Text can also be inserted at any index. Previously selected
text will e.g. move to the bottom when something is inserted
above."));
之后,重新绘制控件
listbox.Invalidate();
历史
- 版本 1.2 - 优化了绘制方法:仅绘制可见的项目,并且测量仅在调整大小后进行。
- 版本 1.1 - 第一个版本