Skip to main content

Add, Delete and Move controls at runtime.



In this article you will learn how to:
  • Add or delete controls at runtime.
  • Access controls with the indexer.
  • Create control collection using Hash Table.

To start with, create a windows application project and add a class to it. We will create a button array so name it as “ButtonArray.cs”.

Add the following code to it:

Class “ButtonArray” is inherited from the Hash table, so it will be easy for us to add buttons in the collection. Let’s have a look into the “AddButton” method.

In this method, we create an instance of a button with the necessary properties and add it to the hash table as well as to the form’s control collection. Each time the control is added in the hash table with its unique key. This helps us to access the controls when required.

Now we’ll see how to remove control from the hash table and form’s control collection.
In order to remove the control, just pass the key. That’s simple! Isn’t it? :-)


Ok! That was about the methods to add and remove controls from the collection. Now we will see how to use this class from the UI (Form).
First thing to do is to initialise the ButtonArray class and a but
ton.

ButtonArray buttonArray;
Button btnSelected = new Button();

Now create two more buttons on the form and name them as “AddButton” and “RemoveButton”.
Code them as shown below.


Under the Mouse Down event of the form, code as shown below.



You can add other controls as well. Just create array class for each control type and use it.

And yea, please do not forget to leave a comment on this post.

Comments

  1. Hi... thanks alot for this code sample..

    ReplyDelete
  2. Hi..thanks for this..
    but how do we update the properties, meaning if I want change the forecolor of the button...how can i do this..can i achieve in this class itself

    ReplyDelete
  3. Yes you can change the ForeColor by creating a Public method. Something like this.


    public void ForeColor(object sender, Color foreColor)
    {
    ((Button)sender).ForeColor = foreColor;
    }


    Add this method to the ButtonArray.cs class.

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. This example is conceptually perfect for resolving my problem, save for the fact that I am looking for a VB.Net version. (Yes, this old dog has yet to learn the new tricks of C#.) I will attempt to translate this to VB.Net with the assistance of the excellent commentary you add to the code in this post. Thanks for sharing.

    ReplyDelete
  6. Thank you very much, this code is very useful for dynamic creation of form controls :D

    ReplyDelete
  7. am adding datagridview in the same manner,but wat happen is am unable to add columns for that gridview any help??
    thanks..

    ReplyDelete
  8. You need to get the control by its name.

    DataGridView dtv = HostForm.Controls["dataGridView1.Site.Name"] as DataGridView;

    Now you can access this control and create the columns.

    ReplyDelete
  9. Thanks for your reply..
    Actually am new to this .net application,
    so,can u tell me about this something detailed like where to add this and how can we access this control??

    ReplyDelete
  10. Can we do the same in WPF??
    Thanks in Advance...

    ReplyDelete
  11. HELLO Mazhar Haque is there any way i cant contact you i need your help my email is bloodsville08@hotmail.com
    They thing I wold like to know if its possible is to, at the mouse event of click instead of a new button just click and drag (selecting a rectangular area) and when mouse is release the new button will have the rectangular shape the user decided. if so can you help me there or contact me? thanks a lot for this tutorial

    ReplyDelete
  12. after add a few button, how can i save it by using binary serialization??

    ReplyDelete
  13. You cannot serialize the form or its contents (i.e. child controls) because the controls cannot be serialized. The controls do not have [Serializable] attribute.

    If you want to save the state of your form after adding controls, then create XML and save the properties as nodes within the XML.

    If you are looking for some real form designers and want to save/load controls then i'll suggest you look the below article:

    http://msdn.microsoft.com/en-us/magazine/cc163634.aspx

    ReplyDelete
  14. Hi guys... How about for textboxes... and their name will be increased one by one... like txt.Box01, txtBox02... And to add to a specific place on the form automatically...

    ReplyDelete
  15. Thanks for the tutorial!

    I have a question, if I want to assign an action to the created button, what would be the best way to do so?

    In frmMain.cs I could utilize the buttonArray_SelectedButton function and help myself with an array or a switch/case to decide which action to execute depending on which button was pressed. But there has to be a more elegant way.

    Any suggestions?

    Manu.

    ReplyDelete

Post a Comment

Popular posts from this blog

Hiding / Disabling Maximize and/or Minimize Box in WPF

After trying my hands on the WPF, I realized that I could convert my existing Windows Forms applications into rich looking WPF applications. The task of migration was definitely not easy. During the course of migration, I came across many hurdles and seemed it was impossible to achieve in WPF. But thanks to the msdn help, which made a lot of things easier :-) Since I was converting my existing application, I started looking for the similar features in WPF. The problem I faced was in the initial phase itself where I wanted to disable MaximizeBox button in WPF. This posed a challenge to me since I had no clue as to how to achieve this. After searching the net for a while, I got a solution which I will share with you all to make your life easier. Disabling Maximize Box Button on the WPF Window: This can be done as follows: Put this piece of code above the main class. internal static class WindowExtensions {      [DllImport("user32.dll")]      internal exte...

Create a Virtual Drive through a Command Prompt

Just a few weeks back, I was working on a very old application where in all the directory paths used in the code were hard coded and pointing to a S:/ drive. Since I did not have a drive "S" on my machine, and I had to make the application up and running, I was left two choices: 1. Rename all the directory paths in the application, OR 2. Create a Virtual Drive Option 1 seemed to be a lot time consuming and hence I chose option 2 which I felt was a lot easier to do. I searched on the net, and found out one command which could be used to create virtual drives. Windows OS has an application called " subst"  which helps in creating virtual drives mapped to the local folder. The syntax of the command is: SUBST [drive1: [drive2:]path] Example: subst S: C:\Dev When you run this command,  a drive will be created with a letter S: assigned to a local folder "Dev" on drive C.  This is an easy way to create Virtual Drives, though the drives...