簡體   English   中英

如何在 C# 中使用箭頭禁用 WinForm 上的導航?

[英]How to disable navigation on WinForm with arrows in C#?

我需要禁用使用表單上的箭頭更改焦點。 有沒有簡單的方法來做到這一點?

謝謝

類似的東西:

    private void Form1_Load(object sender, EventArgs e)
    {
        foreach (Control control in this.Controls)
        {
            control.PreviewKeyDown += new PreviewKeyDownEventHandler(control_PreviewKeyDown);
        }
    }

    void control_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
        if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)
        {
            e.IsInputKey = true;
        }
    }

我已經結束了下面的代碼,該代碼將功能設置為表單上的每個控件:

(該代碼基於來自andynormancx 的代碼)



private void Form1_Load(object sender, EventArgs e)
{
    SetFeatureToAllControls(this.Controls);    
}

private void SetFeatureToAllControls(Control.ControlCollection cc)
{
    if (cc != null)
    {
        foreach (Control control in cc)
        {
            control.PreviewKeyDown += new PreviewKeyDownEventHandler(control_PreviewKeyDown);
            SetFeatureToAllControls(control.Controls);
        }
    }
}

void control_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)
    {
        e.IsInputKey = true;
    }
}

您應該在表單上將KeyPreview設置為 true。 處理KeyDown/KeyUp/KeyPress事件並將事件處理程序中的e.Handled設置為 true 以用於要忽略的鍵。

我試過這種方法,表單處理一次預覽事件。 它生成的代碼比其他選項少。

只需將此方法添加到表單的PreviewKeyDown事件中,並將KeyPreview屬性設置為true

private void form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    switch (e.KeyCode)
    {
        case Keys.Up:
        case Keys.Down:
        case Keys.Left:
        case Keys.Right:
            e.IsInputKey = true;
            break;
        default:
            break;
    }
}

暫無
暫無

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

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