RSS

Custom Dialog Box example : ChooseDateDialog

05 Apr

Here’s a complete example on how to show dialog box and retrieve user input. More details on Dialog Box from Dialog Boxes in Windows Forms. The ChooseDateDialog is a simple custom dialog box which is used to prompt for a date. Its functionality is like this:

First, use a simple button to call ChooseDateDialog:


The ChooseDateDialog consists of a DateTimePicker and OK/Cancel buttons:

When OK button is pressed, the parent form retrieve the chosen date value:

The code of ChooseDateDialog is as follow:

Imports System.Windows.Forms

Public Class ChooseDateDialog
    'Use public property to return user input value to parent form
    'The date chosen by user using DateTimePicker
    Public ReadOnly Property ChosenDate() As Date
        Get
            Return Me.DateTimePicker1.Value
        End Get
    End Property

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

End Class

The code below is from the parent form which calls ChooseDateDialog.

Public Class DialogTest

    Private Sub ButtonChooseDate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonChooseDate.Click
        Dim chooseDateDialog As New ChooseDateDialog
        chooseDateDialog.ShowDialog()

        If chooseDateDialog.DialogResult = Windows.Forms.DialogResult.OK Then
            'When user press OK button
            'Retrieve the chosen date from Dialog Box
            Me.Label1.Text = chooseDateDialog.ChosenDate.ToString
        End If

        chooseDateDialog.Dispose()
    End Sub

End Class
Advertisements
 
Leave a comment

Posted by on April 5, 2012 in .NET, VB, Winform

 

Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: