- 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 button.
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.
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.
Hi... thanks alot for this code sample..
ReplyDeleteHi..thanks for this..
ReplyDeletebut 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
Yes you can change the ForeColor by creating a Public method. Something like this.
ReplyDeletepublic void ForeColor(object sender, Color foreColor)
{
((Button)sender).ForeColor = foreColor;
}
Add this method to the ButtonArray.cs class.
This comment has been removed by a blog administrator.
ReplyDeleteThanks for your help
ReplyDeleteThis 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.
ReplyDeleteThank you very much, this code is very useful for dynamic creation of form controls :D
ReplyDeletesuperb yaar
ReplyDeleteam adding datagridview in the same manner,but wat happen is am unable to add columns for that gridview any help??
ReplyDeletethanks..
You need to get the control by its name.
ReplyDeleteDataGridView dtv = HostForm.Controls["dataGridView1.Site.Name"] as DataGridView;
Now you can access this control and create the columns.
Thanks for your reply..
ReplyDeleteActually 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??
Can we do the same in WPF??
ReplyDeleteThanks in Advance...
HELLO Mazhar Haque is there any way i cant contact you i need your help my email is bloodsville08@hotmail.com
ReplyDeleteThey 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
after add a few button, how can i save it by using binary serialization??
ReplyDeleteYou cannot serialize the form or its contents (i.e. child controls) because the controls cannot be serialized. The controls do not have [Serializable] attribute.
ReplyDeleteIf 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
Good one!
ReplyDeleteHi 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...
ReplyDeleteThanks for the tutorial!
ReplyDeleteI 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.