所以我不确定为什么会这样,但我正在运行一些DataRows,其中我有我想要设置的控件名称,属性和值。一切正常,除非我设置按钮的TEXT属性。出于某种原因,点击事件被称为......
以下是我得到的一些代码:
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;
}
}
}
那么为什么世界上它会在设置值时调用事件处理程序?以下是我设置它的原因:
<SnappletChangePassword>
<ControlName>buttonAcceptPassword</ControlName>
<Property>Text</Property>
<Value>Accept</Value>
</SnappletChangePassword>
答案 0 :(得分:3)
我无法通过简单但完整的简短程序重现这一点:
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);
}
}
你能想出一个做演示的同样完整的程序吗?
答案 1 :(得分:0)
不确定它是什么,但感谢代码。
你没有在.Net2.0中写过这个吗?