Click here to Skip to main content
15,888,351 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   332   7   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 
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 
Why you might ask..

- nothing new - simple usage of for loop
- awful naming convention
- not an article - to short, to few information
- using strings where enums would do much better

just to name a few..
life is study!!!

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.