Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
programming4us
Windows 7

Visual Studio 2010 : Building the Windows Container (part 1) - Setting common form properties & Creating dialog boxes

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
7/2/2011 4:04:27 PM
As the container that holds your controls, the Windows Form is one of the most important elements in building Windows applications. The Windows Form is your application's foundation, and it's highly customizable.

The following sections explore some properties and controls that you can use to modify how your Windows Forms look and behave.

1. Setting common form properties

You can probably spot a typical Windows Form from ten paces:

  • It usually has the familiar little red Close button in its upper-right corner.

  • If it's like most windows, it has menus and toolbars.

  • It's displayed on the taskbar along the bottom of the screen.

All these features of a typical window are determined by properties. Simply turning a property on or off can give a Windows Form a complete makeover. The default Windows Form that Visual Studio creates in all new Windows projects has most of these typical properties. You have to set some properties, such as menus, on your own.

Table 1 describes some common Windows Forms properties, sorted by property group.

Table 1. Common Windows Forms Properties
PropertyWhat It Does
FormBorderStyleDetermines whether the window is resizable
TextSets the caption that appears on the form's title bar
ContextMenuStripSets the shortcut menu that's displayed when a user right-clicks the form
NameSets the identifier used to access the form in code
StartPositionDetermines where the form appears when it's first opened
AcceptButtonSets the default Enter button
CancelButtonSets the default Esc button
ControlBoxToggles the form's minimize, maximize, and close controls on and off
IsMdiContainerDetermines whether the form is a parent container in a Multiple Document Interface (MDI) application
IconSets the icon displayed on the title bar and the taskbar when the form is minimized
ShowInTaskBarShows the form on the Windows taskbar
MainMenuStripSets the form's menu control

You set these properties by using the Properties window.

2. Creating dialog boxes

A dialog box is a Windows Form with attitude. The dialog box pops up on top of forms that are already open and refuses to leave until the user responds. Many Windows applications use dialog boxes to get a user's attention.

Most dialog boxes are modal, which means that they must be closed or hidden before users can continue working with other windows. Modeless forms can remain open alongside other windows. Modeless dialog boxes are harder to manage because you have to keep track of the different windows a user works with. Visual Studio provides a modal dialog form that you can add to your application.

To add a modal dialog box to an existing Windows project, follow these steps:

  1. Press Ctrl+Shift+A to open the Add New Item window.

  2. Select the Dialog template.

  3. Enter a name for the dialog box, such as MyDialog.

  4. Click Add.

    Visual Studio adds the dialog box to your project.

NOTE

The difference between a modal and a modeless dialog box is the method with which you invoke it. For modal dialog boxes, you use the ShowDialog method after the dialog object has been created. For modeless dialogs, you use the Show method after the dialog object has been created.

The dialog box is just a regular Windows Form with the modified properties shown in Table 2.

Table 2. Modified Properties
PropertySets To
AcceptButtonThe form's OK button
CancelButtonThe form's Cancel button
FormBorderStyleFixedDialog
MinimizeBoxFalse
MaximizeBoxFalse
ShowInTaskbarFalse
StartPositionCenterParent

The dialog box also has an OK button and a Cancel button. The Cancel button's DialogResult property is set to Cancel. The DialogResult property returns the selected value to the parent form of the dialog box when the button is clicked.

To use the dialog box, follow these steps:

  1. Add a label and a text box to the dialog box you create in the preceding set of steps; set the Text property of the text box to txtFromDialog and set the Modifiers property to Public in the Properties view.

  2. Set the OK button's DialogResult property to OK.

    When the user clicks the OK button in the dialog box, the value set in the DialogResult property is sent to the parent form.

  3. Add a label, text box, and button to the project's parent form.

  4. If no other forms exist in the project, press Ctrl+Shift+A to open the Add New Items window and add a Windows Form.

  5. Double-click the button you created in Step 3 to access the button's Click event.

    The Code Editor appears.

  6. In the button's Click event, type this code:

    • VB

      Dim dlg As New MyDialog

      If dlg.ShowDialog() = DialogResult.OK Then
      Me.TextBox1.Text = dlg.txtFromDialog.Text
      End If

    • C#

      MyDialog dlg = new MyDialog();

      if (dlg.ShowDialog() == DialogResult.OK)
      {
      this.textBox1.Text = dlg.txtFromDialog.Text;
      }

    The first two lines of code open the dialog box you create in the preceding set of steps. The remaining four lines test the DialogResult property of the dialog box and set the Text property on the text box of the parent form.

  7. Press Ctrl+F5 to run the application.

