簡體   English   中英

如何使用Tab在winform屬性網格的屬性之間移動

[英]How do I use Tab to move between properties of winform property grid

我在我的項目中使用Winform的PropertyGrid ,它一切正常但是Tab鍵順序。

當我點擊Tab時我想切換到下一個屬性,但事實上,選擇移出屬性網格到下一個控件。 我無法弄清楚如何完成這項工作?

謝謝

我們應該深入研究PropertyGrid內部部分,然后我們可以更改控件的默認Tab行為。 在開始時,我們應該創建一個派生的PropertyGrid並覆蓋其ProcessTabKey方法。

在該方法中,首先找到Controls集合中索引2處的內部PropertyGridView Controls 然后使用Reflection獲取其allGridEntries字段,該字段是包含所有GridItem元素的集合。

找到所有網格項后,在集合中找到SelectedGridItem的索引,並檢查它是否不是最后一項,按索引獲取下一項,然后使用項的Select方法選擇它。

using System.Collections;
using System.Linq;
using System.Windows.Forms;
public class ExPropertyGrid : PropertyGrid
{
    protected override bool ProcessTabKey(bool forward)
    {
        var grid = this.Controls[2];
        var field = grid.GetType().GetField("allGridEntries",
            System.Reflection.BindingFlags.NonPublic |
            System.Reflection.BindingFlags.Instance);
        var entries = (field.GetValue(grid) as IEnumerable).Cast<GridItem>().ToList();
        var index = entries.IndexOf(this.SelectedGridItem);

        if (forward && index < entries.Count - 1)
        {
            var next = entries[index + 1];
            next.Select();
            return true;
        }
        return base.ProcessTabKey(forward);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM