【发布时间】:2011-08-06 09:00:19
【问题描述】:
我在表单上有控件,并在运行时使用程序集获取它的对象。现在我想在运行时更改它们的属性,例如前景色、背景色和文本。
private void button1_Click(object sender, EventArgs e)
{
Type formtype = typeof(Form);
foreach(Type type in Assembly.GetExecutingAssembly().GetTypes())
{
if (formtype.IsAssignableFrom(type))
{
listBox1.Items.Add(type.Name);
Form frm = (Form)Activator.CreateInstance(type);
foreach (Control cntrl in frm.Controls)
{
listBox1.Items.Add(cntrl.Name);
}
}
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Control cnt = (Control)listBox1.SelectedItem;
MessageBox.Show(cnt.Name);
cnt.ForeColor = colorDialog1.Color;
}
这段代码在运行时为我获取了对象,但是当我尝试更改前景色时,它给了我一个错误。谁能帮帮我?
【问题讨论】:
-
我想在运行时更改控件的属性。
-
@Gapan 在此处发布您遇到的错误。没有人能在不知道问题的情况下帮助您。
-
它不会产生任何错误但不会在运行时更改控件的属性...