I remember the first time I needed to create a dialog box in a .NET application that I was writing in C#. Being a long-time Visual Basic programmer, I assumed that this could easily be accomplished by using a dialog box template included with Visual Studio.NET. To my surprise, no such form template existed for C#, although one does for Visual Basic 2005. After wading through several books and Web pages filled with information on Windows Forms 2.0 programming, a basic set of steps became apparent to me for manually converting a .NET form into a Windows dialog box:
1. Add a Form to your .NET project and name it ¡°DialogBoxForm¡±.
2. Drop two buttons in the lower right-hand area of the Form and name them ¡°OKButton¡± and ¡°CancelButton¡±.
3. Change the following properties of the Form to adjust its appearance and behavior to be like a standard dialog box:
| Property | Value | Description |
|---|---|---|
| AcceptButton | OK button instance | Causes form to return value DialogResult.OK. Only used on modal dialog boxes. |
| CancelButton | Cancel button instance | Causes form to return value DialogResult.Cancel. Only used on modal dialog boxes. |
| FormBorderStyle | FixedDialog | Create a non-sizable form with no control box on the title bar. |
| HelpButton | True | The Help button appears in the caption bar next to the Close button. The ControlBox property must be True for these buttons to be visible. |
| MaximizeBox | False | Hide the Maximize button in the title bar. |
| MinimizeBox | False | Hide the Minimize button in the title bar. |
| ShowIcon | False | The title bar icon is not visible in a dialog box. |
| ShowInTaskBar | False | Do not indicate the presence of the form on the Windows Task Bar. |
| Start Position | CenterParent | The initial position of a dialog box is over its parent form. |
| Size | As Needed | The fixed size needed for the dialog box. |
These properties can be set using the Properties window for the form, or using code placed in the Form¡¯s Load event:
Me.AcceptButton = OKButton Me.CancelButton = CancelButton Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedDialog Me.HelpButton = True Me.MaximizeBox = False Me.MinimizeBox = False Me.ShowInTaskbar = False Me.ShowIcon = False Me.StartPosition = FormStartPosition.CenterParent
4. Add the following button click event handlers to the Form:
Private Sub OKButton_Click(ByVal sender As Object, _ ByVal e As EventArgs) ¡® User clicked the OK button Me.DialogResult = Windows.Forms.DialogResult.OK End Sub Private Sub CancelButton_Click(ByVal sender As Object, _ ByVal e As EventArgs) ¡® User clicked the Cancel button Me.DialogResult = Windows.Forms.DialogResult.Cancel End Sub
5. Add properties that you need to move data into and out of the dialog box as you would for any Form:
Private _LoginName As String Private _LoginPassword As String Public Property LoginName() As String Get Return _LoginName End Get Set(ByVal value As String) _LoginName = value End Set End Property Public Property LoginPassword() As String Get Return _LoginPassword End Get Set(ByVal value As String) _LoginPassword = value End Set End Property
6. Show the dialog box modally by calling the ShowDialog() of the form:
Public Sub ShowDialogBox() Dim dialog As New DialogBoxForm dialog.LoginName = ¡°JDMurray¡± dialog.LoginPassword = String.Empty If dialog.ShowDialog() = Windows.Forms.DialogResult.OK Then Debug.WriteLine(¡°Login Name: ¡° & dialog.LoginName) Debug.WriteLine(¡°Password: ¡° & dialog.LoginPassword) Else ¡® User clicked the Cancel button End If End Sub
7. To show the dialog box modelessly, call the Show() method of DialogBoxForm instead. You will need to add an event handler to the Close event of DialogBoxForm to know when the user closes the dialog box:
Public Sub ShowDialogBox() Dim dialog As DialogBoxForm = New DialogBoxForm dialog.LoginName = ¡°JDMurray¡± dialog.Password = String.Empty AddHandler dialog.FormClosed, AddressOf dialog_FormClosed dialog.Show() ¡® The Show() method returns immediately End Sub Private Sub dialog_FormClosed(ByVal sender As Object, _ ByVal e As FormClosedEventArgs) ¡® This method is called when the user closes the dialog box End Sub
discuss this topic to forum
