Understanding the Form Class – using C#


This article has been excerpted from book "The Complete Visual C# Programmer's Guide from the Authors of C# Corner".

The main window of your Windows application is a form, derived from the ContainerControl class. A form is a representation of a window in .NET. You can use the Form class to create Single Document Interface (SDI), Multiple Document Interface (MDI), and dialog-based applications. The windows can be borderless, transparent, and floating. You can create an MDI form by setting the IsMDIContainer property to true. To make a form an MDI child, set the MdiParent property with a reference to the MDI container form. To display the child, the child form's Show method must be called or its Visible property set to true.

You can change the appearance, size, and color of a form by using the Properties window. You can use the Form class members to set properties or override event handler methods at runtime. The Form class contains properties and methods too numerous to completely itemize. Among the interesting ones are the following:

  • ControlBox. A Boolean property that toggles the visibility of the minimize, maximize, and close buttons on the caption bar. The default value, true, displays the buttons. The buttons can be set individually, but only as to their being enabled or disabled.
  • KeyPreview. A Boolean property that permits a form to intercept all keystrokes prior to the control that has focus. To implement this property, either a KeyPress, a KeyDown, or a KeyUp event handler must also be attached to the form. To prevent a text box from receiving the event, set the KeyXXXEventArgs.Handled property to true.
  • ShowInTaskbar. A property that shows a window's caption bar text in the taskbar when set to true. The default value is true.
  • WndProc. The .Net version of the Win32 default window procedure. The WndProc method has a Message structure as a parameter and returns void. A window procedure handles operating system notifications that an action, such as a mouse move, has taken place. Each window message, or notification, has a default behavior that is applied by the default window procedure if no action is taken by any of the previously called procedures in the window chain.

Table 9.6 describes some other members of the Form class.

table-9.6.gif

Table 9.6: Form Class Members

Using the properties and methods of the Form class you can alter the appearance of the form.Listing 9.6 contains some samples that can change the appearance and functionality of the form.

Listing 9.6: Changing the appearance of the form


// Set the font of the form.

form1.Font = new System.Drawing.Font ("Verdana", 10,
System.Drawing.FontStyle.Bold);


// Make the Form transparent.

form1.Opacity = 0.50;


// Make the Form topmost.

form1.TopMost = true;


// Set the title bar text of the form.

form1.Text = "My Dialog Box";


// Display a help button on the form.

form1.HelpButton = true;


// Define the border style of the form to that of a dialog box.

form1.FormBorderStyle = FormBorderStyle.FixedDialog;


// Set the MaximizeBox to false to remove the maximize box.

form1.MaximizeBox = false;


// Display the form as a modal dialog box.

form1.ShowDialog();


Conclusion

Hope this article would have helped you in understanding the Form Class – using C#. See other articles on the website on .NET and C#.

visual C-sharp.jpg The Complete Visual C# Programmer's Guide covers most of the major components that make up C# and the .net environment. The book is geared toward the intermediate programmer, but contains enough material to satisfy the advanced developer.

Up Next
    Ebook Download
    View all
    Learn
    View all