470,634 Members | 2,147 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,634 developers. It's quick & easy.

Validating issue with ToolStripDropDown

I've noticed that controls do not raise a Validating event if they are
contained in a ToolStripDropDown via a ToolStripControlHost item. Please run
the following sample and follow the instructions on the form to reproduce
this issue:


Public Class Form1
Inherits Windows.Forms.Form

Public ToolStripPanel As Windows.Forms.ToolStripPanel
Public ToolStrip As Windows.Forms.ToolStrip
Public Item1 As Windows.Forms.ToolStripMenuItem
Public ControlHost1 As Windows.Forms.ToolStripControlHost

Public InstructionsLabel As Windows.Forms.Label

Public DataInput1 As DataInput
Public DataInput2 As DataInput

Public Sub New()

Me.Size = New Drawing.Size(400, 300)

Me.DataInput1 = New DataInput
Me.DataInput1.Name = "DataInput1"
Me.DataInput1.Dock = DockStyle.Fill
Me.DataInput1.Button.Text = "Button1"

Me.DataInput2 = New DataInput
Me.DataInput2.Name = "DataInput2"
Me.DataInput2.Dock = DockStyle.Fill
Me.DataInput2.Button.Text = "Button2"

Me.ControlHost1 = New Windows.Forms.ToolStripControlHost(Me.DataInput2)

Me.Item1 = New Windows.Forms.ToolStripMenuItem("Item")

Me.ToolStrip = New Windows.Forms.ToolStrip

Me.ToolStripPanel = New Windows.Forms.ToolStripPanel
Me.ToolStripPanel.Dock = DockStyle.Top

Me.InstructionsLabel = New Windows.Forms.Label
Me.InstructionsLabel.Text = "Click inside of the TextBox on this form,
then click Button1. Notice that the Validating event is raised. Now, click
the ToolStrip item to show its DropDown. Next, click inside of the
DropDown's TextBox and then click Button2. Notice that the Validating event
for the DropDown's TextBox is not raised."
Me.InstructionsLabel.Height = 128
Me.InstructionsLabel.Dock = DockStyle.Bottom

End Sub

End Class

Public Class DataInput
Inherits Windows.Forms.Panel

Public TextBox As Windows.Forms.TextBox
Public Button As Windows.Forms.Button

Public Overrides Function GetPreferredSize(ByVal proposedSize As
System.Drawing.Size) As System.Drawing.Size
Dim width As Integer = Me.TextBox.Width
Dim height As Integer = (Me.TextBox.Height + Me.Button.Height)
Return New Drawing.Size(width, height)
End Function

Public Sub New()

Me.TextBox = New Windows.Forms.TextBox

Me.Button = New Windows.Forms.Button
Me.Button.Width = Me.TextBox.Width
Me.Button.Top = Me.TextBox.Bottom


AddHandler Me.TextBox.Validating, AddressOf Me.TextBox_Validating
AddHandler Me.TextBox.Validated, AddressOf Me.TextBox_Validated

Me.Size = Me.GetPreferredSize(Drawing.Size.Empty)
End Sub

Private Sub TextBox_Validating(ByVal sender As Object, ByVal e As
MessageBox.Show(System.String.Format("Validating {0}", Me.Name))
End Sub

Private Sub TextBox_Validated(ByVal sender As Object, ByVal e As
MessageBox.Show(System.String.Format("Validated {0}", Me.Name))
End Sub

End Class


Is this a known issue and, more importantly, are there any workarounds?

Thanks for any help!

Aug 22 '07 #1
1 3547
Hi Lance,

Yes, I can reproduce your problem with the code snippet you provided.

Actually, this is has been reported before due to the behavior of
ToolStrip. Internally, the Validating event is fired during the focus
change between controls. The design rational for ToolStrip is that it will
not grab focus from the controls on the form. If we click textbox or button
in ToolStrip, it will not steal focus from the original control, so no
validating is triggered. For details, please refer to the original reported
issue in the link below:
"Control validation events not fired when toolstrip button is clicked"

However, your scenairo is more complex than the customer in above link,
since you wanted to perform validating over the TextBox in the ToolStrip.
Since the TextBox in the ToolStrip never got focus, calling Form.Validate()
method as suggested in the above link will not work correctly.

I have tried to set the focus programatically to the TextBox in the
ToolStrip, however, it does not work either. Without setting the focus onto
the TextBox, there is no single place(LostFocus) to perform validating, so
I do not think there is any perfect workaround for your scenario. The only
workaround I can think of is calling the TextBox_Validating and
TextBox_Validated methods in all the places you may go away after finished
editing in ToolStrip TextBox. For example, you have to call these 2 methods
in Button2.Click event handler like below:

AddHandler Me.DataInput2.Button.Click, AddressOf Button2_Click
Private Sub Button2_Click(ByVal sender As Object, ByVal e As
Me.DataInput2.TextBox_Validating(Me.DataInput2, New
Me.DataInput2.TextBox_Validated(Me.DataInput2, New EventArgs())
End Sub

Public Sub TextBox_Validating(ByVal sender As Object, ByVal e As
MessageBox.Show(System.String.Format("Validating {0}", Me.Name))
End Sub

Public Sub TextBox_Validated(ByVal sender As Object, ByVal e As
MessageBox.Show(System.String.Format("Validated {0}", Me.Name))
End Sub

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
Get notification to my posts through email? Please refer to

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at

This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 23 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Craig Beuker | last post: by
reply views Thread by Bradley Bossard via DotNetMonster.com | last post: by
1 post views Thread by Visual Systems AB \(Martin Arvidsson\) | last post: by
reply views Thread by Gary Shell | last post: by
reply views Thread by Matt Spinder | last post: by
5 posts views Thread by Ben R. | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.