How to Add Controls dynamically ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Crystallugia

    How to Add Controls dynamically ?

    Hi,

    I'm new to C#. I want to add a TabPage dynamically (name depends on data from database). After receiving the data the TabPage tab should be added to the TabControl ( this.Services) but nothing happens. Putting the code somewhere else (without the database dependency) doensn't work either. Even adding items to a ListBox dynamicly doesn't work. Can someone tell me what I have to do to add / create Controls dynamically at runtime?


    Code:
     TabPage tab = new System.Windows.Forms.TabPage();
                        tab.Location = new System.Drawing.Point(4, 22);
                        tab.Name = reader.GetString(0);
                        tab.Padding = new System.Windows.Forms.Padding(3);
                        tab.Size = new System.Drawing.Size(670, 335);
                        tab.TabIndex = 3;// this.Services.Controls.Count + 1;
                        tab.Text = reader.GetString(0);
                        tab.UseVisualStyleBackColor = true;
                        this.Services.Controls.Add(tab);
    thanks,

    Crystallugia
  • Michael OC
    New Member
    • Oct 2010
    • 4

    #2
    Try this post.

    Windows TabControl is a useful control that allows you display multiple dialogs tabs on a single form by switching between the tabs. In this tutorial, I will explain how to create and use a TabControl in your Windows applications with C#.

    Comment

    • GaryTexmo
      Recognized Expert Top Contributor
      • Jul 2009
      • 1501

      #3
      You need to add it to the TabPages for a TabControl, not the Controls. In your case...

      Code:
      TabPage tab = new System.Windows.Forms.TabPage();
      tab.Location = new System.Drawing.Point(4, 22);
      tab.Name = reader.GetString(0);
      tab.Padding = new System.Windows.Forms.Padding(3);
      tab.Size = new System.Drawing.Size(670, 335);
      tab.TabIndex = 3;// this.Services.Controls.Count + 1;
      tab.Text = reader.GetString(0);
      tab.UseVisualStyleBackColor = true;
      [b]//this.Services.Controls.Add(tab);
      this.Services.TabPages.Add(tab);[/b]

      Comment

      Working...