【发布时间】:2009-07-03 19:28:08
【问题描述】:
这是一个 WinForms 问题。
在一个具有 SelectionMode = MultiSimple 的 ListBox 中,如何获取当前焦点项?
注意,我不想获取 SelectedItem 或 SelectedItems,而是当前周围有虚线的项目,例如 ListView.FocusedItem。
【问题讨论】:
这是一个 WinForms 问题。
在一个具有 SelectionMode = MultiSimple 的 ListBox 中,如何获取当前焦点项?
注意,我不想获取 SelectedItem 或 SelectedItems,而是当前周围有虚线的项目,例如 ListView.FocusedItem。
【问题讨论】:
这有点hacky,但我还没有找到更好的解决方案。
捕获 DrawItem 事件并将焦点索引保存在字段上
if (e.State == DrawItemState.Focus) {
myfocus = e.Index;
}
// Draw the background of the ListBox control for each item.
e.DrawBackground();
// Define the default color of the brush as black.
if (brochas.Count != colores.Count) {
ProcesarBrochas();
}
// Draw the current item text based on the current Font
// and the custom brush settings.
if (Items.Count > e.Index) {
e.Graphics.DrawString(Items[e.Index].ToString(),
e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
}
// If the ListBox has focus, draw a focus rectangle around the selected item.
e.DrawFocusRectangle();
使用 myFocus 变量
【讨论】:
我认为默认情况下没有一个 - 用户控件可能是您唯一的选择。
您可能需要重新考虑自己在做什么 - 为什么您需要专注的对象而不是选定的对象?可能有不同的方法。
【讨论】:
这不是完美的解决方案,但一种解决方法可能是在触发模糊事件时将selectedItem 存储到“focusedItem”中,然后在需要时简单地检索它。
【讨论】: