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. Add control dynamically

Add control dynamically

Scheduled Pinned Locked Moved C#
csharptutorialquestion
7 Posts 3 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.
  • T Offline
    T Offline
    thongkk
    wrote on last edited by
    #1

    How to programmatically add a new control in Window Application in C# language?

    J C 2 Replies Last reply
    0
    • T thongkk

      How to programmatically add a new control in Window Application in C# language?

      J Offline
      J Offline
      James T Johnson
      wrote on last edited by
      #2

      MyControl c = new MyControl();

      // set the properties for the control

      this.Controls.Add(c);

      Simple :) I assume that this code is being executed in the forms class. This is also how you add a control to a windows form normally. James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972

      T 1 Reply Last reply
      0
      • T thongkk

        How to programmatically add a new control in Window Application in C# language?

        C Offline
        C Offline
        Christopher Lord
        wrote on last edited by
        #3

        I would guess Container.Controls.Add(new Control()); without having tested it yet *woops, didnt notice the earlier post agreeing.

        1 Reply Last reply
        0
        • J James T Johnson

          MyControl c = new MyControl();

          // set the properties for the control

          this.Controls.Add(c);

          Simple :) I assume that this code is being executed in the forms class. This is also how you add a control to a windows form normally. James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972

          T Offline
          T Offline
          thongkk
          wrote on last edited by
          #4

          Thank you! I can add a new control but i can't set the properties for the control. this is my code: TextBox tb = new TextBox(); this.Controls.Add(tb); this.tb.Location = new System.Drawing.Point(150, 100); this.tb.Multiline = true; this.tb.Name = "textBox2"; this.tb.ReadOnly = false; this.tb.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.tb.Size = new System.Drawing.Size(168, 32); this.tb.TabIndex = 1; this.tb.Text = "textBox1"; this.tb.Visible = false; It gives errors. How to set the properties on the control?

          J 1 Reply Last reply
          0
          • T thongkk

            Thank you! I can add a new control but i can't set the properties for the control. this is my code: TextBox tb = new TextBox(); this.Controls.Add(tb); this.tb.Location = new System.Drawing.Point(150, 100); this.tb.Multiline = true; this.tb.Name = "textBox2"; this.tb.ReadOnly = false; this.tb.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.tb.Size = new System.Drawing.Size(168, 32); this.tb.TabIndex = 1; this.tb.Text = "textBox1"; this.tb.Visible = false; It gives errors. How to set the properties on the control?

            J Offline
            J Offline
            James T Johnson
            wrote on last edited by
            #5

            What errors are you getting? From what I see that code is fine; though I don't think it matters can you try psetting the properties before you do the 'this.Controls.Add(tb)'? James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972

            T 1 Reply Last reply
            0
            • J James T Johnson

              What errors are you getting? From what I see that code is fine; though I don't think it matters can you try psetting the properties before you do the 'this.Controls.Add(tb)'? James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972

              T Offline
              T Offline
              thongkk
              wrote on last edited by
              #6

              Ok, i got it ! I am not suppose to put 'this.tb.Location(....)' but this: 'tb.Location(....)' ok... Thank you very much!! thongkk

              J 1 Reply Last reply
              0
              • T thongkk

                Ok, i got it ! I am not suppose to put 'this.tb.Location(....)' but this: 'tb.Location(....)' ok... Thank you very much!! thongkk

                J Offline
                J Offline
                James T Johnson
                wrote on last edited by
                #7

                lol, I can't believe I missed that! At least you have it figured out now :-) James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972

                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