Other Functionality
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| Exam 70-526 Preparation Guide: Configure a Windows Form to control other functionality. |
Contents |
If you look in the Category view for the form, the following properties are listed under Misc:
Other functionality not covered within the Category view are:
-
Visible- Visibility of the form - Non-rectangular Forms
- Setting the Startup Form
-
AcceptButton
The default value is set to (none). This property allows you to designate a default action to occur when the user presses the ENTER key in your application. The button assigned to this property must be an IButtonControl that is on the current form or located within a container on the current form.
To change the AcceptButton property at run time, simply set the property to the desired value.
// Create button Button button1 = new Button(); // Set the text of button1 to "OK". button1.Text = "OK"; // Set the position of the button on the form. button1.Location = new Point (10, 10); // Set the accept button of the form to button1. this.AcceptButton = button1; // Add button1 to the form. this.Controls.Add(button1);
CancelButton
The default value is set to (none). This property allows you to designate a default action to occur when the user presses the ESC key in your application. The button assigned to this property must be an IButtonControl that is on the current form or located within a container on the current form.
To change the CancelButton property at run time, simply set the property to the desired value.
// Create button Button button2 = new Button (); // Set the text of button2 to "Cancel". button2.Text = "Cancel"; // Set the position of the button based on the location of button1. button2.Location = new Point (button1.Left, button1.Height + button1.Top + 10); // Set the cancel button of the form to button2. this.CancelButton = button2; // Add button2 to the form. this.Controls.Add(button2);
KeyPreview
The default value is set to false. This property enables the form to capture all keystrokes before they’re passed to the control that has the focus. Normally, when you press a key, the KeyPress event of the control with the focus is triggered and you can handle the keystroke from within the control’s appropriate handler.
When this property is set to true, the form will receive all KeyPress, KeyDown, and KeyUp events. After the form's event handlers have completed processing the keystroke, the keystroke is then assigned to the control with focus. To handle keyboard events only at the form level and not allow controls to receive keyboard events, set the KeyPressEventArgs.Handled property in your form's KeyPress event handler to true.
Note:
|
To change the KeyPreview property at run time, simply set the property to the desired value.
this.KeyPreview = true;
Visible
The default value is set to true. This property indicates whether the Form is displayed at run time.
To change the Visible property at run time, simply set the property to the desired value.
this.Visible = false;
Non-rectangular Forms
The form does not even have to be a regular rectangle with rounded corners. The form can infact be any shape you like. For example, a round form can be used to display a clock. In order to achieve this effect, you need to do the following steps.
- Set the following properties of the
Formclass.Property Value FormBorderStyleSystem.Windows.Forms.FormBorderStyle.NoneBackColorGreen - Double-click on the form to open the
Form's load event handler. Add the following code:private void Form1_Load(object sender, EventArgs e) { GraphicsPath path = new GraphicsPath(); path.AddEllipse(0, 0, this.Width, this.Height); Region reg = new Region(path); this.Region = reg; }
- Add a button from the toolbox onto the form, change the
Textproperty toClose. Double-click the button to create a button click event handler and add the following code:this.Close();
- Press F5 to run the application and you get a round form.
Setting the Startup Form
To set the startup form of the application to something other than the default form, follow these two simple steps.
- In the Solution Explorer, double-click
Program.csto view the code in the Code Editor. - Locate the
Mainmethod, and change the line that readsApplication.Run(new Form1());
so thatForm1is the name of the form that you you to see when the application first starts.
MSDN references
|


