2

I know I can add a DateTimePicker to my MenuStrip with the following lines

Dim dp = New ToolStripControlHost(New DateTimePicker)
MenuStrip1.Items.Add(dp)  

But I can't figure out how to add a DateTimePicker to the MenuStrip at designtime. What's the trick behind it? I have been trying and searching for like an hour and I am about to give up even though I know there has to be a way!

TL;DR
How do I add a DateTimePicker to my MenuStrip at design-time? Alternatively we can add it to a ToolStrip instead.

Luke
  • 751
  • 2
  • 7
  • 32

2 Answers2

4

You are close to a solution in using the ToolStripControlHost, but you will need to derive from that class as shown in the linked-to example. The frustrating thing with that example is that it does not decorate the derived class with the System.Windows.Forms.Design.ToolStripItemDesignerAvailabilityAttribute to make it available on the design surface.

The following is a minimalist implementation to get a working example. You may need to override the automatic sizing to suit your needs/wants for the control. The implementation overrides the Text property to prevent designer from assigning invalid text to the underlying DateTimerPicker control.

<System.Windows.Forms.Design.ToolStripItemDesignerAvailability(
    System.Windows.Forms.Design.ToolStripItemDesignerAvailability.ToolStrip _
    Or System.Windows.Forms.Design.ToolStripItemDesignerAvailability.StatusStrip _
    Or System.Windows.Forms.Design.ToolStripItemDesignerAvailability.MenuStrip)> _
Public Class TSDatePicker : Inherits ToolStripControlHost
    Public Sub New()
        MyBase.New(New System.Windows.Forms.DateTimePicker())
    End Sub

    Public ReadOnly Property ExposedControl() As DateTimePicker
        Get
            Return CType(Control, DateTimePicker)
        End Get
    End Property

    <Browsable(False), EditorBrowsable(EditorBrowsableState.Advanced), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
    Public Overrides Property Text As String
        Get
            Return ExposedControl.Text
        End Get
        Set(value As String)
            ' verify valid date
            Dim dt As DateTime
            If DateTime.TryParse(value, dt) Then
                ExposedControl.Text = value
            End If
        End Set
    End Property
End Class
TnTinMn
  • 11,522
  • 3
  • 18
  • 39
  • [This article](https://msdn.microsoft.com/en-us/library/ms171822.aspx) may help too – djv Apr 18 '17 at 16:00
  • I am pretty sure that's what I need and I am almost sorry for asking, but how do I get it into my Toolbox, do actually drag it onto my form? – Luke Apr 19 '17 at 06:42
  • 1
    @Luke, These types of controls are made available in the toolstrip's visual designer "add item" drop-down button and also are selectable from the ToolStrip.Items collection editor in the property grid. AFAIK, they can not be placed in the ToolBox. – TnTinMn Apr 19 '17 at 14:14
  • Shame on me, I was sure i looked at that multiple times yesterday. May be I had to restart Visual Studio. But it's perfectly working for me. Thanks! All attributes are avaliable using the "ExposedControl" Attribute, which refers to the original DateTimePicker. Only the events have to be assigned at runtime using Addhandler TSDatePicker.ExposedControl.EVENTNAME – Luke Apr 19 '17 at 14:34
  • Okay, I have to correct myself. changes to the exposed Control Attributes need to be done programmatically, because they are lost as soon as you compile. – Luke Apr 19 '17 at 14:36
  • @Luke, when defining _In-Project_ controls, you need to perform a build operation for them to become available with the most recent code changes. – TnTinMn Apr 19 '17 at 14:40
  • @luke, The 'ExposedControl` is a quick and dirty way to gain access to the underlying control. As I stated in the answer, this is a minimal example and you should follow the techniques shown in the [ToolStripControlHost Class-Example](https://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripcontrolhost(v=vs.110).aspx#Examples) to expose the underlying control's properties/events. And Yes, it is a pain to do so. – TnTinMn Apr 19 '17 at 14:46
-1

Was going to add as a comment but I trust it justifies an answer.

The only way I have succeeded in this is to add one at design time (to the form) and set Visible to False and use the menu item to set Visible to True (may also need to set the position and/or bring it to the front).

You do need to manually handle setting Visible to False again.

OSKM
  • 728
  • 14
  • 25
  • I think you misunderstood the question. I am not talking about a ContextMenuStrip (Right click menu) nether i want it to be invisible at any point. I am talking about a MenuStrip, which you can found on top of most applications that don't ribbons. – Luke Apr 18 '17 at 11:57
  • So you are wanting to have the `DateTimePicker` permanently visible? – OSKM Apr 18 '17 at 12:03
  • Visibility is no topic of my question. I need a MenuStripDateTimePicker CustomControl. – Luke Apr 18 '17 at 12:05
  • @Luke Sorry - should have tried your code sample above first - you can imitate the effect of your code above by simply placing a `DateTimePicker` in the place you want it at design time and select "Bring to Front" it will appear in line with your Menu Strip but other than that I do not know of a way to make it part of the Menu Strip - you are right though it ought to be easy! – OSKM Apr 18 '17 at 12:12
  • Already tried putting a DateTimePicker onto the MenuStrip and set it Anchor Top Right. Worked as long as i did not resize the window. Because when I did it overlayed the other Items. The DateTime Picker needs to be part of the menustrip. Thanks for your attempts tho :-) – Luke Apr 18 '17 at 12:50
  • I also found [this](http://stackoverflow.com/questions/12694646/winforms-adding-custom-toolstripmenuitem-to-menustrip-in-designer) but have run out of time to research how to do it. – OSKM Apr 18 '17 at 13:33