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

发布于 2024-11-10 18:40:19 字数 868 浏览 5 评论 0原文

我试图做到这一点,以便当用户按下按钮时,会显示一个 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 中进行编码,这是否有帮助? :-\

I am trying to make it so when the user pushes a button, a DateTimePicker dialog gets shown, and I would even like to get the resulting date they picked. Here's what I have so far:

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

But when I push the button and run the code, nothing gets displayed. I would think there ought to be a way to simply display this dialog programmatically. Can anyone help me please? :-)

Just FYI, I'm coding this in VB 2010 if that helps any? :-\

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝梦月影 2024-11-17 18:40:19

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

尽管其外观为“弹出”,但 DateTimePicker 是一个控件对象,因此必须将其放置(添加到)表单或窗口上,然后才能显示和使用。

In vb.net, a DateTimePicker is not a dialog box like the OpenFileDialog, SaveFileDialog, PrintDialog or other similar dialogs.

Despite its 'popup' appearance the DateTimePicker is a control object, and as such it must be placed on (added to) a form or window before it can be displayed and used.

流绪微梦 2024-11-17 18:40:19

我最终要做的是制作一个按钮,它会打开一个单独的表单,我将其看起来像一个对话框,并在上面放置了一个 DateTimePicker。这是我能想到的最好的解决方法。

What I wound up doing, is I made a button, which opens up a separate form, which I made to look like a dialog, and put a DateTimePicker on. That was the best workaround I could figure out how to do.

戏舞 2024-11-17 18:40:19

将表单添加到您的项目中,并将其设置为日历或日期选择器控件的大小。我使用了日历:

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

Add a form to your project and make it about the size of a calendar or date picker control. I used a calendar:

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

To use it:

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

frm.Dispose()
'Do something with varDate
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文