Click here to Skip to main content
15,899,026 members
Articles / Programming Languages / C#

Controls Array

Rate me:
Please Sign up or sign in to vote.
3.82/5 (8 votes)
17 May 2011CPOL 22.5K   333   8   6
Basic steps to create controls in array

Introduction

This post describes how to create controls in an array.

Background

You can now create the defined number of controls in an array according to the user input or as desired.

Using the Code

Here, you can find sample code to create the number of controls in an array.

As the user inputs the value and clicks the button to create controls, the controls are created as defined.

C#
public partial class Form1 : Form
{
	public Form1()
	{
		InitializeComponent();
	}

	private System.Windows.Forms.TextBox[] TextBoxArray;
   private System.Windows.Forms.Label[] LabelArray;

	private int Draw_count = 0;
   private void AddControls(string AnyCtrl, int CtrlNum)
	{
		switch (AnyCtrl)
		{
			case "LabelCtrl":
			{
				LabelArray = new System.Windows.Forms.Label[CtrlNum];
               for (Draw_count = 0; Draw_count < CtrlNum; Draw_count++)
				{
					LabelArray[Draw_count] = 
					new System.Windows.Forms.Label();
				}
				break;
			}
			case "TextBoxCtrl":
			{
				TextBoxArray = 
				new System.Windows.Forms.TextBox[CtrlNum];
               for (Draw_count = 0; Draw_count < CtrlNum; Draw_count++)
				{
					TextBoxArray[Draw_count] = 
					new System.Windows.Forms.TextBox();
				}
				break;
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			if (maskedTextBox1.Text == string.Empty)
			{
				MessageBox.Show("Please, enter the value.", 
				"Error message", MessageBoxButtons.OK, 
				MessageBoxIcon.Error);
               maskedTextBox1.Focus();
			}
			else
			{
				this.panel1.Controls.Clear();
               AddControls("TextBoxCtrl", Convert.ToInt32(maskedTextBox1.Text));

				Draw_count = Convert.ToInt32(maskedTextBox1.Text);
				{
					for (int i = 0; i < Draw_count; )
					{
						TextBoxArray[i].Location = 
						new Point(5, (5 + (22 + 5) * i));
                    	TextBoxArray[i].Size = new Size(150, 22);
                    	TextBoxArray[i].Text = "Textbox " + (i + 1).ToString();
                    	TextBoxArray[i].ForeColor = Color.Navy;

						this.panel1.Controls.Add
						(TextBoxArray[i]);

						i++;
					}
				}
			}
		}

		private void button2_Click(object sender, EventArgs e)
		{
			if (maskedTextBox2.Text == string.Empty)
			{
				MessageBox.Show("Please, enter the value.", 
				"Error message", MessageBoxButtons.OK, 
				MessageBoxIcon.Error);
               maskedTextBox2.Focus();
			}

			else
			{
				this.panel2.Controls.Clear();
               AddControls("LabelCtrl", Convert.ToInt32(maskedTextBox2.Text));

				Draw_count = Convert.ToInt32(maskedTextBox2.Text);

				for (int j = 0; j < Draw_count; )
				{
					 LabelArray[j].Location = 
					new Point(5, (5 + (22 + 5) * j));
                    LabelArray[j].Size = new Size(150, 22);
                    LabelArray[j].Text = "Label " + (j + 1).ToString();
                    LabelArray[j].ForeColor = Color.Navy;

					 this.panel2.Controls.Add(LabelArray[j]);

					 j++;
				}
			}
		}
	}
}
  1. Enter the number to create textbox or label.

  2. Click the create button to create textbox or label.

  3. You can see the list of array controls created.

Thank you.
Santosh Shrestha

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Nepal Nepal
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 2 Pin
lukeer28-Nov-13 19:56
lukeer28-Nov-13 19:56 
GeneralVery Useful Information Pin
rctaubert17-May-11 8:20
rctaubert17-May-11 8:20 
GeneralRe: Very Useful Information Pin
HaBiX17-May-11 23:06
HaBiX17-May-11 23:06 
GeneralRe: Very Useful Information Pin
rctaubert18-May-11 2:50
rctaubert18-May-11 2:50 
First of all let me say I am just an amateur programmer. The only programming I did professionally was in MS Access and I was pretty good at it. That was ten years ago. I used VB6 to write utilities that I used to support the users in my organization and small apps for myself. Today, I use VB.net 10 to write programs for myself.

YES, you are absolutely correct in your statement “you can do arrays of anything since .net 1.0”. BUT, and this is a BIG BUT, you could not access them individually in the manner of LabelArray(n).

Back around 2006 there was a big discussion at About.com for Visual Basic about Control Arrays. Various programmers, all of whom were considered pretty knowledgeable, came up with eight different methods for creating them. None of the methods was a simple as the method used in VB6 or the method shown by santoshshrest.

Apparently you are not familiar with the Control Array as it existed in VB6. You could place a Control (say a textbox or label) on a form, then copy and paste the copy back to the form. VB6 would ask you if you wanted to make a Control Array out of them. Answering Yes would allow you to access each control in the array in the manner of LabelArray(n). Adding a control array at runtime was just as easy.

I never ran across a reason to use Control Arrays professionally so I can understand why you and Seishin# don't consider this article of any value. But I have an App that I wrote for my own use that uses three control arrays.

I love to work Cryptograms. According to Wikipedia “A cryptogram is a type of puzzle which consists of a short piece of encrypted text. Generally the cipher used to encrypt the text is simple enough that cryptogram can be solved by hand. Frequently used are substitution ciphers where each letter is replaced by a different letter or number. To solve the puzzle, one must recover the original lettering. Though once used in more serious applications, they are now mainly printed for entertainment in newspapers and magazines.”

I wrote my little app to help me solve cryptograms. There are three control arrays of labels. Each label contains one character. The first control array contains the letters of the alphabet and a blank character. The second array contains the letters, spaces and punctuation of the encrypted text and the third contains the letters, spaces and punctuation of my solution.

I click on a letter of the alphabet (array 1) to select the letter I want to use to replace a letter in the encrypted text. Then I click on the letter in the encrypted text (array 2) to be replaced and the replacement letter appears in the 3rd array under the encrypted text.

When I first tried to convert my program from VB6 to .Net I tried using your method. It was slow and very convoluted. I gave up on converting it until I ran across the discussion on creating control arrays in .Net.

Now that there is a very simple method of creating control arrays I may go ahead and update my program with many of the methods and techniques I have learned over the last few years.

So, enough of you and Seishin# putting us down simply because YOU don't fully understand the problem being addressed and don't see any value in it. Control Arrays (as implemented in VB6) have a very limited usefulness, but when they are needed, nothing else will work.
GeneralRe: Very Useful Information Pin
lukeer28-Nov-13 19:54
lukeer28-Nov-13 19:54 
General[My vote of 1] not an article and not useful at all.. Pin
Seishin#17-May-11 4:38
Seishin#17-May-11 4:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.