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. How to place user controls dynamically on a windows form

How to place user controls dynamically on a windows form

Scheduled Pinned Locked Moved C#
winformstutorial
8 Posts 7 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.
  • N Offline
    N Offline
    Nekkantidivya
    wrote on last edited by
    #1

    Hi, In my project I have to place the existing user controls dynamically on a form at the given location. That is I will give the X-axis and Y-axis values in the text boxes dynamically then the existing user control should be dynamically created at that location. If any one have any idea to solve this, please reply me as soon as possible. Thanks in advance.

    C D T 3 Replies Last reply
    0
    • N Nekkantidivya

      Hi, In my project I have to place the existing user controls dynamically on a form at the given location. That is I will give the X-axis and Y-axis values in the text boxes dynamically then the existing user control should be dynamically created at that location. If any one have any idea to solve this, please reply me as soon as possible. Thanks in advance.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      This is quite trivial, you should buy a book and read it. The Petzold book is good in this regard. You add the control to the form's controls collection and you set it's position.

      Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )

      1 Reply Last reply
      0
      • N Nekkantidivya

        Hi, In my project I have to place the existing user controls dynamically on a form at the given location. That is I will give the X-axis and Y-axis values in the text boxes dynamically then the existing user control should be dynamically created at that location. If any one have any idea to solve this, please reply me as soon as possible. Thanks in advance.

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        If using 2.0 or above you can use a generic function such as this in your form.

        private void AddControl<T>(string text, Point location, Size size) where T : Control, new()
        {
        T control = new T();
        control.Text = text;
        control.Location = location;
        control.Size = size;
        this.Controls.Add(control);
        }

        and call it like

        AddControl<Button>("New Button", new Point(12, 12), new Size(75, 23));

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
        Why are you using VB6? Do you hate yourself? (Christian Graus)

        M H R 3 Replies Last reply
        0
        • N Nekkantidivya

          Hi, In my project I have to place the existing user controls dynamically on a form at the given location. That is I will give the X-axis and Y-axis values in the text boxes dynamically then the existing user control should be dynamically created at that location. If any one have any idea to solve this, please reply me as soon as possible. Thanks in advance.

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

          unless I'm missing the point entirely wouldnt it be something along these lines:

          yourControl.Left = Convert.ToInt32(yourXPosTextBox.Text);
          yourControl.Top = Convert.ToInt32(yourYPosTextBox.Text);

          or at least thats how I position controls within a form, if you mean something else ginore me :)

          1 Reply Last reply
          0
          • D DaveyM69

            If using 2.0 or above you can use a generic function such as this in your form.

            private void AddControl<T>(string text, Point location, Size size) where T : Control, new()
            {
            T control = new T();
            control.Text = text;
            control.Location = location;
            control.Size = size;
            this.Controls.Add(control);
            }

            and call it like

            AddControl<Button>("New Button", new Point(12, 12), new Size(75, 23));

            Dave
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
            Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
            Why are you using VB6? Do you hate yourself? (Christian Graus)

            M Offline
            M Offline
            Michael Bookatz
            wrote on last edited by
            #5

            nice solution

            1 Reply Last reply
            0
            • D DaveyM69

              If using 2.0 or above you can use a generic function such as this in your form.

              private void AddControl<T>(string text, Point location, Size size) where T : Control, new()
              {
              T control = new T();
              control.Text = text;
              control.Location = location;
              control.Size = size;
              this.Controls.Add(control);
              }

              and call it like

              AddControl<Button>("New Button", new Point(12, 12), new Size(75, 23));

              Dave
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
              Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
              Why are you using VB6? Do you hate yourself? (Christian Graus)

              H Offline
              H Offline
              Henry Minute
              wrote on last edited by
              #6

              :cool::thumbsup:

              Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

              1 Reply Last reply
              0
              • D DaveyM69

                If using 2.0 or above you can use a generic function such as this in your form.

                private void AddControl<T>(string text, Point location, Size size) where T : Control, new()
                {
                T control = new T();
                control.Text = text;
                control.Location = location;
                control.Size = size;
                this.Controls.Add(control);
                }

                and call it like

                AddControl<Button>("New Button", new Point(12, 12), new Size(75, 23));

                Dave
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
                Why are you using VB6? Do you hate yourself? (Christian Graus)

                R Offline
                R Offline
                Rob Philpott
                wrote on last edited by
                #7

                Well blinking 'eck. I never knew that Control had a text property. Just assumed it was lower down in the class hierarchy on certain controls. That's what I've learnt today. :)

                Regards, Rob Philpott.

                D 1 Reply Last reply
                0
                • R Rob Philpott

                  Well blinking 'eck. I never knew that Control had a text property. Just assumed it was lower down in the class hierarchy on certain controls. That's what I've learnt today. :)

                  Regards, Rob Philpott.

                  D Offline
                  D Offline
                  DaveyM69
                  wrote on last edited by
                  #8

                  Control has got way too many properties IMO. Deriving from it to create a new control is a PITA as you have to try and hide all the non relevant ones (and methods too) which is not only messy/time consuming but very non OOP. If they had a IControl interface that just had the basics required, and a form's Controls property was a list of IControl it would be way better. Something like

                  interface IControl : ISynchronizeInvoke, IWin32Window
                  {
                  bool Enabled { get; set; }

                  Point Location { get; set; }
                  
                  Size Size { get; set; }
                  
                  bool Visible { get; set; }
                  

                  }
                  public abstract ControlBase: IControl
                  {
                  // Implement derived stuff here
                  ...
                  //
                  }

                  It'll never happen though :( [Edit] Made a mess of Interface with ControlBase stuff in it :doh: [/Edit] Actually, now I've thought about it a few minutes - just the ControlBase (would need a little more than I provided so we can have OnPaint etc to override) would be enough. Give me ten minutes and I'll probably change my mind again!

                  Dave
                  BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                  Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
                  Why are you using VB6? Do you hate yourself? (Christian Graus)

                  modified on Tuesday, May 12, 2009 11:20 AM

                  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