Dynamically cast a control type in runtime
Posted
by
JayT
on Stack Overflow
See other posts from Stack Overflow
or by JayT
Published on 2010-12-29T12:46:07Z
Indexed on
2010/12/29
12:53 UTC
Read the original article
Hit count: 240
c#
Hello,
I have an application whereby I dynamically create controls on a form from a database. This works well, but my problem is the following:
private Type activeControlType;
private void addControl(ContainerControl inputControl, string ControlName, string Namespace,
string ControlDisplayText, DataRow drow, string cntrlName)
{
Assembly assem;
Type myType = Type.GetType(ControlName + ", " + Namespace);
assem = Assembly.GetAssembly(myType);
Type controlType = assem.GetType(ControlName);
object obj = Activator.CreateInstance(controlType);
Control tb = (Control)obj;
tb.Click += new EventHandler(Cntrl_Click);
inputControl.Controls.Add(tb);
activeControlType = controlType;
}
private void Cntrl_Click(object sender, EventArgs e)
{
string test = ((activeControlType)sender).Text; //Problem ???
}
How do I dynamically cast the sender object to a class that I can reference the property fields of it.
I have googled, and found myself trying everything I have come across..... Now I am extremely confused... and in need of some help
Thnx
JT
© Stack Overflow or respective owner