C # WinForm dynamic control instance: oral calculation Training

Source: Internet
Author: User

  

I went back to my bedroom last night and saw my roommate being troubled by a C # class assignment. The assignment content was to write a verbal training program to add questions at will. So I decided to save them if I liked to write C.

Create dynamic controls

Since dynamic addition is required, dynamic controls must be used. In C #, controls are also classes. In addition to adding fixed controls when drawing a form, you can also add them in code using the method of instantiating classes.

The specific operation is to first define a control variable, then set the Size and Location attributes of the control, and finally add a panel to the control. In addition, we only need to define the control variable once, and then use new to continuously add, we can get a lot of controls.

Some code is as follows:

TxtBox = new TextBox (); txtBox. size = new Size (50, 50); // you can specify the Size of txtBox. location = new Point (x, y); // you can specify the coordinates of txtBox. name = "txt" + Convert. toString (I); // set the control name (which can be duplicate) panelQuestion. controls. add (txtBox );
Access dynamic controls

For controls manually drawn in the form, we can directly access them through the control Name, but the dynamically added controls cannot. You can only find the control corresponding to the Name attribute in the panel.

string str = ((TextBox)panelQuestion.Controls.Find("txtBox" ,true)[0]).Text;

The first parameter in the Find method is the control name, and the second parameter is whether to search for the Child control. The return result is a control array. The preceding [0] indicates that the first returned result is obtained. Since the returned type is Control, you also need to forcibly convert it to a specific Control type. Therefore, a TextBox is added before and it is forcibly converted to the TextBox type before it can be used as a TextBox.

Implementation

For example, the control names are txtTotal, btnAdd, btnJudge, and panelQuestion.

In the question button event, dynamically add TextBox and Label. Three TextBox entries are displayed in each row, and the result of two adders and one blank box is displayed. The names are both txt + row numbers. There are also three labels, "+", "=", and blank from left to right are used to display right and error.

In the correction button event, access the control that has been dynamically created to obtain the value in the TextBox, and then correct it, and write the right or wrong to the last Label of each line.

The running result is as follows:

Other things are not nonsense. paste the code!

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; namespace AddProgram {public partial class Form1: Form {public Form1 () {InitializeComponent ();} private void btnAdd_Click (object sender, EventArgs e) {if (txtTotal. text = "") {MessageBox. show ("Enter the number of questions! "," Error "); return;} // whether panelQuestion is not specified in the number of question items. autoScroll = true; // Add the scroll bar panelQuestion to the panel. controls. clear (); // Clear existing question int total = int. parse (txtTotal. text); // total number of questions TextBox txtBox = new TextBox (); Label label = new Label (); Random rand = new Random (); // random number for (int I = 0; I <total; I ++) {for (int j = 0; j <3; j ++) {txtBox = new TextBox (); txtBox. size = new Size (50, 50); // textbox Size txtBox. locat Ion = new Point (10 + 70 * j, 30 * I); // textbox coordinate txtBox. name = "txt" + Convert. toString (I); // set the control name if (j <2) {txtBox. text = Convert. toString (rand. next (100); txtBox. readOnly = true;} // generates a random number as the addition number panelQuestion. controls. add (txtBox); // Add the control to the panel label = new Label (); label. size = new Size (12, 12); label. location = new Point (64 + 70 * j, 30 * I); switch (j) {case 0: label. text = "+"; break; Case 1: label. text = "="; break; case 2: label. name = "labelResult" + Convert. toString (I); break;} panelQuestion. controls. add (label) ;}} private void btnJudge_Click (object sender, EventArgs e) {if (txtTotal. text = "") {MessageBox. show ("Enter the number of questions! "," Error "); return;} int total = Convert. toInt32 (txtTotal. text); for (int I = 0; I <total; I ++) {TextBox [] txtBox = new TextBox [3]; // use the control array to define the TextBox of each row. A total of three TextBox for (int j = 0; j <3; j ++) // obtain the TextBox that has been added dynamically to access txtBox [j] = (TextBox) panelQuestion. controls. find ("txt" + Convert. toString (I), true) [j]; Label label = (Label) panelQuestion. controls. find ("labelResult" + Convert. toString (I), true) [0]; if (txtBox [2]. text = "") // if no answer is provided, correct it as an error {label. text = "×"; continue;} int add = Convert. toInt32 (txtBox [0]. text) + Convert. toInt32 (txtBox [1]. text); if (add = Convert. toInt32 (txtBox [2]. text) label. text = "√"; else label. text = "× ";}}}}

  

 

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.