Question validated event in single textbox form

ideprize

Well-known member
Joined
Oct 19, 2011
Messages
96
Programming Experience
10+
Hi All

I have been fighting a nuance with a form that has only one label and one textbox. The validated event does not fire! I have employed the click, enter, keydown, leave, and validated events. I cannot exit the form unless I trap the return key in the keydown event and exit from that event via a me.dispose command. Is this normal behavior? The validated event is never reached. If I don't execute the me.dispose command the vb.net model places the textbox control in "oblivion". The other interesting nuance created by this required exit method is the next form that I have in the application has two labels and two textboxes. When this second form is entered the first textbox is skipped! After a lot of head scratching I entered the application.doevent command prior to the me.dispose command in the keydown event in the first form. This addition resolved the entry issue with the second form. What is going on here? Is this a known anomaly in VB.net? Any insights on this behavior would be greatly appreciated.

Respectfully
Ideprize
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,349
Location
Sydney, Australia
Programming Experience
10+
The form has a Validate method that will validate the last control. Sounds like you need to call it. Generally speaking, controls raise their Validating and, optionally, Validated events when an attempt is made to move focus to another control. Even in a form with lots of controls, you still need to call Validate when clicking a Save or OK button to ensure that the last control to have focus is validated. You can also call ValidateChildren to validate all controls, in case some never received focus.
 

ideprize

Well-known member
Joined
Oct 19, 2011
Messages
96
Programming Experience
10+
Whenever I create a textbox control I ALWAYS include the click, enter, keydown, leave, and validated events. The problem is if I don't place a return key trap in the keydown event handler with a me.dispose command (following the "SendKeys.Send(vbTab)" command) in that handler the execution path goes off to oblivion - it never reaches the validated event handler or the leave event handler. Understand, this happens only in the "degenerate case" - 1 textbox on the form. I have tried adding the validating event handler as well - no cigar! I am convinced that this is a bug in the VB.net model.
 

ideprize

Well-known member
Joined
Oct 19, 2011
Messages
96
Programming Experience
10+
Below is an application with 2 very simple forms. Form1 "calls" Form2 via a showdialog method. If Form2 has only one textbox the "me.dispose" command in textbox1.validated is never reached unless I click the "form.close" event. If I add a second textbox then both textboxes are entered as expected and the textbox2.validated event is reach and form2 is disposed with execution returning to form1.

VB.NET:
Imports System
Imports System.Windows.Forms
Imports System.Data
Imports System.Windows.Forms.Control
Imports System.IO
Imports System.Drawing.Printing
Imports System.Data.Sql
Imports MySql.Data.MySqlClient
Imports System.Data.OleDb
Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        Label1.Text = "Start Program"
        form2.showdialog()
    End Sub

End Class

Public Class Form2
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub TextBox1_Click(sender As Object, e As EventArgs) Handles TextBox1.Click

    End Sub

    Private Sub TextBox1_Enter(sender As Object, e As EventArgs) Handles TextBox1.Enter
        TextBox1.BackColor = Color.Yellow
    End Sub

    Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Return Then
            SendKeys.Send(vbTab)
        End If
    End Sub

    Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
        TextBox1.BackColor = Color.White
    End Sub

    Private Sub TextBox1_Validated(sender As Object, e As EventArgs) Handles TextBox1.Validated
        'Me.Dispose()
    End Sub

    Private Sub TextBox2_Click(sender As Object, e As EventArgs) Handles TextBox2.Click

    End Sub

    Private Sub TextBox2_Enter(sender As Object, e As EventArgs) Handles TextBox2.Enter
        TextBox1.BackColor = Color.Yellow
    End Sub

    Private Sub TextBox2_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox2.KeyDown
        If e.KeyCode = Keys.Return Then
            SendKeys.Send(vbTab)
        End If
    End Sub

    Private Sub TextBox2_Leave(sender As Object, e As EventArgs) Handles TextBox2.Leave
        TextBox1.BackColor = Color.White
    End Sub

    Private Sub TextBox2_Validated(sender As Object, e As EventArgs) Handles TextBox2.Validated
        Me.Dispose()
    End Sub
End Class

As mentioned above this set of code behaves as expected - the Me.Dispose() in textbox2.validated is executed and execution is returned to form1.
If you remove the code for textbox2 (and the textbox2 control) execution will hang unless you click the form2.close event. If you eliminate the minimize, maximize, and close form events
you have a hung application. If I am misinterpreting this sequence of events then please inform me of my "misinterpretation". I see this as a classic "degenerate case" bug.
Respectfully,
Ideprize
 
Last edited by a moderator:

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,439
Location
Norway
Programming Experience
10+
insertcode.png
 
Top Bottom