键盘的向上和向下按钮如何控制焦点的顺序? [英] How does the keyboard's up and down buttons control the order of focus?

查看:84
本文介绍了键盘的向上和向下按钮如何控制焦点的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个关于向上按钮和向下按钮的问题,以控制焦点。

例如,有一个窗口Form1包含listbox和button1。

在button1单击事件Form2中。弹出

1.,只需运行程序,运行Form1,然后这次窗口集中在列表框上

2.,如果我按下button1,弹出Form2然后关闭,窗口的焦点在按钮1上。

3.此时,我想按下键盘向上或向下按钮,这样当Form2关闭时,焦点是立即在列表框上。我该怎么办?

因为我发现了Tab键序列的设置,Tab键设置似乎与键盘的向上和向下按钮无关,尽管它们都可以控制焦点。 br />
那么,我在哪里可以设置向上和向下按钮的焦点顺序,以及如何编程?



我尝试了什么:



i试图设置tab键,并使用keydown事件来处理焦点。



I ran into a problem, about the up button and the down button, to control the focus.
For example, there is a window Form1 that contains listbox and button1.
Inside the button1 click event, Form2. pops up
1., just run the program, is running Form1, then this time the window focus on the listbox
2., if I press button1, pop up Form2 and then close, the focus of the window is on the button1.
3. at this point, I want to press the keyboard up or down button so that when the Form2 is turned off, the focus is immediately on the listbox. What should I do?
Because I found that the settings of the tab key sequence, the Tab key setting seems to be unrelated to the keyboard's up and down buttons, although they can all control the focus.
So, where can I set the focus order for up and down buttons, and how to program?

What I have tried:

i tried to set the tab key,and use keydown event to handle focuse.

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    switch (e.KeyCode)
    {
        case Keys.Up:
            this.listboxPoints.Focus();
            break;
        case Keys.Down:
            this.listboxPoints.Focus();
            break;
        default:
            break;
    }
}

推荐答案

假设这是一个Windows窗体应用程序:



0.向上和向下箭头键不会移动焦点:如果你让它们这样做,你就不会遵循Win UI指南...不应该做的事情除非你有引人注目的原因。


1.箭头键'向下事件触发一个Form的KeyDown EventHandler 如果Form KeyPreView属性设置为'true。



2.如果您想基于箭头键执行操作,最好的方法是覆盖'ProcessCmdKey在表单中:
Assuming this is a Windows Form application:

0. up- and down- arrow keys do not move focus: if you make them do that, you are not following the Win UI guidelines ... something that should not be done unless you have a compelling reason.

1. Arrow-key 'down events trigger a Form's KeyDown EventHandler only if the Form KeyPreView property is set to 'true.

2. if you want to take action based on arrow-keys, the best way is to override 'ProcessCmdKey in the Form:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    switch (keyData)
    {
        case Keys.Up:
            if(OnUp != null) if (OnUp()) return true;
            break;
        case Keys.Down:
            break;
        case Keys.Right:
            break;
        case Keys.Left:
            break;
    }

    return base.ProcessCmdKey(ref msg, keyData);
}

public Func<bool> OnUp = () =>
{
    Console.WriteLine("up");
    return true;
};

在此示例中,截取了向上箭头键,如果定义了函数'OnUp,则调用该函数;如果对函数的调用返回'true,则ProcessCmdKey不会将键击传递给任何其他键处理程序。



为什么使用ProcessCmdKey,何时可以使用将KeyPreView设置为'true KeyDown处理程序以处理箭头kwys的表单?一个判断问题,但是,请记住,您正在创建一个可重用的组件,其他开发人员可能正在安装关键事件处理程序。

In this example the up-arrow key is intercepted, and if a function 'OnUp is defined, the function is called; if the call to the function returns 'true, then ProcessCmdKey does not pass along the key-stroke to any other Key Handlers.

Why use ProcessCmdKey, when you could use a Form with KeyPreView set to 'true KeyDown handler to process the arrow kwys ? A matter of judgement, but, keep in mind that you are creating a re-usable component, other developers may be installing key event handlers.


这篇关于键盘的向上和向下按钮如何控制焦点的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