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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. howto add control dynamicaly

howto add control dynamicaly

Scheduled Pinned Locked Moved C#
helptutorialquestion
5 Posts 4 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.
  • S Offline
    S Offline
    Stephan Wright
    wrote on last edited by
    #1

    Hy everyone! I designed a windows Form which contains a control by default. Now I do want to add another control depending on the data, meaning if the data contains ínfos which have to be displayed in the control then the control should be inserted, the Form size is changed etc. (if the length where the data is stored is not 0) I managed to check if the data needs the second form and I changed the Forms size, but at the moment I am not sure how to insert the second control dynamicaly. I can't just throw it in like I did with the otherone and let the code being insert automaticaly. So I do want to do something like if(data.secondform.length!=0) { resize Form insert second control display data in the control } the other way round shouldn't be the problem meaning removing the control again when I do get data which is not asking for the control. Could anyone of you please tell me what I do have to do to insert the control in my Forms windows? (maybe with some code statements) Thanks! Stephan.

    S N L 3 Replies Last reply
    0
    • S Stephan Wright

      Hy everyone! I designed a windows Form which contains a control by default. Now I do want to add another control depending on the data, meaning if the data contains ínfos which have to be displayed in the control then the control should be inserted, the Form size is changed etc. (if the length where the data is stored is not 0) I managed to check if the data needs the second form and I changed the Forms size, but at the moment I am not sure how to insert the second control dynamicaly. I can't just throw it in like I did with the otherone and let the code being insert automaticaly. So I do want to do something like if(data.secondform.length!=0) { resize Form insert second control display data in the control } the other way round shouldn't be the problem meaning removing the control again when I do get data which is not asking for the control. Could anyone of you please tell me what I do have to do to insert the control in my Forms windows? (maybe with some code statements) Thanks! Stephan.

      S Offline
      S Offline
      S Sansanwal
      wrote on last edited by
      #2

      check http://support.microsoft.com/default.aspx?scid=kb;EN-US;319266 Sanjay Sansanwal www.sansanwal.com

      S 1 Reply Last reply
      0
      • S S Sansanwal

        check http://support.microsoft.com/default.aspx?scid=kb;EN-US;319266 Sanjay Sansanwal www.sansanwal.com

        S Offline
        S Offline
        Stephan Wright
        wrote on last edited by
        #3

        is this the same for Windows.Controls? Meaning could I use the button for example as a sample for my task? Thanks! Stephan.

        1 Reply Last reply
        0
        • S Stephan Wright

          Hy everyone! I designed a windows Form which contains a control by default. Now I do want to add another control depending on the data, meaning if the data contains ínfos which have to be displayed in the control then the control should be inserted, the Form size is changed etc. (if the length where the data is stored is not 0) I managed to check if the data needs the second form and I changed the Forms size, but at the moment I am not sure how to insert the second control dynamicaly. I can't just throw it in like I did with the otherone and let the code being insert automaticaly. So I do want to do something like if(data.secondform.length!=0) { resize Form insert second control display data in the control } the other way round shouldn't be the problem meaning removing the control again when I do get data which is not asking for the control. Could anyone of you please tell me what I do have to do to insert the control in my Forms windows? (maybe with some code statements) Thanks! Stephan.

          N Offline
          N Offline
          Nick Parker
          wrote on last edited by
          #4

          The Form class in System.Windows.Forms namespace contains a Controls property which is a collection of controls. It has methods to add and remove controls, among others. Check it out on MSDN. - Nick Parker
          My Blog | My Articles

          1 Reply Last reply
          0
          • S Stephan Wright

            Hy everyone! I designed a windows Form which contains a control by default. Now I do want to add another control depending on the data, meaning if the data contains ínfos which have to be displayed in the control then the control should be inserted, the Form size is changed etc. (if the length where the data is stored is not 0) I managed to check if the data needs the second form and I changed the Forms size, but at the moment I am not sure how to insert the second control dynamicaly. I can't just throw it in like I did with the otherone and let the code being insert automaticaly. So I do want to do something like if(data.secondform.length!=0) { resize Form insert second control display data in the control } the other way round shouldn't be the problem meaning removing the control again when I do get data which is not asking for the control. Could anyone of you please tell me what I do have to do to insert the control in my Forms windows? (maybe with some code statements) Thanks! Stephan.

            L Offline
            L Offline
            LongRange Shooter
            wrote on last edited by
            #5

            Here is one possible approach: in start of program:

            object Item = GetData();
            ...cast Item to specific type...

            in your data component:

            public object GetData()
            {
            ...read the data and determine the different types of data
            ...now cast that data to the specific type of data
            ...and return that type-specific object
            }

            public class DataTypeOne
            { .. what makes this unique .. }
            public class DataTypeTwo
            { .. what makes this unique .. }

            in your form

            InitializeComponents(); // look familiar?
            LoadForm(TypeSpecificItem);
            ...
            public void LoadForm(DataTypeOne item)
            {
            DataOneForm form = new DataOneForm(item);
            this.Components.Add(form);
            }
            public void LoadForm(DataTypeTwo item)
            {
            DataTwoForm form = new DataTwoForm(item);
            this.Components.Add(form);
            }
            .... overload for each object type ....

            This signature left intentionally blank

            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