Validating issue with ToolStripDropDown

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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()
MyBase.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.Item1.DropDownItems.Add(Me.ControlHost1)

Me.ToolStrip = New Windows.Forms.ToolStrip
Me.ToolStrip.Items.Add(Me.Item1)

Me.ToolStripPanel = New Windows.Forms.ToolStripPanel
Me.ToolStripPanel.Dock = DockStyle.Top
Me.ToolStripPanel.Controls.Add(Me.ToolStrip)

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

Me.Controls.Add(Me.DataInput1)
Me.Controls.Add(Me.InstructionsLabel)
Me.Controls.Add(Me.ToolStripPanel)
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()
MyBase.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

Me.Controls.Add(Me.TextBox)
Me.Controls.Add(Me.Button)

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
System.ComponentModel.CancelEventArgs)
MessageBox.Show(System.String.Format("Validating {0}", Me.Name))
End Sub

Private Sub TextBox_Validated(ByVal sender As Object, ByVal e As
System.EventArgs)
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!
Lance
 
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"
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?Feedba
ckID=117084

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
System.EventArgs)
Me.DataInput2.TextBox_Validating(Me.DataInput2, New
System.ComponentModel.CancelEventArgs())
Me.DataInput2.TextBox_Validated(Me.DataInput2, New EventArgs())
End Sub

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

Public Sub TextBox_Validated(ByVal sender As Object, ByVal e As
System.EventArgs)
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
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

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
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

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