【问题标题】:ListBox Focused Item列表框重点项目
【发布时间】:2009-07-03 19:28:08
【问题描述】:

这是一个 WinForms 问题。

在一个具有 SelectionMode = MultiSimple 的 ListBox 中,如何获取当前焦点项?

注意,我不想获取 SelectedItem 或 SelectedItems,而是当前周围有虚线的项目,例如 ListView.FocusedItem。

【问题讨论】:

    标签: .net winforms controls


    【解决方案1】:

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

    1. 将 ListBox.DrawMode 放在 OwnerDrawFixed 上
    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 变量

    【讨论】:

      【解决方案2】:

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

      您可能需要重新考虑自己在做什么 - 为什么您需要专注的对象而不是选定的对象?可能有不同的方法。

      【讨论】:

      • 当用户“关注”第一项并按下“向上箭头”键时,我想要将焦点设置在其他控件上
      【解决方案3】:

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

      【讨论】:

      • ListBox控件上没有找到blur事件,在哪里?
      猜你喜欢
      • 1970-01-01
      • 2011-03-10
      • 1970-01-01
      • 1970-01-01
      • 2011-07-23
      • 2012-05-20
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      相关资源
      最近更新 更多