当用户按下按钮时(无需将控件添加到窗体),如何显示DateTimePicker对话框)

时间:2011-05-28 00:00:04

标签: vb.net datetimepicker

我试图这样做,当用户按下按钮时,会显示一个DateTimePicker对话框,我甚至想得到他们选择的结果日期。这是我到目前为止所做的:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim Dis As System.Windows.Forms.DateTimePicker = New System.Windows.Forms.DateTimePicker()

    ' Set the MinDate and MaxDate.
    Dis.MinDate = New DateTime(1985, 6, 20)
    Dis.MaxDate = DateTime.Today

    ' Set the CustomFormat string.
    Dis.CustomFormat = "MMMM dd, yyyy - dddd"
    Dis.Format = Windows.Forms.DateTimePickerFormat.Custom

    ' Show the CheckBox and display the control as an up-down control.
    Dis.ShowCheckBox = True
    Dis.ShowUpDown = True

    Dis.Show()

End Sub

但是当我按下按钮并运行代码时,没有任何内容显示出来。我认为应该有一种简单地以编程方式显示此对话框的方法。有人可以帮我吗? : - )

仅供参考,我在VB 2010中对此进行编码,如果这有帮助吗? : - \

3 个答案:

答案 0 :(得分:5)

在vb.net中,DateTimePicker不是OpenFileDialog, SaveFileDialog, PrintDialog或其他类似对话框的对话框。

尽管它的'弹出'外观,DateTimePicker是一个控制对象,因此它必须放在(添加到)窗体或窗口之后才能显示和使用。

答案 1 :(得分:0)

我做了什么,是我做了一个按钮,打开了一个单独的表单,我看起来像一个对话框,并打开DateTimePicker。这是我能想出怎样做的最好的解决方法。

答案 2 :(得分:0)

向您的项目添加一个表单,使其与日历或日期选择器控件的大小相同。我使用了日历:

Public Class frmDatePicker
    Private _Selected As Date
    Public Property Selected As Date
        Get
            Return _Selected
        End Get
        Set(value As Date)
            _Selected = value
        End Set
    End Property


Private Sub calPickDate_DateChanged(sender As Object, e As DateRangeEventArgs) Handles calPickDate.DateChanged
    Selected = calPickDate.SelectionStart

End Sub    
End Class

要使用它:

Dim varDate As String = ""
Dim frm As frmDatePicker = New frmDatePicker()
frm.ShowDialog()
varDate = frm.Selected.ToString()

frm.Dispose()
'Do something with varDate