All,
I have searched google and the newsgroups but can't find anything the
same as what I am experiencing (though I may have missed something).
I have controls (textboxes) within UserControls which are not behaving
as I would expect. Specifically, if there is a command button external
to the usercontrol which is activated by a shortcut key (eg Alt-B),
the command button Click event handler code 'executes' even though the
textbox set the e.Cancel to True in its Validating event.
My repro sample (SAMPLE CODE) is below. Note that the number will
increase each time the validating event fires (that way you know
Validating is firing). It works fine when clicking either button, or
activating Internal Button using the keyboard, but not the External
Button (using the board). That is the problem I am trying to solve...
This is on VB.NET 1.0. Framework is 1.0 SP3.
Note that I have tried some other techniques, specifically adding:
Private Sub UserControl1_Va lidating(ByVal sender As Object, ByVal
e As System.Componen tModel.CancelEv entArgs) Handles MyBase.Validati ng
If e.Cancel = False Then
If Not Me.Validate Then
e.Cancel = True
End If
End If
End Sub
But it didn't work either. With this change, when you activate the
External Button with the keyboard shortcut, on the first go it will
not execute the External Button Click event handler code (which is
what I want) but the Label value goes up by 2 (ie the textbox was
validated twice). Then if you activate the External Button with the
keyboard shortcut again, the Label value stays the same and the Click
event handler code executes (this may be because after the first
activation Me.ActiveContro l in the UserControl apparently changed from
the textbox to Nothing). If you repeat the keyboard activation the
cycle continues (double validation on the first, then click event
handler execution on the next).
All in all, I'm lost. Any suggestions?
Thanks
Matthew
SAMPLE CODE:
UserControl1.vb :
Public Class UserControl1
Inherits System.Windows. Forms.UserContr ol
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeCompo nent()
'Add any initialization after the InitializeCompo nent() call
End Sub
'UserControl overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er
'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TextBox1 As System.Windows. Forms.TextBox
Friend WithEvents TextBox2 As System.Windows. Forms.TextBox
Friend WithEvents Button1 As System.Windows. Forms.Button
Friend WithEvents Label1 As System.Windows. Forms.Label
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
Me.TextBox1 = New System.Windows. Forms.TextBox()
Me.TextBox2 = New System.Windows. Forms.TextBox()
Me.Button1 = New System.Windows. Forms.Button()
Me.Label1 = New System.Windows. Forms.Label()
Me.SuspendLayou t()
'
'TextBox1
'
Me.TextBox1.Loc ation = New System.Drawing. Point(144, 208)
Me.TextBox1.Nam e = "TextBox1"
Me.TextBox1.Siz e = New System.Drawing. Size(80, 20)
Me.TextBox1.Tab Index = 2
Me.TextBox1.Tex t = "TextBox1"
'
'TextBox2
'
Me.TextBox2.Loc ation = New System.Drawing. Point(152, 240)
Me.TextBox2.Nam e = "TextBox2"
Me.TextBox2.Tab Index = 3
Me.TextBox2.Tex t = "TextBox2"
'
'Button1
'
Me.Button1.Loca tion = New System.Drawing. Point(272, 208)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing. Size(184, 48)
Me.Button1.TabI ndex = 4
Me.Button1.Text = "&Internal Button"
'
'Label1
'
Me.Label1.Locat ion = New System.Drawing. Point(40, 272)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing. Size(96, 32)
Me.Label1.TabIn dex = 5
Me.Label1.Text = "0"
'
'UserControl1
'
Me.Controls.Add Range(New System.Windows. Forms.Control()
{Me.Label1, Me.Button1, Me.TextBox2, Me.TextBox1})
Me.Name = "UserContro l1"
Me.Size = New System.Drawing. Size(512, 344)
Me.ResumeLayout (False)
End Sub
#End Region
Private Sub TextBox1_Valida ting(ByVal sender As System.Object,
ByVal e As System.Componen tModel.CancelEv entArgs) Handles
TextBox1.Valida ting
Label1.Text = CInt(Label1.Tex t) + 1
e.Cancel = True
End Sub
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e
As System.EventArg s) Handles Button1.Click
MsgBox("Interna l Hi")
End Sub
Private Sub UserControl1_Lo ad(ByVal sender As System.Object, ByVal
e As System.EventArg s) Handles MyBase.Load
End Sub
End Class
Form1.vb:
Public Class Form1
Inherits System.Windows. Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeCompo nent()
'Add any initialization after the InitializeCompo nent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er
'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents pnlControls As System.Windows. Forms.Panel
Friend WithEvents Button1 As System.Windows. Forms.Button
Friend WithEvents UserControl11 As
WindowsApplicat ion32.UserContr ol1
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
Me.pnlControls = New System.Windows. Forms.Panel()
Me.UserControl1 1 = New WindowsApplicat ion32.UserContr ol1()
Me.Button1 = New System.Windows. Forms.Button()
Me.pnlControls. SuspendLayout()
Me.SuspendLayou t()
'
'pnlControls
'
Me.pnlControls. Controls.AddRan ge(New
System.Windows. Forms.Control() {Me.UserControl 11})
Me.pnlControls. Dock = System.Windows. Forms.DockStyle .Bottom
Me.pnlControls. Location = New System.Drawing. Point(0, 272)
Me.pnlControls. Name = "pnlControl s"
Me.pnlControls. RightToLeft =
System.Windows. Forms.RightToLe ft.No
Me.pnlControls. Size = New System.Drawing. Size(760, 373)
Me.pnlControls. TabIndex = 4
'
'UserControl11
'
Me.UserControl1 1.Location = New System.Drawing. Point(8, 8)
Me.UserControl1 1.Name = "UserContro l11"
Me.UserControl1 1.Size = New System.Drawing. Size(760, 373)
Me.UserControl1 1.TabIndex = 0
'
'Button1
'
Me.Button1.Loca tion = New System.Drawing. Point(416, 40)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing. Size(216, 56)
Me.Button1.TabI ndex = 0
Me.Button1.Text = "&External Button"
'
'Form1
'
Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)
Me.ClientSize = New System.Drawing. Size(760, 645)
Me.Controls.Add Range(New System.Windows. Forms.Control()
{Me.pnlControls , Me.Button1})
Me.Name = "Form1"
Me.Text = "Form1"
Me.pnlControls. ResumeLayout(Fa lse)
Me.ResumeLayout (False)
End Sub
#End Region
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e
As System.EventArg s) Handles Button1.Click
MsgBox("Hi")
End Sub
End Class