150 likes | 498 Views
Windows Forms Controls . Wednesday, October 10, 2012. Windows Forms control hierarchy. Windows Forms Controls. Windows Forms Controls. Windows Forms Controls. Working Buttons (Button class).
E N D
Windows Forms Controls Wednesday, October 10, 2012
Working Buttons (Button class) One of the most common controls used in windows applications are buttons. Buttons can be created using the—you guessed it—Button class! Buttons differ from labels, so you will most likely want an action to occur when the user clicks on a button. Before jumping into creating button actions, it is worth taking a minute to cover creating and drawing buttons. As with labels, the first step to using a button is to instantiate a button object with the class: Button myButton = new Button(); After you’ve created the button object, you can then set properties to customize it to the look and feel you want. As with the Label control, there are too many properties, data members, and methods to list here. You can get the complete list from the help documents.
Image in Button The Image property of a Button control is used to set a button background as an image. The Image property needs an Image object. The Image class has a static method called FromFile that takes an image file name with full path and creates an Image object. You can also align image and text. The ImageAlign and TextAlign properties of Button are used for this purpose. The following code snippet sets an image as a button background. // Assign an image to the button. dynamicButton.Image = Image.FromFile(@"C:\Images\Dock.jpg"); // Align the image and text on the button. dynamicButton.ImageAlign = ContentAlignment.MiddleRight; dynamicButton.TextAlign = ContentAlignment.MiddleLeft; // Give the button a flat appearance. dynamicButton.FlatStyle = FlatStyle.Flat;
Button Events Recall that buttons differ from labels; you generally use a button to cause an action to occur. When the user clicks on a button, you want something to happen. To cause the action to occur, you use events. This method must also be protected and of type void. The format is protected void methodName( object sender, System.EventArgs args ) When working with windows, you generally name the method based on what control caused the event followed by what event occurred. For example, if button ABC was clicked, the method name for the handler could be ABC_Click. To activate the event, you need to associate it to the appropriate delegate. A delegate object called System.EventHandler takes care of all the windows events. By associating your event handlers to this delegate object, they will be called when appropriate. The format is ControlName.Event += new System.EventHandler(this.methodName); where ControlName.Event is the name of the control and the name of the event for the control. this is the current form, and methodName is the method that will handle the event (as mentioned previously). You will see that the date and time are still displayed in the form. You will also see, however, that a button has been added. When the button is clicked, an event fires that will update the date and time. Additionally, four other event handlers have been added to this listing for fun. These events are kicked off whenever the mouse moves over or leaves either of the two controls.
Creating a Button Dynamically Creating a Button control at run-time is merely a work of creating an instance of Button class, set its properties and add Button class to the Form controls. First step to create a dynamic button is to create an instance of Button class. The following code snippet creates a Button control object. Next step, you need to set Button class properties. You need to make sure to specify the Location, Width, Height or Size properties. The default location of Button is left top corner of the Form. The Location property takes a Point that specifies the starting position of the Button on a Form. The Size property specifies the size of the control. We can also use Width and Height property instead of Size property. The following code snippet sets Location, Width, and Height properties of a Button control. In the next step, you may set more properties of the Button control. The following code snippet sets background color, foreground color, Text, Name, and Font properties of a Button. A Button control is used to process the button click event. We can attach a button click event handler at run-time by setting its Click event to an EventHandler obect. The EventHandler takes a parameter of an event handler. The Click event is attached in the following code snippet. The signature of Button click event handler is listed in the following code snippet. Now the last step is adding a Button control to the Form. The Form.Controls.Add method is used to add a control to a Form. The following code snippet adds a Button control to the current Form. The complete code is listed in Listing , where CreateDynamicButton methods creates a Button control to a Form at run-time, attaches a click event handler of the button and adds Button control to the Form by calling Form.Controls.Add() method.
Creating a Button Dynamically You need to make sure to call CreateDynamicButton() method on the Form's constructor just after InitializeComponent() method, listed as following. public Form1() { InitializeComponent(); CreateDynamicButton(); } /// This method creates a Button control at runtime private void CreateDynamicButton() { // Create a Button object Button dynamicButton = new Button(); // Set Button properties dynamicButton.Height = 40; dynamicButton.Width = 300; dynamicButton.BackColor = Color.Red; dynamicButton.ForeColor = Color.Blue; dynamicButton.Location = new Point(20, 150); dynamicButton.Text = "I am Dynamic Button"; dynamicButton.Name = "DynamicButton"; dynamicButton.Font = new Font("Georgia", 16); // Add a Button Click Event handler dynamicButton.Click += new EventHandler (DynamicButton_Click); // Add Button to the Form. Placement of the Button // will be based on the Location and Size of button Controls.Add(dynamicButton); } /// Button click event handler private void DynamicButton_Click(object sender, EventArgs e) { MessageBox.Show("Dynamic button is clicked"); }