Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Using ToolStripControlHost with DateTimePicker
 

Using ToolStripControlHost with DateTimePicker

I ran into a problem todayusing VS 2005 and .NET 2.0 that I hope someone else has seen and can help with. I'm attempting to add DateTimePicker controls to a ToolStrip in the Designer. Since DateTimePicker is not supported by default, I created a new class that inherits from ToolStripControlHost. I applied the ToolStripItemDesignerAvailability attribute to the new class so that it would show up in the list of available ToolStrip items, which works great. However, when I try to add one of those puppies to my ToolStrip, I see it appear briefly and then vanish. I checked the designer-generated code and there's no sign of it.

My class works great if I try to wrap a different kind of control (so far I've tried CheckBox, RadioButton, NumericUpDown, and even MonthCalendar). The wrapped controls appear on the ToolStrip when added through the Designer. Only the DateTimePicker seems to have a problem.

I can create a DateTimePicker wrapped by ToolStripControlHost at run-time and add it to the ToolStrip, and that works fine. I just can't get it to appear and hang around through the Designer.

At the moment my class doesn't do anything more than create the DateTimePicker and wrap it inside the ToolStripControlHost. Any ideas for what might be wrong would be appreciated.

Rex Simmons  Wednesday, December 20, 2006 7:19 PM

Hi,Rex Simmons

Have a look at my sample:

dtpToolStripItem : ToolStripControlHost

{

private FlowLayoutPanel controlPanel;

private DateTimePicker picker = new DateTimePicker();

public dtpToolStripItem()

: base(new FlowLayoutPanel())

{

// Set up the FlowLayouPanel.

controlPanel = (FlowLayoutPanel)base.Control;

controlPanel.BackColor = Color.Transparent;

// Add two child controls.

controlPanel.Controls.Add(picker);

}

public DateTime Value

{

get { return this.picker.Value; }

set { this.picker.Value = value; }

}

protected override void OnSubscribeControlEvents(Control control)

{

base.OnSubscribeControlEvents(control);

//Add your code here to subsribe Control Events

}

protected override void OnUnsubscribeControlEvents(Control control)

{

base.OnUnsubscribeControlEvents(control);

//Add your code here to unsubscribe control events.

}

}

To:Ofer Gal,

To add a numericUpDown control in the menuItem, just replace the datetimepicker in my code to the NumericUpDown control.

Hope it helps.
Best Regards.
Ye

Zhi-Xin Ye  Friday, February 02, 2007 12:19 PM
Can you show me code how to have a numericUpDown in a toolstrip?
Ofer Gal  Thursday, February 01, 2007 7:37 PM

I Have Tested This, It works. The key is overriding Text Property.

Imports System
Imports
System.Windows.Forms
Imports
System.Windows.Forms.Design
Imports
System.ComponentModel
<ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.All)> _

Public
Class DateTimePickerStripx
    Inherits ToolStripControlHost
    Public Sub New()
        MyBase.New(CreateControlInstance)
    End Sub
    Private Shared Function CreateControlInstance() As Control
        Dim dp As DateTimePicker = New DateTimePicker()
        Return dp
    End Function
    Public ReadOnly Property DateTimePicker() As DateTimePicker
        Get
            Return CType(Me.Control, DateTimePicker)
        End Get
    End Property
    <Browsable(
False), EditorBrowsable(EditorBrowsableState.Never)> _
    Public Overrides Property Text() As String
        Get
            Return String.Empty
        End Get
        Set(ByVal value As String)
            MyBase.Text = String.Empty
        End Set
    End Property
End
Class

R.A  Friday, February 02, 2007 12:11 PM

Hi,Rex Simmons

Have a look at my sample:

dtpToolStripItem : ToolStripControlHost

{

private FlowLayoutPanel controlPanel;

private DateTimePicker picker = new DateTimePicker();

public dtpToolStripItem()

: base(new FlowLayoutPanel())

{

// Set up the FlowLayouPanel.

controlPanel = (FlowLayoutPanel)base.Control;

controlPanel.BackColor = Color.Transparent;

// Add two child controls.

controlPanel.Controls.Add(picker);

}

public DateTime Value

{

get { return this.picker.Value; }

set { this.picker.Value = value; }

}

protected override void OnSubscribeControlEvents(Control control)

{

base.OnSubscribeControlEvents(control);

//Add your code here to subsribe Control Events

}

protected override void OnUnsubscribeControlEvents(Control control)

{

base.OnUnsubscribeControlEvents(control);

//Add your code here to unsubscribe control events.

}

}

To:Ofer Gal,

