TextBox Validation and Containers

P

Phill. W

Is there anything I need to watch out for when placing TextBoxes
onto Containers like Tab Pages?

I've got a funny where the Validating Event for a TextBox doesn't
fire when I tab out of the TextBox, and adamantly refuses to fire
*until* I tab out of the TabPage that contains it and onto another
control on the form, at which point the Validating Event on the
TextBox (finally) appears.

Any suggestions?

TIA,
Phill W.
 
G

Guest

Hi Phil,

If I understand your question correctly, you are having trouble w/ the
validating event of the textbox. The validating event is fired when the
control i.e. your textbox loses focus. You can change the focus by using the
programatically or by using the Keyboard or Mouse. I think what you are
seeing is normal behaviour.

If you want some checking routine to fire as and when you are typing in the
textbox you need to use the textbox_changed event.

Here is a link that talks abt the Validating even
http://msdn.microsoft.com/library/d...emwindowsformscontrolclassvalidatingtopic.asp

HTH.
Good Luck!
 
P

Phill. W

Sarika said:
Hi Phil,

If I understand your question correctly, you are having trouble w/ the
validating event of the textbox.
Correct.

The validating event is fired when the control ... loses focus. .. . .
I think what you are seeing is normal behaviour.

The Validating Event being raised /immediately/ the Control loses
focus is normal.
What /I'm/ seeing is the Validating Event for /one/ Control being
raised after a /number/ of changes of focus in and out of /other/
Controls on the form (all of which raise /their/ Validating Events in
the proper order.
I usually have to tab all the way out of the Tab Control before my
errant Control [finally] gets round to validating itself.

What effect does the CausesValidation property on the GroupBox,
TabPage and TabControl have on any controls that are placed
within them?

TIA,
Phill W.
 
G

Guest

Ok I understand your problem now. But I am stumped as to why this is
happening. I created a dummy project and did not encounter this problem.

I tried different combinations of CausesValidation on the tab and its
container controls and observed that as long as the txtBox has its
CausesValidation = True, the Validating event is always raised AS SOON as the
txtBox loses focus.

I am pasting my code, if its going to be of any help. If you can post your
code and we can see what you are missing.

\\\
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 tbsOptions As System.Windows.Forms.TabControl
Friend WithEvents tbsOptions1 As System.Windows.Forms.TabPage
Friend WithEvents tbsOptions2 As System.Windows.Forms.TabPage
Friend WithEvents txttbs1 As System.Windows.Forms.TextBox
Friend WithEvents txttbs2 As System.Windows.Forms.TextBox
Friend WithEvents btntbs2 As System.Windows.Forms.Button
Friend WithEvents btntbs1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.tbsOptions = New System.Windows.Forms.TabControl
Me.tbsOptions1 = New System.Windows.Forms.TabPage
Me.btntbs1 = New System.Windows.Forms.Button
Me.txttbs1 = New System.Windows.Forms.TextBox
Me.tbsOptions2 = New System.Windows.Forms.TabPage
Me.btntbs2 = New System.Windows.Forms.Button
Me.txttbs2 = New System.Windows.Forms.TextBox
Me.tbsOptions.SuspendLayout()
Me.tbsOptions1.SuspendLayout()
Me.tbsOptions2.SuspendLayout()
Me.SuspendLayout()
'
'tbsOptions
'
Me.tbsOptions.CausesValidation = False
Me.tbsOptions.Controls.Add(Me.tbsOptions1)
Me.tbsOptions.Controls.Add(Me.tbsOptions2)
Me.tbsOptions.ItemSize = New System.Drawing.Size(0, 21)
Me.tbsOptions.Location = New System.Drawing.Point(8, 16)
Me.tbsOptions.Name = "tbsOptions"
Me.tbsOptions.SelectedIndex = 0
Me.tbsOptions.Size = New System.Drawing.Size(464, 272)
Me.tbsOptions.TabIndex = 0
'
'tbsOptions1
'
Me.tbsOptions1.CausesValidation = False
Me.tbsOptions1.Controls.Add(Me.btntbs1)
Me.tbsOptions1.Controls.Add(Me.txttbs1)
Me.tbsOptions1.Location = New System.Drawing.Point(4, 25)
Me.tbsOptions1.Name = "tbsOptions1"
Me.tbsOptions1.Size = New System.Drawing.Size(456, 243)
Me.tbsOptions1.TabIndex = 0
Me.tbsOptions1.Text = "tbs1"
'
'btntbs1
'
Me.btntbs1.Location = New System.Drawing.Point(16, 144)
Me.btntbs1.Name = "btntbs1"
Me.btntbs1.Size = New System.Drawing.Size(120, 56)
Me.btntbs1.TabIndex = 1
Me.btntbs1.Text = "GoTo TBS2"
'
'txttbs1
'
Me.txttbs1.Location = New System.Drawing.Point(16, 32)
Me.txttbs1.Name = "txttbs1"
Me.txttbs1.Size = New System.Drawing.Size(128, 22)
Me.txttbs1.TabIndex = 0
Me.txttbs1.Text = ""
'
'tbsOptions2
'
Me.tbsOptions2.CausesValidation = False
Me.tbsOptions2.Controls.Add(Me.btntbs2)
Me.tbsOptions2.Controls.Add(Me.txttbs2)
Me.tbsOptions2.Location = New System.Drawing.Point(4, 25)
Me.tbsOptions2.Name = "tbsOptions2"
Me.tbsOptions2.Size = New System.Drawing.Size(456, 243)
Me.tbsOptions2.TabIndex = 1
Me.tbsOptions2.Text = "tbs2"
'
'btntbs2
'
Me.btntbs2.Location = New System.Drawing.Point(32, 112)
Me.btntbs2.Name = "btntbs2"
Me.btntbs2.Size = New System.Drawing.Size(112, 48)
Me.btntbs2.TabIndex = 1
Me.btntbs2.Text = "GoTo TBS1"
'
'txttbs2
'
Me.txttbs2.Location = New System.Drawing.Point(32, 24)
Me.txttbs2.Name = "txttbs2"
Me.txttbs2.Size = New System.Drawing.Size(128, 22)
Me.txttbs2.TabIndex = 0
Me.txttbs2.Text = ""
'
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15)
Me.BackColor = System.Drawing.SystemColors.GrayText
Me.ClientSize = New System.Drawing.Size(499, 329)
Me.Controls.Add(Me.tbsOptions)
Me.Name = "Form1"
Me.Text = "Test Project"
Me.tbsOptions.ResumeLayout(False)
Me.tbsOptions1.ResumeLayout(False)
Me.tbsOptions2.ResumeLayout(False)
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Me.tbsOptions1.Visible = True
Me.tbsOptions2.Visible = False
End Sub

Private Sub btntbs1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btntbs1.Click
Me.tbsOptions1.Visible = False
Me.tbsOptions2.Visible = True
End Sub

Private Sub btntbs2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btntbs2.Click
Me.tbsOptions1.Visible = True
Me.tbsOptions2.Visible = False
End Sub

Private Sub txttbs1_Validating(ByVal sender As System.Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles txttbs1.Validating
If IsNumeric(txttbs1.Text) Then
Else
MsgBox("Non numeric data unacceptable!")
txttbs1.SelectAll()
e.Cancel = True
End If
End Sub
End Class

///

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top