当文本框获得焦点时选择文本框的内容
我在 让 WinForms TextBox 表现出类似的问题就像你的浏览器的地址栏
现在我试图通过使其通用来修改或使其更加不同。我想对表单中的所有文本框应用相同的操作,而不需要为每个文本框编写代码......我不知道有多少。一旦我在表单中添加文本框,它应该具有类似的选择操作。
所以想知道该怎么做?
I have found a similar question to mine in Making a WinForms TextBox behave like your browser's address bar
Now i am trying to modify or make it some more different by making it general. I want to apply same action to all the textboxes in form without write code for each one... how many i dun know. As soon as i add a textbox in my form it should behave with similar action of selecting.
So wondering how to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下代码继承自 TextBox 并实现您在 中提到的代码WinForms TextBox 的行为类似于浏览器的地址栏。
将 MyTextBox 类添加到项目后,您可以全局搜索 System.Windows.Forms.Text 并替换为 MyTextBox。
使用此类的优点是您不会忘记连接每个文本框的所有事件。此外,如果您决定对所有文本框进行另一项调整,您可以在一个地方添加该功能。
The following code inherits from TextBox and implements the code you mentioned in Making a WinForms TextBox behave like your browser's address bar.
Once you've added the MyTextBox class to your project you can do a global search for System.Windows.Forms.Text and replace with MyTextBox.
The advantage of using this class is you can't forget to wire all the events for every textbox. Also if you decide on another tweak for all textboxes you have one place to add the feature.
假设您要使用链接到的问题中已接受的解决方案,您所需要做的就是每当创建新文本框时,使用 AddHandler 将相同的 3 个事件处理程序添加到每个新文本框。
然后,您需要更改事件处理程序,而不是将文本框引用为
this.textBox1
,而是将其引用为CType(sender, TextBox)
这意味着它们将使用生成事件的文本框。编辑:我也会在此处添加该行代码,因为这样更容易阅读
Assuming you're going to use the accepted solution from the question you link to, all you'd need to do would be that whenever you create a new textbox, you use AddHandler to add the same 3 eventhandlers to each new textbox.
Then you need to change the event handlers to instead of referencing the textbox as
this.textBox1
they'll reference it asCType(sender, TextBox)
which means that they'll use the textbox that generated the event.Edit: I'll add that line of code here as well since it's easier to read then
我们使用这个自定义文本框控件:
您可以在 GitHub https:// /github.com/logico-dev/TextBoxX
We use this custom textbox control:
You can see the full project of our TextBox (on steroids) on GitHub https://github.com/logico-dev/TextBoxX