To add a numericUpDown control in the menuItem, just replace the datetimepicker in my code to the NumericUpDown control.

Hope it helps.
Best Regards.
Ye

Zhi-Xin Ye  Friday, February 02, 2007 12:19 PM
Hi there,

Wow - this is awesome. Thanks!

Is there any way to control the display name of the control on the list that drops down on the ToolStrip in the designer? E.g. the ProgressBar item shows up as "ProgressBar", not "ProgressBarToolStripItem". Obviously you can't CALL it "ProgressBar" - the name would conflict with the real progress bar.

So how is this achieved? I've tried looking through Reflector to find some property that controls this but there doesn't appear to be one.
Patrick Sears  Tuesday, February 06, 2007 3:38 AM

Thanks for your post R.A. That's helped me out heaps. Can I suggest the following changes so that the Format property of the DateTimePicker is serialized. I've also added some code to handle the ValueChanged event as this is the main one I want to handle. There are various ways to do this but this is the easiest in this senario...

Code Snippet

<ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.All), DefaultEvent("ValueChanged")> _
Public Class PbsaToolStripDateTimePicker
Inherits ToolStripControlHost

Public Event ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs)

Private WithEvents mDateTimePicker As DateTimePicker

Public Sub New()
MyBase.New(New DateTimePicker)
mDateTimePicker = Me.DateTimePicker
End Sub

<System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Content)> _
Public ReadOnly Property DateTimePicker() As DateTimePicker
Get
Return CType(Me.Control, DateTimePicker)
End Get
End Property

<Browsable(False), EditorBrowsable(EditorBrowsableState.Never)> _
Public Overrides Property Text() As String
Get
Return String.Empty
End Get
Set(ByVal value As String)
MyBase.Text = String.Empty
End Set
End Property

Private Sub mDateTimePicker_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles mDateTimePicker.ValueChanged
RaiseEvent ValueChanged(sender, e)
End Sub

End Class

avalpied  Tuesday, April 24, 2007 1:51 PM
That worked great for adding the Date-Time-Picker to the Toolbar, but now how do I retrieve the date selected by the user since the text property has been overridden?
DeepMaroon  Monday, July 13, 2009 8:06 PM

Code Snippet

<ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.All), DefaultEvent("ValueChanged")> _
Public Class PbsaToolStripDateTimePicker
Inherits ToolStripControlHost

Public Event ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs)

Private WithEvents mDateTimePicker As DateTimePicker

Public Sub New()
MyBase.New(New DateTimePicker)
mDateTimePicker = Me.DateTimePicker
End Sub

<System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Content)> _
Public ReadOnly Property DateTimePicker() As DateTimePicker
Get
Return CType(Me.Control, DateTimePicker)
End Get
End Property

Private Sub mDateTimePicker_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles mDateTimePicker.ValueChanged
RaiseEvent ValueChanged(sender, e)
End Sub

End Class

I removed the Override Text property above that still lets me use the text property of the control (i.e. the user selected time-date value). However after a few runs I get this error:

Contructor on type 'System.Windows.Forms.ToolStripControlHost' Not Found.

Is there another way to do this? Referencing this link:

http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/4d496476-5493-42fe-8fff-b0b6006394c3/

I've tried creating my own ToolStripControlHost But that has failed miserably:
Imports System
Imports System.Windows.Forms
Imports System.Windows.Forms.Design
Imports System.ComponentModel


Public Class MyToolStripControlHost
    Inherits ToolStripControlHost

    Private WithEvents mToolStripControlHost As ToolStripControlHost

    Public Sub New()
        MyBase.New(New ToolStripControlHost)
        mToolStripControlHost = Me.ToolStripControlHost
    End Sub

    Public ReadOnly Property ToolStripControlHost() As ToolStripControlHost
        Get
            Return CType(Me.Control, ToolStripControlHost)
        End Get
    End Property
First, a Tool StripControlHost is not a Control so this Fails:
Return CType(Me.Control, ToolStripControlHost)

Secondly, this is overloaded and isn't possible:


    MyBase.New(New ToolStripControlHost)

Is there another way to go on this?







DeepMaroon  Tuesday, July 21, 2009 7:34 PM

You can use google to search for other answers

Custom Search

More Threads

• Implementing IDesignerSerializationService
• DesignerActionList invoking template editing
• Question: Do you want to set the Text property? on drop of a control..
• How to favour code over designer
• Differents types of ListBox Control
• Combbox Stop Manual Data Entry
• Changing culture problem
• UserControl KeyPress event
• Combobox Help to Bind 3 Columns from database
• Generating BeginInit() & EndInit()