472,784 Members | 857 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,784 software developers and data experts.

Validating event - how to ensure focus is 'locked' to a control within a UserControl

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
Nov 21 '05 #1
0 2370

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

Similar topics

6
by: Alex Bink | last post by:
Hi, I have a validating event on a textbox in which I want to prevent the user to leave the textbox without entering the right data. Only if he clicks on another specific control he is allowed...
7
by: | last post by:
Hello to all I'm handling the Validating event for one text box. If something is wrong in user input I show a warning message. The problem is that if I add to method the message is displayed...
2
by: Eric Maia | last post by:
I have a textbox (StartDateTextBox) in a UserControl on my page, that is supposed to have a date entered into it. I have a RequiredFieldValidator that has its ControlToValidate property set to the...
0
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...
5
by: norton | last post by:
Hi All, I am writing a simple win form which contains a button, when user click the button i want to validate all the other textbox and see if there is anything goes wrong may i know how...
3
by: TheSteph | last post by:
Hi Experts ! I have a Winform Program in C# / .NET 2.0 I would like to ensure that a value in a TextBox is a valid Int32 when user get out of it (TextBox loose focus)
1
by: =?Utf-8?B?bGpsZXZlbmQy?= | last post by:
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...
2
by: CGatto | last post by:
Hi, I have Windows Form with several User Controls embedded within. Only one user control is visible at any one time. The user moves through the various user controls by clicking Previous and...
2
by: Tony Johansson | last post by:
Hello! I have created a Control that consist of a label and a textbox.I have called this class ctlLabelTextbox. public partial class ctlLabelTextbox : UserControl { .... } The class that I...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.