自动调整c#列表框大小(更新)

介绍 这通常是Christian Tratz的自动调整c#列表框的扩展版本。他 没有时间发布这个版本,所以我写了这篇非常短的文章。 改进是'\n'的支持,现在话到 右边缘,行(整数)被更改为字符串,允许自定义值 的头。就是这样,玩得开心! 使用的代码 参见Christian Tratz关于此的文章,《没有改变》。但是如果你使用 项。插入而不是项。添加选定项将 现在移到底部(当然是在有东西插入上面的时候)。另请参阅 上面提供的例子。简而言之:藏起来复制Code

// 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);

然后,你可以开始在列表框的末尾添加消息:复制Code

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."));

或者,将它们插入一个索引:Hide  复制Code

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."));

然后,重新绘制控件:Hide  复制Code

listbox.Invalidate();

历史 版本1.2 -优化的绘图方法:只绘制可见的项目 只有在调整大小后才会进行测量。 版本1.1 -第一版 本文转载于:http://www.diyabc.com/frontweb/news377.html

原文地址:https://www.cnblogs.com/Dincat/p/13443937.html