任何方法可以阻止文本框在表单加载时获得自动对焦 [英] Any way to stop textbox gaining autofocus on form load

查看:166
本文介绍了任何方法可以阻止文本框在表单加载时获得自动对焦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Visual Basic中创建了一个表单,并在其中创建了一个文本框。每当我启动表单[debug it:p](按F5)时,文本框会自动获得焦点。我不希望它获得焦点,任何方式做到这一点?

解决方案

设置Tab键顺序,以便可以接受用户输入焦点的不同控件具有较低的数字:OK按钮,例如。





它只能用于按钮和文本框,RTF框!我不需要它,但有没有办法用图片框或标签的东西...只是好奇:)







:笑:不!想一想 - 这是键盘输入:所以它会跳过所有无法输入的控件(比如标签等)。否则你可能有

标签TextBox 
用户名:..........



并且焦点将转到标签而不是文本框。并且您希望标签的标签顺序值比匹配的文本框小一个,因此&处理工作也是如此。即如果上例中的标签文字是:

& Username:

然后它显示为用户名,但按住ALT并U带下划线,因此ALT + U将焦点移动到标签后面的文本框。


Focus始终表示键盘焦点。您可以专注于其他内容,其他一些控件允许用户输入。在激活表单时没有键盘焦点根本没有意义。



此外,最好将焦点设置为您选择的某个控件仅一次,第一次显示表单时。在所有其他时间,应该是用户的选择而不是其他 System.Windows.Forms 库提供了一种机制,用于在之前聚焦的控件上设置键盘焦点。你永远不应该转移这个焦点:用户这样做。请参阅:

http:// msdn .microsoft.com / zh-cn / library / system.windows.forms.containercontrol.activecontrol.aspx [ ^ ]。



-SA

I have created a form in Visual basic and a textbox into it. Whenever i start the form[debug it :p] (press F5) the textbox automatically gains focus. I dont want it to gain focus, any way to do that?

解决方案

Set the tab order so that a different control that can accept the user input focus has a lower number: the OK button, for example.


"It works but only with buttons and textbox,RTF boxes ones ! i dont need it but is there any way to work it with picture box or labels something... Just curious :)
"



:laugh: No! Think about it - it's the keyboard input: so it skips all controls that can't take input, (like labels and so forth). Otherwise you could have

Label     TextBox
Username: ..........


and the focus would go to the label instead of the text box. And you want the label to have a tab order value one less than the matching textbox, so that "&" processing works as well. I.e. if the label text in the above example is :

&Username:

Then it displays as "Username", but hold down ALT and the "U" is underlined, so ALT+U moves focus to the Textbox that follows the label.


"Focus" always means "keyboard focus". You can have focus on something else, some other control which allows user input. Not having keyboard focus when a form gets activated makes no sense at all.

Besides, it's the best to set focus to some control of your choice only once, when the form is shown for the very first time. At all other times, it should be user's choice and nothing else. The System.Windows.Forms library provides a mechanism for setting a keyboard focus on the control which was focused before. You should never shift this focus: a user does it. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.containercontrol.activecontrol.aspx[^].

—SA


这篇关于任何方法可以阻止文本框在表单加载时获得自动对焦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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