Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Working with Array of Controls in C#

Author: DevASP
Download Source Code : 674_ArrayOfControlsObject.zip

In this article I will try to explain you with the sample program that how you can create the array of objects in C#.

While creating application some time we need to maintain/create arrays of simple constant variables or we need to maintain array of objects. C# allows the developer to create array of objects same as simple array. The only difference is first we create object of array then we assign value of objects in array.

 

To begin with application, create a new windows application in C# and drop the listbox control, button control and checkbox control on form.

 

Now in code behind declare two global arrays of controls and define the following method:

 

        Control[] arr = new Control[10];

        Control[] arr1 = new Control[10];

     

 

public Control[] arrayOfControl()

        {

            for (int i = 0; i < 10; i++)

            {

                arr[i] = new Control();

            }

 

            return arr;

        }

 

After adding the following code now adds two events (button click event and Checkbox check change events).

 

        private void button1_Click(object sender, EventArgs e)

        {

           arr1 = arrayOfControl();

 

           for (int i = 0; i < 10; i++)

           {

               Button btn = new Button();              

               arr[i].Controls.Add(btn);

               if (arr[i].Contains(btn) == true)

               {

                   listBox1.Items.Add("btn added");

               }

           }

        }

 

 

 

This method defines the new button control and adds that control in array of controls. Similarly we can add the check box control in array of controls:

 

     

 

 

 

private void checkBox1_CheckedChanged(object sender, EventArgs e)

        {

 

            arr1 = arrayOfControl();

 

            for (int i = 0; i < 10; i++)

            {

                CheckBox chk = new CheckBox();

                arr[i].Controls.Add(chk);

                if (arr[i].Contains(chk) == true && checkBox1.Checked == true)

                {

                    listBox1.Items.Add("Checkbox Control Added");

                }

                else if(checkBox1.Checked == false)

                    listBox1.Items.Remove("Checkbox Control Removed");

 

            }

        }

 

In similar fashion we can store controls object in array of object and use them in our application.

Article Comments
hi
i have checkd uyours example i want example in array list.
Regards
husain

Posted on 2/16/2007 12:36:33 AM by Husain

this information u have provided is very simple, precise well understood.
its more clear and explained in good manner.
thank you

Posted on 2/19/2007 3:40:11 AM by vishwanath

In My application i am using more than 13
textboxs which contain certain values
at runtime
Using control array will you show the method how to use it in C#.NET2005
Please guide me

Posted on 3/11/2007 1:37:32 PM by Raman

I want to use Control Array in C#.NET VS2005 in the same way we used it in VB6.
Take a same example of addition of 10 nos. in Textbox and show
Please help me as fast as you can

Posted on 3/11/2007 1:54:49 PM by Raman

Hello
how can i use check box and work with database

Posted on 7/17/2007 7:56:03 AM by arshad

I am using two tables binded to two separate datalist.I have to take the value from one datalist or one datatable AND compare with data in another table and if the record in first table matches with record in another table i have to display all data in datalist3.Please give me the code-behind

Posted on 9/6/2007 8:19:30 AM by Poorni

I'm creating an array of textboxes of size 60 and these are successfully creating but the problem is that I'm unable to create its event as key down or key press, because the text boxes are creating at runtime, please help me.

Posted on 10/30/2007 9:09:14 AM by Savita Pal

I don't really understand how arrays work and their importance, please help.

Posted on 11/6/2007 5:19:23 AM by Lorraine

I want to create an array of objects in C# and how can I access those objects.
please guide me

Posted on 12/12/2007 8:03:42 AM by Natarajan

hi,
In case a program where an array of 3 employees and his respective info should be accepted and displayed and their netpay calculated

Posted on 2/21/2008 3:14:31 AM by sharon

hiiii
pls tell me how can i make array of controls. i'm going to made a calculator in c#windows application.i want that when i click button1 - button9 .the text related to the button which i clicked will be enter in the textbox.i want make only one function and all buttons 1-9 working...pls send me the source code on my id..

Posted on 4/19/2008 7:28:01 AM by Rohit

I have created fileupload control dynamically.How to provide functionality or event for that dynamically created fileupload

Posted on 6/6/2008 4:31:35 AM by jahnavi

I have created fileupload control dynamically.How to provide functionality or event for that dynamically created fileupload?

Posted on 6/17/2008 1:36:50 AM by jahnavi

I have created control "ascx". and add it the in old ASCX but Old ASCX not change?

Posted on 7/14/2008 12:43:19 AM by zuangshijum

can you post the event handler code also???

Posted on 9/8/2008 2:35:37 AM by Arun

you right, but i need how to use the control(button). can u give sample code

Posted on 11/6/2008 1:19:43 AM by anand

hi
i want to know that how to create array of object in c#.net.
can anyone please tell me fast.
its urgent.


thank u.

Posted on 4/13/2010 7:41:44 AM by rahul

Thanks for this code.I am new to c# and Iwas searching for array of controls.

Posted on 5/4/2010 6:52:20 AM by ena

hi
once you have remove one control from the control array.how to automatically rearrange the control arrays as per sequence manner.

Posted on 5/13/2010 7:33:37 AM by R.Chinandurai

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Basic Textbox Control Events in C#

Disclaimer - Privacy
© 2002-2017 DevASP.net