如何处理文本框的加载事件 [英] How to handle textbox's load event

查看:119
本文介绍了如何处理文本框的加载事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含许多定制TextBox的表单(我们称它们为InheritedTextBox).现在,我需要基于文本框的数据绑定属性设置MaxLength属性.该属性本身具有我需要阅读的MaxLength属性(这是微不足道的).

问题在于TextBox控件没有Load事件. 所以在加载表单时,我应该在哪里调用设置MaxLength属性的代码?我尝试了文本框的构造函数(尚未设置数据绑定)和TextBox.DataBindings.CollectionChanged事件(直到处理完文本框后才被调用).

简而言之:
-我将文本框绑定到设计器中的数据源(例如,Order实体及其属性OrderID)
-OrderID属性具有自定义属性MaxLength(30)
-我希望在表单加载时在绑定的文本框的MaxLength上设置该属性的值

当实例化文本框并且没有Load事件时,绑定不存在.那该怎么办?


有任何建议吗?

I have a form with lots of custom-made TextBoxes (let''s call them InheritedTextBox). Now I need to set the MaxLength property based on the textbox''s data binded property. The property itself has a MaxLength attribute that I need to read (that''s trivial).

The problem is that the TextBox control doesn''t have a Load event. So where should I call the code that sets the MaxLength property when the form is loaded? I''ve tried the textbox''s constructor (no databindings set yet) and the TextBox.DataBindings.CollectionChanged event (doesn''t get called until the textbox is disposed).

So in a nutshell:
- I bind a textbox into a datasource in designer (for example an Order entity and its property OrderID)
- The OrderID property has custom attribute MaxLength(30)
- I want that attribute''s value to be set on the bound textbox''s MaxLength when the form loads up

The binding don''t exist when the textbox is being instantiated and there''s no Load event. So what to do?


Any suggestions?

推荐答案

没有这样的事件;没有加载文本框的概念.有一个只读属性System.Windows.Forms.TextBox.Text.此属性的设置器将触发事件System.Windows.Forms.TextBox.TextChanged.使用它-这就是您所需要的.

-SA
There is no such event; there is no a concept of loading of the text box. There is a read-only property System.Windows.Forms.TextBox.Text. The setter of this property fires the event System.Windows.Forms.TextBox.TextChanged. Use it — this is what you need.

—SA


表单具有Loaded事件.您可以使用该处理程序并枚举控件,如果找到文本框,请执行所需的操作.
The Form has a Loaded event. You can use that handler and enumerate through the controls and if you find a textbox, do whatever you need to do.


这篇关于如何处理文本框的加载事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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