Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Creating dynamic controls on a form

Creating dynamic controls on a form

Scheduled Pinned Locked Moved C#
helpquestiontutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fracalifa
    wrote on last edited by
    #1

    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

    L 1 Reply Last reply
    0
    • F fracalifa

      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

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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.


      F 1 Reply Last reply
      0
      • L Luc Pattyn

        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.


        F Offline
        F Offline
        fracalifa
        wrote on last edited by
        #3

        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

        L 1 Reply Last reply
        0
        • F fracalifa

          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

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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.


          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups