当用户按下按钮时,如何显示 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 中进行编码,这是否有帮助? :-\
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 vb.net 中,
DateTimePicker
不是像OpenFileDialog、SaveFileDialog、PrintDialog
或其他类似对话框那样的对话框。尽管其外观为“弹出”,但
DateTimePicker
是一个控件对象,因此必须将其放置(添加到)表单或窗口上,然后才能显示和使用。In vb.net, a
DateTimePicker
is not a dialog box like theOpenFileDialog, 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.我最终要做的是制作一个按钮,它会打开一个单独的表单,我将其看起来像一个对话框,并在上面放置了一个 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.将表单添加到您的项目中,并将其设置为日历或日期选择器控件的大小。我使用了日历:
使用它:
Add a form to your project and make it about the size of a calendar or date picker control. I used a calendar:
To use it: