I need to have a pop-up dialog like the Color Dialog or Save Dialog but for choosing a date from a calendar. The DateTimePicker is what I need, but I don't know how to launch this like a pop-up dialog in C#.
A:
The DateTimePicker
is a Control
, not a Form
. You'll have to create your own Form
and add the control to it; there is not a standard dialog for selecting dates.
Adam Robinson
2010-03-15 18:57:13
That's what I was afraid of, but is there any way to make a dialog?
James
2010-03-15 18:59:53
There is no way in win forms other than Form
anishmarokey
2010-03-15 19:09:42
@James: Like I said, you'll just need to create another `Form`, add the control (and any properties you'll need; I'd assume you'd need one to get/set the value displayed), then instantiate it and call `ShowDialog`. You'll probably also want to set the `FormBorderStyle` to `FixedDialog`.
Adam Robinson
2010-03-15 19:11:42
Perfect, thanks!
James
2010-03-15 19:23:41
A:
You need to add a DateTimePicker to a form and show the form as a dialog:
var picker = new DateTimePicker();
Form f = new Form();
f.Controls.Add(picker);
var result = f.ShowDialog();
if(result == DialogResult.OK)
{
//get selected date
}
Lee
2010-03-15 19:10:26