cod*_*ife 4 c# listbox listboxitem winforms
反正只有一个项目无效ListBox吗?似乎没有获取项目矩形的方法,或者即使特定项目可见(除了调用IndexFromPoint控件中的每个像素/至少一列中的每个像素).
这适用于C#WinForms(不是WPF).
有关我要做的事情的更多信息:
我有一个ListBox包含很多项目的东西,我希望在你悬停的项目上出现一组"按钮"(例如红色的X用于删除).我有这个工作很好,除了在10个或更多项目的列表上,每次你将鼠标悬停在一个新项目上它会导致可见的重绘,因为我使整个控件无效.数据不会更改,只会更改显示.
编辑:更多信息和以前的尝试
我已经子类化ListBox并执行我的绘图,OnDrawItem因此ListBox可以使用受保护的方法.
我尝试了以下不同程度的成功.变量this是扩展的ListBox,index是要绘制old_index的项目,是先前被绘制的项目.
// Causes flicker of entire list, but works
this.Invalidate();
// Causes flicker of selected item, but works
int sel_index = this.SelectedIndex;
this.SelectedIndex = old_index;
this.SelectedIndex = index;
this.SelectedIndex = sel_index;
// Does not work
if (old_index >= 0)
this.RefreshItem(old_index);
if (index >= 0)
this.RefreshItem(index);
Run Code Online (Sandbox Code Playgroud)
好吧,我很傻.感谢@LarsTech我决定再次查看整个ListBox功能列表并找到合适的功能:GetItemRectangle它甚至是公开的!我不知道在过去一小时里我是多么想念这个...
工作解决方案是:
if (old_index >= 0)
this.Invalidate(this.GetItemRectangle(old_index));
if (index >= 0)
this.Invalidate(this.GetItemRectangle(index));
Run Code Online (Sandbox Code Playgroud)
这仍然会产生一点点闪烁,但显着更少(只有当我在移动单个项目之前非常快速地鼠标移动时才能看到它).
| 归档时间: |
|
| 查看次数: |
2650 次 |
| 最近记录: |