Creating Your Own Dialog Boxes

function OpenWin(url, w, h) { if(!w) w = 400; if(!h) h = 300; window.open(url, "_new", "width=" + w + ",height=" + h + ",menubar=no,toobar=no,scrollbars=yes", true); } function Print() { window.focus(); if(window.print) { window.print(); window.setTimeout('window.close();',5000); } }
Team-Fly    

Special Edition Using Microsoft® Visual Basic® .NET
By Brian Siler, Jeff Spotts
Table of Contents
Chapter 13.  Using Dialog Boxes


Although the CommonDialog class provides you with several types of dialog boxes to use in your programs, sometimes you just can't accomplish certain tasks with these built-in tools. For example, if you want to set your own captions for the command buttons in a dialog box, you can't use the message box; nor can the message box handle more than three buttons (not including the Help button). Also, you may need to present a dialog box that asks the user several questions at once. If your program needs dialog box capabilities that the Dialog controls can't offer, you can simply use a standard Windows form to create your own custom dialog box.

Visual Basic's Dialog Box Features

Visual Basic .NET provides several features that make it easy to create custom dialog boxes. They include the following:

  • One of the settings for a form's FormBorderStyle property is FixedDialog. This sets up the form so the user cannot resize it.

  • You can use a form's ShowDialog method to cause it to be shown as a modal dialog box. The form is shown modally; that is, no code after the call to the ShowDialog method is executed until the dialog box form is closed.

  • Forms have a DialogResult property that you can use to report how the user closed (answered) the dialog box. Possible values, provided through the DialogResult enumeration, are OK, Cancel, Abort, Retry, Ignore, Yes, and No. Typically, the Click event handler for each of a dialog box's buttons that can close the box will set the form's DialogResult property; doing so will automatically close the dialog box and return control to the calling form.

  • A form that calls a dialog box can examine the dialog box form's DialogResult property to decide how to proceed.

Creating a Custom Dialog Box

The techniques involved in creating a custom dialog box will be easy to demonstrate by adding one to our sample application. We will create a dialog box that will allow the user to change the contents of a variable named sUserName, which will be defined in the main form. Follow these steps:

  1. In the Code window for frmDialogBoxes, add the following variable declaration near the top, just below the "Windows Form Designer generated code" designator:

     Public sUserName As String = "Initial User Name" 

    This creates a String variable named sUserName and sets its initial value to Initial User Name.

  2. Choose Project, Add Windows Form from Visual Basic .NET's menu system.

  3. In the Add New Item dialog box, make sure the Windows Form template is selected. Enter frmTestDialog.vb as the new form's name and click Open.

  4. A new form named frmTestDialog.vb is created and added to the Solution Explorer.

  5. If you do not see frmTestDialog's Designer window, right-click its name in the Solution Explorer and choose View Designer from the pop-up menu.

  6. Change frmTestDialog's FormBorderStyle property to FixedDialog.

  7. Change frmTestDialog's Text property to Custom Dialog Box.

  8. Set the form's ControlBox, MaximizeBox, and MinimizeBox properties to False.

  9. Add a Label control to the form; set its Text property to New User Name: and set its TextAlign property to MiddleRight.

  10. Add a TextBox control to the right of the Label control. Set its Name property to txtUserName and clear its Text property.

  11. Add a Button control to the form. Set its Name property to cmdOK and set its Text property to OK.

  12. Add a second Button control to the form. Set its Name property to cmdCancel and set its Text property to Cancel.

  13. Set the form's AcceptButton property to cmdOK. This specifies that if the user presses Enter to close the dialog box, cmdOK's Click event handler should be invoked.

  14. Set the form's CancelButton property to cmdCancel. This specifies that if the user presses Esc to close the dialog box, cmdCancel's Click event handler should be invoked.

  15. Add the following code to cmdOK's Click event handler:

     Me.DialogResult = DialogResult.OK 

    If the user clicks the OK button, this code sets the form's DialogResult property to OK, and causes the dialog box to close.

  16. Add the following code to cmdCancel's Click event handler:

     Me.DialogResult = DialogResult.Cancel 

    If the user clicks the Cancel button, this code sets the form's DialogResult property to Cancel, and causes the dialog box to close.

  17. Open the Designer window for the main form, frmDialogBoxes.

  18. Add a final Button control to frmDialogBoxes. Set its Name property to cmdCustomDialog and its Text property to Custom Dialog Box.

  19. Add the following code to cmdCustomDialog's Click event handler:

     Dim ThisDlg As New frmTestDialog()  Dim sMsg As String  ThisDlg.ShowDialog()  If ThisDlg.DialogResult = DialogResult.OK Then      sUserName = ThisDlg.txtUserName.Text  End If  sMsg = "The current user name is " & sUserName & "."  MessageBox.Show(sMsg) 

    This code begins by declaring a variable (ThisDlg) to hold an instance of frmTestDialog. After invoking the ShowDialog method of ThisDlg, control will be paused until the user closes the dialog box represented by ThisDlg. After he has done so, the code in this procedure continues to check the value of the DialogResult property of ThisDlg (which was set when the user clicked one of the two buttons). If DialogResult is OK, then the new user name value entered in the dialog box is used to replace the form-level sUserName variable. In any event, the current contents of sUserName are shown via a message box so you can see when it was updated.

Save and run your project, then click the Custom Dialog Box button. Click the dialog box's Cancel button and see that sUserName still has its initial value. Try going back to the custom dialog box and entering a new name, then clicking OK. SUserName is updated with the new name. Experiment with the dialog box to see its behavior.


    Team-Fly    
    Top
     



    Special Edition Using Visual Basic. NET
    Special Edition Using Visual Basic.NET
    ISBN: 078972572X
    EAN: 2147483647
    Year: 2001
    Pages: 198

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net