Reprint: use Visual C # to dynamically generate components. Please refer! (2)

Source: Internet
Author: User
3. Key steps and solutions in programming:
The main function of the software in this article is to use two buttons on the form to create two different types of WinForm components-the Button component and the TextBox component, and assign values to the attributes of each component while creating the component, an event is also created for each created component.
(1). How to create the Button component on the form:
In fact, it is very convenient to use Visual C # to create a component. Only the following two rows of statements can be used:
// Create a new Button component
Button myButton = new Button ();
// Display this button in the form
This. Controls. Add (myButton );
However, the Button component created at this time has no attributes and no events. The Button component created in the program described in this article has not only attributes but also events, the following statement is the source code of the Button component created in this article:
// The Button quantity calculator adds "1" after each button is pressed"
Counter + = 1;
// Add "3" to the vertical coordinate of the button to be generated"
LocY + = this. btnAdd. Height + 3;
// Create a new Button component
Button myButton = new Button ();
// Set its name and Text attributes and the relative location
MyButton. Name = "Button" + counter;
MyButton. Text = "button" + counter;
MyButton. Location = new Point (btnAdd. Location. X, locY );
// Set events for the generated new Button component. In this article, three events are set for the generated Button.
MyButton. MouseEnter + = new System. EventHandler (this. btn_MouseEnter );
MyButton. MouseLeave + = new System. EventHandler (this. btn_MouseLeave );
MyButton. Click + = new System. EventHandler (this. btn_Click );
// Display this button in the form
This. Controls. Add (myButton );
The program not only assigns values to the attributes of each component, but also creates three events for each component. Careful readers may have noticed that the names of events created by a program for each component are the same. In this way, there is a question about how to identify which Button component triggers the event in the same event.

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.