2009-07-03 74 views
0

这是一个WinForms问题。ListBox重点项目

在SelectionMode = MultiSimple的ListBox中,如何获取当前焦点的项目?

请注意,我不想获得SelectedItem或SelectedItems,但目前有虚线的项目,如ListView.FocusedItem。

回答

1

这是有点hacky,但我还没有找到更好的解决方案。在OwnerDrawFixed

  1. 认沽ListBox.DrawMode
  2. 捕捉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(); 
    
  3. 使用对焦指数myFocus变量

+0

+1很好的解决方法 – 2009-07-13 10:29:50

0

我不认为在默认情况下有一个 - 用户控件可能是您唯一的选择。

你可能想重新考虑你在做什么 - 为什么你需要专注的而不是选择的?可能有不同的做法。

+0

我要的是当用户在第一个项目上“聚焦”并且脉冲“向上箭头”键时将焦点设置在其他控制上 – 2009-07-03 21:41:04

0

这不是一个完美的解决方案,但是一个解决方法可能是在blur事件触发时将selectedItem存储到“focusedItem”中,然后在需要时简单检索它。

+0

我没有鳍d ListBox控件上的模糊事件,它在哪里? – 2009-07-07 12:38:11