Winform应用程序中的焦点设置 [英] focus setting in winform application

查看:205
本文介绍了Winform应用程序中的焦点设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
我想把重点放在形式上.
意味着在我的应用程序中,当我运行此应用程序时,某些标签,文本框和复选框会出现,然后焦点位于文本框内.但是我想将焦点放在窗体中,所以我可以将焦点设置为窗体(如果是),然后告诉我该怎么做.因为我想在启动应用程序时使用表单的按下事件

hello
I want to set focus to form.
means in my application some label, text box and check box when i run this application then focus are in text box.but i want focus in form so i can set focus to form if yes then tell me the way for that. because i want to use form''s key down event when i start my application

推荐答案

您无法将焦点设置为所需的表单-它没有任何用户可编辑的部分-焦点转到了文本框,因为它是Tab键顺序中第一个可以接受用户输入的控件.

如果要处理表单中所有控件以及表单本身的KeyDown事件,则必须在设计器或表单Load事件中向每个控件添加处理程序:
You can''t set the focus to the form in the way you want - it doesn''t have any user editable parts - the focus goes to the textbox because it is the first control in the Tab Order which can accept user input.

If you want to handle the KeyDown event for all the controls in your form, as well as teh form itself, then you have to add the handler to each, either in the designer, or in the form Load event:
private void myForm_Load(object sender, EventArgs e)
    {
    foreach (Control c in Controls)
        {
        c.KeyDown += new KeyEventHandler(myForm_KeyDown);
        }
    }

void myForm_KeyDown(object sender, KeyEventArgs e)
    {
    ...
    }


您可以将表单的KeyPreview属性设置为True

来自MSDN:
You can set the KeyPreview property of your form to True

From MSDN:
When this property is set to true, the form will receive all KeyPress, KeyDown, and KeyUp events. After the form's event handlers have completed processing the keystroke, the keystroke is then assigned to the control with focus. For example, if the KeyPreview property is set to true and the currently selected control is a TextBox, after the keystroke is handled by the event handlers of the form the TextBox control will receive the key that was pressed. To handle keyboard events only at the form level and not allow controls to receive keyboard events, set the KeyPressEventArgs..::.Handled property in your form's KeyPress event handler to true.



例如:



example:

protected override void OnKeyDown( KeyEventArgs e )
{
    // check for Ctrl.X
    if (e.KeyCode == Keys.X && Control.ModifierKeys == Keys.Control)
    {
        // your code to handle Ctrl.X
        
        e.Handled = true; // Decide whether or not focused control shall receive keystroke.
    }
}




问候
Michel




regards
Michel


尝试一下可能会帮助您
Try this may help you
this.Focus();



谢谢
--RA



Thanks
--RA


这篇关于Winform应用程序中的焦点设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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