• home
  • forum
  • my
  • kt
  • download
  • Creating Dialog Boxes in VB.NET

    Author: 2007-09-05 18:18:42 From:

    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
    AcceptButtonOK button instanceCauses form to return value DialogResult.OK. Only used on modal dialog boxes.
    CancelButtonCancel button instanceCauses form to return value DialogResult.Cancel. Only used on modal dialog boxes.
    FormBorderStyleFixedDialogCreate a non-sizable form with no control box on the title bar.
    HelpButtonTrueThe Help button appears in the caption bar next to the Close button. The ControlBox property must be True for these buttons to be visible.
    MaximizeBoxFalseHide the Maximize button in the title bar.
    MinimizeBoxFalseHide the Minimize button in the title bar.
    ShowIconFalseThe title bar icon is not visible in a dialog box.
    ShowInTaskBarFalseDo not indicate the presence of the form on the Windows Task Bar.
    Start PositionCenterParentThe initial position of a dialog box is over its parent form.
    SizeAs NeededThe 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

    relation tutorial

    No relevant information

    Category

      .NET (8)
      Buttons (3)
      Database Related (7)
      Date and Time (1)
      Development (3)
      Error Handling (2)
      File Manipulation (5)
      Introduction to Visual Basic (9)
      Miscellaneous (2)
      Multimedia (9)
      Networking (9)
      Security (1)
      VB Script (6)

    New

    Hot