473,715 Members | 6,043 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

Similar topics

6
5795
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 to leave the textbox without entering the right information. Is there a way to determine which other control was clicked in the validating event of the textbox? Thanks
7
7770
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 twice (event fired twice). Without event is fired once. if(txtMSG.Length > 0) {
2
3194
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 textbox. I also have a CompareValidator pointed to the textbox with its Type="Date." Despite all documentation to the contrary, I am getting an exception when the page first loads that is thrown by the CompareValidator, complaining that it...
0
2437
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 (http://support.microsoft.com/kb/810852), but then I realized that the hotfix mentioned was in .Net v1.1, which I am using. I took the sample from that article and recreated the situation I see in my application. (Code included below.) If you run the...
5
9339
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 can i trigger the validating event for all text box? thx and regard,s
3
2795
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
3791
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 on the form to reproduce this issue: ------------------------------------ Public Class Form1 Inherits Windows.Forms.Form
2
8711
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 Next buttons. What I want to happen is for the individual user controls to have the ability to set focus to a specific textbox control when the user control becomes active. Normally I would handle this sort of thing via a form's activate event,...
2
19293
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 have created for this purpose is derived from class UserControl.
0
8821
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8718
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9196
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9103
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7973
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6646
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4477
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4738
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2539
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.