Creating dynamic controls on a form
-
Hi all, I want to create controls dynamicly on a form with the properties comming from a DataTable. (see code below) The Table contains the infos like "name", "typ", "location", "size", ... of each control to create.
...
adapter.SelectCommand.CommandText = "SELECT * FROM controls WHERE controls_ID = 123";
adapter.SelectCommand.Connection = conn;
adapter.SelectCommand.CommandTimeout = 60;DS = new DataSet();
adapter.Fill(DS,"Ctrl");
System.Windows.Forms.Control[] X=null;Table = DS.Tables["Ctrl"];
if(Table.Rows.Count>0)
{
this.SuspendLayout();
for(int i=0; i<Table.Rows.Count; i++)
{
DataRow row = Table.Rows[i];
Type typ = Type.GetType(row["typ"].ToString()); // can be TextBox, Label,
typ X[i] = new typ(); // <-----here is the problem ------ how to assign/create the typ of control ?
X[i].Text = row["text"].ToString();
X[i].Name = row["name"].ToString();
this.Controls.Add(X[i]);
}
this.ResumeLayout();
}
...how can I assign or create the right type of control. (see the marked row) Is there an other way to do that ? Thank you for your help frank
-
Hi all, I want to create controls dynamicly on a form with the properties comming from a DataTable. (see code below) The Table contains the infos like "name", "typ", "location", "size", ... of each control to create.
...
adapter.SelectCommand.CommandText = "SELECT * FROM controls WHERE controls_ID = 123";
adapter.SelectCommand.Connection = conn;
adapter.SelectCommand.CommandTimeout = 60;DS = new DataSet();
adapter.Fill(DS,"Ctrl");
System.Windows.Forms.Control[] X=null;Table = DS.Tables["Ctrl"];
if(Table.Rows.Count>0)
{
this.SuspendLayout();
for(int i=0; i<Table.Rows.Count; i++)
{
DataRow row = Table.Rows[i];
Type typ = Type.GetType(row["typ"].ToString()); // can be TextBox, Label,
typ X[i] = new typ(); // <-----here is the problem ------ how to assign/create the typ of control ?
X[i].Text = row["text"].ToString();
X[i].Name = row["name"].ToString();
this.Controls.Add(X[i]);
}
this.ResumeLayout();
}
...how can I assign or create the right type of control. (see the marked row) Is there an other way to do that ? Thank you for your help frank
Hi, there are at least two ways to do it: 1. for a limited number of types, provide specific code for each type, maybe inside a switch statement; simple but not really elegant. 2. use reflection, have a look at Activator.CreateInstance() :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
Hi, there are at least two ways to do it: 1. for a limited number of types, provide specific code for each type, maybe inside a switch statement; simple but not really elegant. 2. use reflection, have a look at Activator.CreateInstance() :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
Hi Luc, tnx for your answer. Your first way you have mentioned is a way I can follow for that task but, for the future the second solution might be more interesting. Do you have a link to an example for the second way ? txn in advance frank
No but google will have plenty. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.