Microsoft KB Archive/943456

= Cancelling TabControl Validating event may cause ComboBox malfunction =

Article ID: 943456

Article Last Modified on 12/3/2007

-

APPLIES TO


 * Microsoft .NET Framework 2.0

-



Source: Microsoft Support



RAPID PUBLISHING
RAPID PUBLISHING ARTICLES PROVIDE INFORMATION DIRECTLY FROM WITHIN THE MICROSOFT SUPPORT ORGANIZATION. THE INFORMATION CONTAINED HEREIN IS CREATED IN RESPONSE TO EMERGING OR UNIQUE TOPICS, OR IS INTENDED SUPPLEMENT OTHER KNOWLEDGE BASE INFORMATION.



Action
A Winforms application contains a TabControl with two or more tabs. In the first TabPage's Validating event handler, a MessageBox is displayed and e.Cancel is set to True to keep the focus on the same tab.



Result
After dismissing the MessageBox, if the user tries to drop down the ComboBox dropdown, it will not stay dropped down and will go back to its original state.



Cause
Displaying a MessageBox inside a TabPage's Validating event and setting e.Cancel to true might cause unexpected behavior with the controls on that TabPage.



Resolution
If it is necessary to display a MessageBox inside a TabPage's Validating event handler, do not set e.Cancel. Instead, add a Deselecting handler on the tab control and cancel deselection for tab pages which failed validation. Also, add a FormClosing handler on the form and cancel closing if validation failed.



MoreInformation
Here is code which demonstrates this solution:

Private cancel As Boolean

Private Sub TabPage1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TabPage1.Validating, TabPage4.Validating

If MessageBox.Show(&quot;save the changes&quot;, &quot;save&quot;, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Cancel Then

cancel = True

Else

cancel = False

End If

End Sub

Private Sub TabControl1_Deselecting(ByVal sender As Object, ByVal e As TabControlCancelEventArgs) Handles TabControl1.Deselecting

Dim tc As TabControl

tc = sender

If (tc.SelectedIndex <> 0) Then

Return

End If

e.Cancel = cancel

cancel = False

End Sub

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing

If (Me.TabControl1.SelectedIndex <> 0) Then

Return

End If

e.Cancel = cancel

cancel = False

End Sub



DISCLAIMER
MICROSOFT CORPORATION AND/OR ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY, RELIABILITY, OR ACCURACY OF THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN. ALL SUCH INFORMATION AND RELATED GRAPHICS ARE PROVIDED &quot;AS IS&quot; WITHOUT WARRANTY OF ANY KIND. MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS HEREBY DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THIS INFORMATION AND RELATED GRAPHICS, INCLUDING ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, WORKMANLIKE EFFORT, TITLE AND NON-INFRINGEMENT. YOU SPECIFICALLY AGREE THAT IN NO EVENT SHALL MICROSOFT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, PUNITIVE, INCIDENTAL, SPECIAL, CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF USE, DATA OR PROFITS, ARISING OUT OF OR IN ANY WAY CONNECTED WITH THE USE OF OR INABILITY TO USE THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN, WHETHER BASED ON CONTRACT, TORT, NEGLIGENCE, STRICT LIABILITY OR OTHERWISE, EVEN IF MICROSOFT OR ANY OF ITS SUPPLIERS HAS BEEN ADVISED OF THE POSSIBILITY OF DAMAGES.

Keywords: kbnomt kbrapidpub KB943456

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.