Recursion?

ideprize

Well-known member
Joined
Oct 19, 2011
Messages
97
Programming Experience
10+
Hi All. Below you see the code to a very simple form which I created to duplicate a strange behavior on a much larger form and I am not sure what to make of it.
VB.NET:
Public Class Form1
    Dim str1 As String

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        MsgBox("form1 loaded")
        Me.Show()
    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
        MsgBox("box1 in")
    End Sub

    Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
        MsgBox("box1 out")
        str1 = TextBox1.Text
        'TextBox2.Select()
        TextBox3.Select()
    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
        MsgBox("box2 in")
        MsgBox("str1 = " + str1)
    End Sub

    Private Sub TextBox2_Leave(sender As Object, e As EventArgs) Handles TextBox2.Leave
        MsgBox("box2 out")
    End Sub

    Private Sub TextBox3_Click(sender As Object, e As EventArgs) Handles TextBox3.Click

    End Sub

    Private Sub TextBox3_Enter(sender As Object, e As EventArgs) Handles TextBox3.Enter
        MsgBox("box3 in")
        MsgBox("str1 = " + str1)
    End Sub

    Private Sub TextBox3_Leave(sender As Object, e As EventArgs) Handles TextBox3.Leave
        MsgBox("box3 out")
    End Sub
End Class

The form itself is the default form in VS 2013. It has 3 labels (vertical) and 3 textboxes next to them. If you use the textbox2.select() command in the textbox1_leave sub the program transfers to the textbox2_enter sub as expected. If you use the textbox3.select() command, however, the the program cycles twice in the textbox1_leave sub prior to leaving and then cycles twice in the textbox3_enter sub. I can only assume that this happens something to do with "tabindex" but what is really disturbing is the code is executed twice in the "from sub" and the "to" sub. This can't be normal. Any thoughts on this would be greatly appreciated. I have attached a jpeg image of the form itself.
Respectfully,
Ideprize
 

Attachments

  • vbmess.jpg
    vbmess.jpg
    161.2 KB · Views: 46
Last edited by a moderator:
Thanks for your response Herman. As it turned out I found a discussion about this same problem on the web. The solution is NOT to use the "leave" handler but the "validated" handler. After placing the transfer select code in the validated handler the transfer happened without incident. You can definitely say I learned something on this one.
 
Back
Top