我有一個窗體上的控件,我使用程序集在運行時獲取它的對象。現在我想在運行時改變它們的屬性,如forecolor,backcolor和text。在運行時自定義控件
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;
}
該代碼在運行時讓我獲得對象,但是當我嘗試更改forecolor時,它會給我一個錯誤。誰能幫我?
什麼錯誤呢它給你? –
我想在運行時更改控件的屬性。 – Gagan
@Gapan在這裏發佈您遇到的錯誤。沒有人知道問題,沒有人不能幫助你。 –