Since only some standard items (textBox etc.) can be added to a ToolStrip control at design time in a Winform app, I've added a DateTimePicker control dynamically as follows but I'm not sure how to access the datetime value of the selected date in the control
var datePicker = new ToolStripControlHost(new DateTimePicker());
toolStrip1.Items.Add(datePicker);
I've tried the following that still works as above but can't get access to the selected date value of the dateTimePicker added:
DateTimePicker myDateTimePicker = new DateTimePicker();
var datePicker = new ToolStripControlHost(myDateTimePicker);
toolStrip1.Items.Add(datePicker);
This MSDN tutorial describes how to add items dynamically to ToolStrip but nothing else.
You can handle
ValueChangedevent of theDateTimePickerthis way:Also as another option, you can find the
DateTimePickercontrol in another button click event handler this way:Note: A better solution would be deriving from
ToolStripControlHost. This way you can expose properties and events which you need. For example, you can exposeValueproperty andValueChangedevent of embeddedDateTimePickercontrol.The MSDN example How to: Wrap a Windows Forms Control with ToolStripControlHost shows you how to create a custom control to host in
ToolStrip. You can simply use the example to create your customToolStripDateTimePickercontrol.