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_Validating(ByVal sender As Object, ByVal
e As System.ComponentModel.CancelEventArgs) Handles MyBase.Validating
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.ActiveControl 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.UserControl
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() 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.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'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.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
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.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(144, 208)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(80, 20)
Me.TextBox1.TabIndex = 2
Me.TextBox1.Text = "TextBox1"
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(152, 240)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.TabIndex = 3
Me.TextBox2.Text = "TextBox2"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(272, 208)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(184, 48)
Me.Button1.TabIndex = 4
Me.Button1.Text = "&Internal Button"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(40, 272)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(96, 32)
Me.Label1.TabIndex = 5
Me.Label1.Text = "0"
'
'UserControl1
'
Me.Controls.AddRange(New System.Windows.Forms.Control()
{Me.Label1, Me.Button1, Me.TextBox2, Me.TextBox1})
Me.Name = "UserControl1"
Me.Size = New System.Drawing.Size(512, 344)
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub TextBox1_Validating(ByVal sender As System.Object,
ByVal e As System.ComponentModel.CancelEventArgs) Handles
TextBox1.Validating
Label1.Text = CInt(Label1.Text) + 1
e.Cancel = True
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
MsgBox("Internal Hi")
End Sub
Private Sub UserControl1_Load(ByVal sender As System.Object, ByVal
e As System.EventArgs) 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.
InitializeComponent()
'Add any initialization after the InitializeComponent() 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.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'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
WindowsApplication32.UserControl1
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.pnlControls = New System.Windows.Forms.Panel()
Me.UserControl11 = New WindowsApplication32.UserControl1()
Me.Button1 = New System.Windows.Forms.Button()
Me.pnlControls.SuspendLayout()
Me.SuspendLayout()
'
'pnlControls
'
Me.pnlControls.Controls.AddRange(New
System.Windows.Forms.Control() {Me.UserControl11})
Me.pnlControls.Dock = System.Windows.Forms.DockStyle.Bottom
Me.pnlControls.Location = New System.Drawing.Point(0, 272)
Me.pnlControls.Name = "pnlControls"
Me.pnlControls.RightToLeft =
System.Windows.Forms.RightToLeft.No
Me.pnlControls.Size = New System.Drawing.Size(760, 373)
Me.pnlControls.TabIndex = 4
'
'UserControl11
'
Me.UserControl11.Location = New System.Drawing.Point(8, 8)
Me.UserControl11.Name = "UserControl11"
Me.UserControl11.Size = New System.Drawing.Size(760, 373)
Me.UserControl11.TabIndex = 0
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(416, 40)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(216, 56)
Me.Button1.TabIndex = 0
Me.Button1.Text = "&External Button"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(760, 645)
Me.Controls.AddRange(New System.Windows.Forms.Control()
{Me.pnlControls, Me.Button1})
Me.Name = "Form1"
Me.Text = "Form1"
Me.pnlControls.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
MsgBox("Hi")
End Sub
End Class