To test the sapplication, follow these steps:

  1. On the parent form, click the button that launches the dialog box.

    The dialog box appears.

  2. In the dialog box, type Some Text in the text box and then click OK.

    The dialog box closes.

  3. The phrase Some Text appears in the text box in the parent form.

    Figure 1 shows you the parent form with the dialog box open.

Figure 1. Text typed in the dialog box appears in the parent form.


Visual Studio has a number of preconfigured dialog-box components that you can use in your applications. You work with the dialog-box components in the Visual Studio toolbox by using code (unlike working with the dialog box you create in this section). The preconfigured dialog boxes create a consistent way for you to provide access to common features, such as printing and opening files. Table 3 lists the preconfigured dialog boxes.

Table 3. Preconfigured Dialog Boxes
Toolbox TabDialog BoxWhat It Does
DialogsColorDialogDisplays a color palette and controls for selecting a color
 FolderBrowserDialogPrompts user to select a folder
 FontDialogPrompts user to select a font
 OpenFileDialogPrompts user to open a file
 SaveFileDialogPrompts user to save a file
PrintingPrintDialogPrompts user to select a printer and configure settings
 PageSetupDialogPrompts user to change page-related settings
 PrintPreviewDialogPreviews the document to be printed

Use the preconfigured dialog boxes, rather than create your own. The steps for using a preconfigured dialog box vary slightly because each dialog box has its own set of properties you must set.


To use the ColorDialog dialog box, follow these steps:

  1. Create a new Windows Form.

  2. Drag and drop a text box and button onto the form.

  3. Drag and drop a ColorDialog control onto the form from the Dialogs tab of the toolbox.

    The ColorDialog appears at the bottom of the Windows Forms Designer, rather than on the Windows Form.

  4. Set the ColorDialog control's Name property to MyColorDialog.

  5. Double-click the button on the Windows Form.

    The Code Editor appears.

  6. Type the following code in the Code Editor:

    • VB

      If MyColorDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
      txtSelectedColor.BackColor = MyColorDialog.Color
      End If

    • C#

      if( MyColorDialog.ShowDialog() == DialogResult.OK )
      {
      txtSelectedColor.BackColor = MyColorDialog.Color;
      }

    Use the ShowDialog() method to display preconfigured dialog boxes.


  7. Press Ctrl+F5 to run the application.

  8. Click the button on the form to launch the ColorDialog control.

    The ColorDialog appears, as shown in Figure 2.

  9. Select a color and then click OK.

    The background color of the text box changes.

Figure 2. This control is a preconfigured dialog box.
Other -----------------
- Microsoft Visio 2010 : Adding Labels to Flowcharts & Understanding Swimlane Diagrams
- Microsoft Visio 2010 : Selecting a Flowchart Type & Creating Flowcharts
- Automating the Windows 7 Installation : Choosing Automated Deployment Options (part 3) - An Overview of the System Preparation Tool and Disk Imaging
- Automating the Windows 7 Installation : Choosing Automated Deployment Options (part 2)
- Automating the Windows 7 Installation : Choosing Automated Deployment Options (part 1) - An Overview of the Microsoft Deployment Toolkit 2010
- Advanced .NET Framework with VB 2010 : Coding Attributes - Reflecting Attributes
- Advanced .NET Framework with VB 2010 : Coding Attributes - Coding Custom Attributes
- Advanced .NET Framework with VB 2010 : Coding Attributes - Applying Attributes
- Microsoft Visio 2010 : Adding Sophistication to Your Drawings - Working with Background Pages and Borders
- Microsoft Visio 2010 : Adding Sophistication to Your Drawings - Managing Pages and Page Setup
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us
Popular tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 windows Phone 7 windows Phone 8
programming4us programming4us
 
programming4us
Natural Miscarriage
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Game Trailer