System.Reflection.PropertyInfo.SetValue () calls the default event handler for the button
So I'm not really sure why this is happening, but I'm running some DataRows where I have the name, property, and value of the control that I want to set. Everything works fine except when I set the TEXT property of the button. For some reason, the click event gets called ...
Here is the code I have:
string controlName, value, property;
Control currentControl = null;
System.Reflection.PropertyInfo propertyInfo = null;
// run through all rows in the table and set the property
foreach (DataRow r in languageDataset.Tables[_parentForm.Name].Rows)
{
controlName = r["ControlName"].ToString().ToUpper();
value = r["Value"].ToString();
property = r["Property"].ToString();
// check all controls on the form
foreach (Control c in formControls)
{
// only change it if its the right control
if (c.Name.ToUpper() == controlName)
{
propertyInfo = c.GetType().GetProperty(property);
if (propertyInfo != null)
propertyInfo.SetValue(c, value, null); ******Calls Event Handler?!?!******
//
currentControl = c;
break;
}
}
}
So why in the world could it call an event handler when setting a value? Here's what I am setting with what is causing this:
<SnappletChangePassword>
<ControlName>buttonAcceptPassword</ControlName>
<Property>Text</Property>
<Value>Accept</Value>
</SnappletChangePassword>
0
source to share
2 answers
I cannot reproduce this with a simple short but complete program:
using System;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
class Test
{
static void Main()
{
Button goButton = new Button {
Text = "Go!",
Location = new Point(5, 5)
};
Button targetButton = new Button {
Text = "Target",
Location = new Point(5, 50)
};
goButton.Click += (sender, args) => SetProperty(targetButton, "Text", "Changed");
targetButton.Click += (sender, args) => MessageBox.Show("Target clicked!");
Form f = new Form { Width = 200, Height = 120,
Controls = { goButton, targetButton }
};
Application.Run(f);
}
private static void SetProperty(object target, string propertyName, object value)
{
PropertyInfo property = target.GetType().GetProperty(propertyName);
property.SetValue(target, value, null);
}
}
Can you find a similar program that demonstrates this?
+3
source to share