Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Tham khảo tài liệu 'c# bible 2002 phần 7', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | and background colors. Because all the controls share this behavior it makes sense to encapsulate it in a base class and derive the control-specific functionality in derived classes. The authors of the control classes found in the .NET Framework took this design approach when building the classes. Figure 21-2 Control class hierarchy Working with controls on a form Listing 21-7 shows a WindowsForm application that includes a button. The button displays a message in a message box when clicked. Listing 21-7 Working with a Button on a Form using System using System.Drawing using System.Windows.Forms public class MainForm Form public static void Main MainForm MyForm new MainForm Application.Run MyForm public MainForm Button MyButton new Button Text Button Test MyButton.Location new Point 25 25 MyButton.Text Click Me MyButton.Click new EventHandler MyButtonClicked Controls.Add MyButton public void MyButtonClicked object sender EventArgs Arguments MessageBox.Show The button has been clicked. Listing 21-7 illustrates several important concepts that you need to keep in mind when working with WindowsForms controls. Consider first the form s constructor. It creates a new object of a Button class and sets its position on the form with the button s Location property. This property is inherited from the Control class which means that the property is available to any control derived from the Control class and sets the position of the upper-left corner of the button relative to its container. In Listing 21-7 the button s location is set to a position 25 pixels to the right of the form s left edge and 25 pixels below the top of the form. The position is set with a new instance of a structure called Point which is available in the .NET Framework System.Drawing namespace MyButton.Location new Point 25 25 Tip Listing 21-7 uses the Location property to set the positions of the control. Using this property to programmatically position controls can be tedious for complicated forms with .