C# CheckedListBox水平滚动条不允许我查看所有文本
我有一个基于CheckedListBox的自定义控件C# CheckedListBox水平滚动条不允许我查看所有文本,c#,.net,winforms,horizontal-scrolling,checkedlistbox,C#,.net,Winforms,Horizontal Scrolling,Checkedlistbox,我有一个基于CheckedListBox的自定义控件 public CustomCheckedListBox : CheckedListBox { public void AddItems(CustomCollection MyCollection) { foreach(var C in MyCollection.Items) { // Some logic is here to determine if an item
public CustomCheckedListBox : CheckedListBox
{
public void AddItems(CustomCollection MyCollection)
{
foreach(var C in MyCollection.Items)
{
// Some logic is here to determine if an item should be added.
this.Items.Add(C); // The C object has a string overload
}
}
}
当此控件与一些长文本一起使用时,水平滚动条的大小似乎不正确。当我向右滚动时,文本被切断
如果在控件加载后清除Items集合并再次写入其内容,则水平滚动条的大小正确
你知道这是什么原因吗
如果只是使用标准的CheckedListBox,我也会遇到同样的问题
在红线上,末尾大约缺少5个字符。所有角色类型都会发生这种情况。
在
列表框
实现中似乎有一个错误,它使用不同的方法测量项目文本宽度,这导致了您遇到的行为
要解决此问题,可以使用以下帮助程序(警告:使用ListBox
private实现成员)
非常有趣的代码。我喜欢,但不管用。我在调用方法填充checkedlistbox之后直接调用它。这是在它显示之前填充的。@CathalMF我已经在构造函数、
Load
事件和窗体显示并运行之后对它进行了测试。确保CheckedListBox.UseCompatibleTextRendering
属性设置为false
。
using System;
using System.Linq.Expressions;
using System.Windows.Forms;
namespace Samples
{
public static class ListBoxUtils
{
public static void UpdateHorizontalScrollbar(this ListBox target)
{
updateHorizontalScrollbarFunc(target);
}
private static readonly Action<ListBox> updateHorizontalScrollbarFunc = CreateUpdateScrollbarFunc();
private static Action<ListBox> CreateUpdateScrollbarFunc()
{
var target = Expression.Parameter(typeof(ListBox), "target");
var body = Expression.Block(
Expression.Assign(Expression.Field(target, "maxWidth"), Expression.Constant(-1)),
Expression.Call(target, "UpdateHorizontalExtent", null)
);
var lambda = Expression.Lambda<Action<ListBox>>(body, target);
return lambda.Compile();
}
}
}
listBox.UpdateHorizontalScrollbar();