473,513 Members | 2,417 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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()
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

Aug 22 '07 #1
1 3776
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/Visual...ck.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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Aug 23 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4207
by: Craig Beuker | last post by:
Hello, I am experimenting with this XmlValidatingReader and have a question about how it is working (or not working as would be the case) The sample documents and code are included at the end...
0
1582
by: Bradley Bossard via DotNetMonster.com | last post by:
I am having an issue with the .NET framework (or C#) and validating events. I have implemented several validating event handlers for textboxes on a form. When I run the app, the form works...
1
1317
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have written a simple validating event for a couple of textbox's on a form. They validate just fine when leaving the form, now for the problem. If i click a button to begin execution, i...
0
2424
by: Gary Shell | last post by:
I am experiencing some strange behavior between a UserControl's validating event and a treeview control. Initially, I thought it was related to an issue in the Knowledgebase article 810852...
0
1582
by: Matt Spinder | last post by:
Can I create a ToolStripDropDown that overflows to a second column instead of scrolling?
2
7139
by: josh | last post by:
Hi, I am trying to validate cXML documents against cXML.dtd using the XmlValidatingReader. If I set the XMLValidatingReader's ValidatingType to ValidationType.DTD, I get the following...
5
3452
by: Ben R. | last post by:
Hi, I have a ToolStripDropDownButton and would like to databind a list of menuitems for its associated ToolStripDropDown object. Is this possible? The ToolStripDropDown class doesn't have a...
0
1408
by: MP | last post by:
Hi all, I have: ToolStripDropDown control with Label, TextBox and two Button controls inside it. When TextBox has the focus and I press right Alt key then ToolStripDropDown is closed in the...
2
2665
by: Peted | last post by:
Hi if i derive a reference to a control on a winform (ie Control activeControl = somecontrol on the form) how can i test if that control has a validating or validated event and more importantly...
0
7161
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7539
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1596
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.