Home  
  Microsoft  
  Cisco  
  CompTIA  
  CWNP  
  InfoSecurity  
  Forums  
  Blogs  
  Topsites  
  Search the Web  
 
 
     
  Subnet Calculator  
  Online Degrees  
  Exam Vouchers  
  Tell-A-Friend  
  Contact us  
  About us  
  Support us  
  Advertise  

   
     

 

  TechExams Blogs Login  

« Errata for Microsoft Press MCTS Self-Paced Training Kit Books Synchronizing the Time on Windows XP and Vista with the Internet »

Creating Dialog Boxes in .NET

June 12th, 2007 by James D. Murray

Microsoft Certification Exam: 70-526 (MCTS)
Objective: Create and use custom dialog boxes in Windows Forms applications.
Language: Visual Basic 2005 (click here for the C# version of this entry)

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
Views: 9,765 | Tags: , , Print This Post Print This Post

4 Responses to “Creating Dialog Boxes in .NET”

  1. Ada Says:

    Hi James

    This info is a great resource. Could you pls also provide the hyperlink to the C# version of the code you provided.

    Thanks,
    Ada

  2. Roie Says:

    Excellent info!!

    I’ve been looking for a whole day now how to make new a dialog box and this is exactly what I need.

    thank you.

  3. BlueBeetle Says:

    I was trying to figure out how to make a dialog box in .NET until I found this site. Very useful and beautifully presented! Thanks!

  4. himu Says:

    thank you for the valuable concise information

Leave a Reply



Exchange 2003 Video Training




 
Featured Sponsors

TrainSignal - “Hands On” computer training for IT professionals. Network+ Training, MCSE, Cisco & more! Visit Train Signal’s free training site to get loads of Free Computer Training, videos, articles and practice exams.

The CWNP® Program - the industry standard for vendor neutral wireless LAN training and certification. Career certifications in WLAN administration, WLAN security, WLAN analysis, and CWNE for wireless LAN experts. Learn more about the CWNP Program. Find a class near you.
 

All images and text are copyright protected, violations of these rights will be prosecuted to the full extent of the law.
2002-2008 TechExams.Net | Advertise | Disclaimer