使用winforms app中的默认文本关注文本框。 [英] Focus on textbox with its default text in winforms app.

查看:68
本文介绍了使用winforms app中的默认文本关注文本框。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望专注于文本框,当它获得焦点时,默认文本不应被移除,直到用户输入数据为止。我可以这样做吗?

i有一个textbox属性设置为first name的文本框。当此文本框被聚焦时,在用户输入其名称之前,不应删除默认文本first name。

i want to get a focus on a textbox,when it get focus the default text shouldn't be removed until the user enter the data in it.how can i do this?
i have a textbox whose text property is set to "first name".when this textbox is focused then the default text which is "first name"shouldn't be removed until user enter his name.

推荐答案

在Winforms中,挂钩GotFocus事件。在XAML中,创建一个行为并将其附加到GotFocus事件。无论哪种方式,该函数都应该将SelectedPosition属性设置为0(或1 ...我不记得哪个离开我的头顶)和SelectedLength属性设置为text属性的长度。这将在方法触发时选择文本框中的文本。



将来,请包括您正在使用的技术等重要细节。 C#的问题并不是很多。
In Winforms, hook the GotFocus event. In XAML, create a behavior and attach it to the GotFocus event. Either way, the function should set the "SelectedPosition" property to 0 (or 1... I can't remember which off the top of my head) and the "SelectedLength" property to the length of the text property. That will select the text in the text box when the method is fired.

In the future, please include the important details like which technology you are working in. Tagging the question with C# isn't a whole lot to go on.


你不能阻止用户将焦点从你的任何控件移开。即使你可以,这对用户来说也是一种极大的恶意行为,他们完全有权集中整个系统的控制权;如果你做过这样的技巧,没有人会再次使用你的项目。



相反,你可以捕捉失去焦点的事件(忽略焦点丢失的情况)激活其他窗口,也许是其他应用程序的窗口),比如显示一些错误情况(数据不完整或无效)。这就是:例如:

You cannot prevent the user from moving focus away from any of your control. Even if you could, it would be a great malicious act against the user, who has every right to focus whatever control in the whole system; nobody would use your projects again if you had done such tricks.

Instead, you can capture the event of loosing the focus (ignoring the cases when the focus is lost due to activation of other windows, perhaps the windows of other applications) and, say, show some error condition (incomplete or invalid data). This is how, for example:
myEditBox.LostFocus += (sender, eventArgs) => { /* do something about it */ };



请参阅: http: //msdn.microsoft.com/en-us/library/system.windows.forms.control.lostfocus(v=vs.110).aspx [ ^ ]。



-SA


您无法阻止用户将焦点移离任何控件。即使你可以,这对用户来说也是一种极大的恶意行为,他们完全有权集中整个系统的控制权;如果你做过这样的技巧,没有人会再次使用你的项目。



相反,你可以捕捉失去焦点的事件(忽略焦点丢失的情况)激活其他窗口,也许是其他应用程序的窗口),比如显示一些错误情况(数据不完整或无效)。这就是:例如:

You cannot prevent the user from moving focus away from any of your controls. Even if you could, it would be a great malicious act against the user, who has every right to focus whatever control in the whole system; nobody would use your projects again if you had done such tricks.

Instead, you can capture the event of loosing the focus (ignoring the cases when the focus is lost due to activation of other windows, perhaps the windows of other applications) and, say, show some error condition (incomplete or invalid data). This is how, for example:
myEditBox.LostFocus += (sender, eventArgs) => { /* do something about it */ };



请参阅: http: //msdn.microsoft.com/en-us/library/system.windows.forms.control.lostfocus(v=vs.110).aspx [ ^ ]。



-SA


这篇关于使用winforms app中的默认文本关注文本框。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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