Other Functionality

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
Exam Prep. Guides
70-505 Study Guide
70-526 Study Guide

1. Forms Controls

2. Integrating Data
3. Printing/Reporting
4. Enhancing Usability
5. Asynchronous Prog.
6. Forms Controls
7. Configure/Deploy

edit
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:


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:
If a form has no visible or enabled controls, it automatically receives all keyboard events.

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.

  1. Set the following properties of the Form class.
    Property Value
    FormBorderStyle System.Windows.Forms.FormBorderStyle.None
    BackColor Green
  2. 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;
    }
  3. Add a button from the toolbox onto the form, change the Text property to Close. Double-click the button to create a button click event handler and add the following code:
    this.Close();
  4. Press F5 to run the application and you get a round form.

Image:526-30.jpg


Setting the Startup Form

To set the startup form of the application to something other than the default form, follow these two simple steps.

  1. In the Solution Explorer, double-click Program.cs to view the code in the Code Editor.
  2. Locate the Main method, and change the line that reads
    Application.Run(new Form1());
    so that Form1 is the name of the form that you you to see when the application first starts.


MSDN references


Previous_Page_.gif Next_Page_.gif
© 2007-2008 Mike Kitchen

Personal tools