Implementing a simple array of controls in C #

Source: Internet
Author: User

One classmate is doing calculator program, another classmate is doing well word chess game. The two programs have a common feature: a number of controls with similar functions (the calculator's number button and the nine drop bits of the well character). If you create these controls, you'll have to write a lot of duplicate code, which is more cumbersome to modify. A better option is to create an array of controls. The following is a simple implementation of the button array:

Button[] btns = new Button[9];
private void ShowButtonArray()
{
for(int i = 0; i < 9; i++)
{
btns[i] = new Button();
//这一句往往为初学者忽视,须知要创建对象的实例!
btns[i].Location =
new System.Drawing.Point(100 + 50 * (i % 3),100 + 50 * (i / 3));
btns[i].Name = "btnTest";
btns[i].Size = new System.Drawing.Size(48, 48);
btns[i].Text = i.ToString();
btns[i].Click += new System.EventHandler(this.btns_Click);
//统一的事件处理
this.Controls.Add(btns[i]);//在窗体上呈现控件
}
}
private void btns_Click(object sender, System.EventArgs e)
{
MessageBox.Show(((Button)sender).Text + " was clicked !");
//通过sender判断激发事件的控件
}
private void Form1_Load(object sender, System.EventArgs e)
{
ShowButtonArray();
}

In fact, you can quickly understand the "code generated by the Windows forms Designer" just by looking at it. NET creates and renders a control's process, which writes out a simple array of controls. In the example above, the position of the button rendering is calculated by a formula, and in practice, it can be flexibly changed according to the need (for example, the calculator's number button, 1~9 can be calculated by formula, 0 can be used if such as the statement special processing). Also noteworthy is the uniform handling of events: in the Btns_click function, the control that fires events is judged by sender.

If necessary, you can encapsulate the array of controls into classes, plus certain functional code to make it easy to use. Even some custom controls can be made into array classes to achieve more complex functionality.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.