Detect changes in Label
I've got a Form with some labels on it. From time to time the开发者_如何转开发 program changes the text on the labels with
label1.Text = "some message"
I want to create a function that is executed every time the label text is assigned and implemented an event handler like this:
this.label1.TextChanged += new System.EventHandler(this.label1_TextChanged);
[...]
private void label1_TextChanged(object sender, EventArgs e) {
// do some stuff
}
This works fine as long as the text is really changed. But if the text is set to the same value it already has, the event doesn't execute. Is there an easy way to hook a function into the text assignment without changing every occurence of label1.Text = ...
to a custom function call?
The Control's Text property is virtual so you can create your own label control and add custom functionality there, such as raising an event when the property's setter is called even if it doesn't result in changed text.
Set the label text via property every time program set its value it will call that function that u will define in property.
like
public string LabelText
{
set
{
label1.Text = value;
// your Function Calling
}
}
精彩评论