none
Problem about Textbox Event "Enter, Leave" manytimes RRS feed

  • Question

  • From my code below:

       Private Sub TB01_Enter(sender As Object, e As EventArgs) Handles TB01.Enter
            Debug.Print("TB01 Enter ")
            TB02.Select() '************************* This Line make many following Events as the Debug.print below

        End Sub
        Private Sub TB02_Enter(sender As Object, e As EventArgs) Handles TB02.Enter
            Debug.Print("TB02 Enter ")

        End Sub

        Private Sub TB01_Leave(sender As Object, e As EventArgs) Handles TB01.Leave
            Debug.Print("TB01 Leave ")

        End Sub

        Private Sub TB02_Leave(sender As Object, e As EventArgs) Handles TB02.Leave
            Debug.Print("TB02 Leave ")

        End Sub

        Private Sub TB01_Validating(sender As Object, e As CancelEventArgs) Handles TB01.Validating
            Debug.Print("TB01 Validating")
             
        End Sub

        Private Sub TB02_Validating(sender As Object, e As CancelEventArgs) Handles TB02.Validating
            Debug.Print("TB02 Validating")

        End Sub


    Why many Events occur with only one Enter (one mouse click) in the TB01 as the debug.print below:

    TB01 Enter 
    TB02 Enter 
    TB02 Leave 
    TB02 Enter 
    TB02 Leave 
    TB02 Validating
    TB01 Enter 
    TB02 Enter 
    TB02 Leave 
    TB02 Enter 
    TB02 Validating


    remark: 1st edit for more clear about the question

    At last, after found the answer from Viorel, I also found knowledge about this in >> https://www.codeproject.com/Articles/10311/What-s-up-with-BeginInvoke.

    Regard,

    • Edited by NewbieMe Sunday, June 17, 2018 7:10 PM
    Sunday, June 17, 2018 11:36 AM

Answers

  • Try the next workaround:

        Private Sub TB01_Enter(sender As Object, e As EventArgs) Handles TB01.Enter

            Debug.Print("TB01 Enter ")

            BeginInvoke(Sub()

                            TB02.Select()

                        End Sub)

        End Sub


    • Marked as answer by NewbieMe Sunday, June 17, 2018 2:54 PM
    Sunday, June 17, 2018 2:43 PM

All replies

  • Hi

    You are seeing several events firing during initialization as well as user events.

    Try this: this example uses a TextBox on the Form instead of Console - see layout used in below image. The Button is only to clear the TextBox3.Text.

    This may be closer to what you were expecting. NOTE: the 'Started' variable which skips initialization events until the Form is shown.

    Option Strict On
    Option Explicit On
    Imports System.ComponentModel
    Public Class Form1
      Dim started As Boolean = False
      Private Sub TextBox1_Enter(sender As Object, e As EventArgs) Handles TextBox1.Enter
        If Not started Then Exit Sub
        TextBox3.AppendText("TextBox1 Enter" & vbCrLf)
      End Sub
      Private Sub TextBox2_Enter(sender As Object, e As EventArgs) Handles TextBox2.Enter
        If Not started Then Exit Sub
        TextBox3.AppendText("TextBox2 Enter" & vbCrLf)
      End Sub
      Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
        If Not started Then Exit Sub
        TextBox3.AppendText("TextBox1 Leave" & vbCrLf)
      End Sub
      Private Sub TextBox2_Leave(sender As Object, e As EventArgs) Handles TextBox2.Leave
        If Not started Then Exit Sub
        TextBox3.AppendText("TextBox2 Leave" & vbCrLf)
      End Sub
      Private Sub TextBox1_Validating(sender As Object, e As CancelEventArgs) Handles TextBox1.Validating
        If Not started Then Exit Sub
        TextBox3.AppendText("TextBox1 Validating" & vbCrLf)
      End Sub
      Private Sub TextBox2_Validating(sender As Object, e As CancelEventArgs) Handles TextBox2.Validating
        If Not started Then Exit Sub
        TextBox3.AppendText("TextBox2 Validating" & vbCrLf)
      End Sub
      Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        TextBox3.Select()
        started = True
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        TextBox3.Clear()
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Sunday, June 17, 2018 12:11 PM
  • I agree with Les on the events. I think that is the way it works? Is there some reason you think it should not happen??

    Order of events.

    I am mostly posting for a test. I started a post and canceled as I want to see the result.

    Btw here is a nit pick: I believe from the compiler view its better to do it this way. Using exit sub in an if makes it less efficient ... it makes two routines in the .exe one to continue and one to exit. The if has to be resolved. Not sure exactly???

     Private Sub TextBox1_Enter(sender As Object, e As EventArgs) Handles TextBox1.Enter
            If started Then
                TextBox3.AppendText("TextBox1 Enter" & vbCrLf)
            End If
    
        End Sub

    You can also do things like don't enable controls until after the form load. And finally sometimes the firing controls in form load is good like say you want to set a value and then call the event to do other things you always do after setting the value.

    Now lest see if my earlier cancel post affects the last post name on the main page list.

    Sunday, June 17, 2018 12:39 PM
  • Now lest see if my earlier cancel post affects the last post name on the main page list.

    Yes. Because I started a post, canceled it, then did it again and posted this time, after the real post my name is not shown in the main page as the last post.

    So that is a bug.

    Lets see what it says after this post.

    Yup. Bug. It is a minor problem as then the OP does not know a reply has been made.

    Sunday, June 17, 2018 12:43 PM
  • @ Tommy

    Hi

    In these trivial cases, you may well be right regarding the efficiency of the code.

    However, in cases where the code block is complex, I would doubt that having to test the variable throughout that complex code could possibly be the best option.

    But there again, I don't really know .......... :)


    Regards Les, Livingston, Scotland

    Sunday, June 17, 2018 12:55 PM
  • @ Tommy

    Hi

    In these trivial cases, you may well be right regarding the efficiency of the code.

    However, in cases where the code block is complex, I would doubt that having to test the variable throughout that complex code could possibly be the best option.

    But there again, I don't really know .......... :)


    Regards Les, Livingston, Scotland

    Les,

    I am not saying test it every line in the routine. I am saying test it once as in the example it tests for true and the if surrounds the entire sub code. So the effect is the same.

    :)

    But I just wanted to test for the cancel bug. If someone can remind me where we post forum editor problems I will post there???

    Sunday, June 17, 2018 1:07 PM
  • PS So much for my cancel theory? Maybe if you cancel at all in the post?

    I did not cancel an edit this time and I still don't seem to show up as the last post after I post. It should say me but it says leshay as last reply.

    PS and now after my second post I am the last reply

    Notice how the first image has 4 replies but I had replied after les and the second image now shows 6 replies. Reply 5 for some reason does not get listed as last post.

    Plus it shows 28 views on both images?



    Sunday, June 17, 2018 1:13 PM
  • Thank you for every replies,

    I try to make some window application that have

    1. A textbox for cursor to exist and waiting user for typing any commands.

    2. Another textbox(es) , to do any actions after the 1st textbox accept the user commands.

    (The action to be done in texboxes including entering to theme or leaving too)

    So, the debug.print as I asked is not only when starting the program but every time I "click" the TB01.

    I wonder that why TB02 Enter, Leave , Enter, Leave, TB1 come back to enter again?????, TB2 Enter .... and finally TB2 Enter,

    these Events occur in only one click!!

    This make me confuse when coding inside the Sub because it will repeat enter and leave many time for only 1 user action.

    Thank you for any reply.


    • Edited by NewbieMe Sunday, June 17, 2018 1:27 PM
    Sunday, June 17, 2018 1:23 PM
  • Thank you for every replies,

    I try to make some window application that have

    1. A textbox for cursor to exist and waiting user for typing any commands.

    2. Another textbox(es) , to do any actions after the 1st textbox accept the user commands.

    (The action to be done in texboxes including entering to theme or leaving too)

    So, the debug.print as I asked is not only when starting the program but every time I "click" the TB01.

    I wonder that why TB02 Enter, Leave , Enter, Leave, TB1 come back to enter again?????, TB2 Enter .... and finally TB2 Enter,

    these Events occur in only one click!!

    This make me confuse when coding inside the Sub because it will repeat enter and leave many time for only 1 user action.

    Thank you for any reply.


    Newbie,

    Remove  TB02.Select() temporary from TB01_Enter event and see what happens.

    Check the order of events for focus.

    You also have to remember if you put a break point in an event and then single step the code or continue the following events are lost in the debugger. So you may not get the same events running in the debugger as running the .exe and etc. Its tricky. Takes practice to learn them. Not that I know them all. Not sure that debug print may do.


    PS After this post I am shown as the last reply.

    Sunday, June 17, 2018 1:31 PM
  • Thank you for your reply, but the TB02.Select() is the action that I have to do.

    In fact , my code have many line so I remake it for easiest to focus.

    I wonder that why I Enter TB01 (by mouse click) but "many Events" occur.

    The code is TB02.Select(), only 1 command.


    Sunday, June 17, 2018 1:43 PM
  • I think the Debug.Print should be like this:

    TB01.Enter  (When click)

    TB01.Leave

    TB02.Enter


    But, where another events come from???? (unfocus in Validating) 

    Sunday, June 17, 2018 1:48 PM
  • I think the Debug.Print should be like this:

    TB01.Enter  (When click)

    TB01.Leave

    TB02.Enter


    But, where another events come from???? (unfocus in Validating) 


    Why dont you tells us what you want to do more exactly in detail as it seems you are getting much too complicated?

    It is hard for us to guess what your question is or what your code is doing and why.

    Do you have a bunch of text boxes and want the user to enter say 3 values into 3 textboxes and then you want to execute some code? If so add a "Go" button to the form and when the user has entered the data they click go. Then you execute your code with the values in the text box.

    Will that work better. I just have the feeling you are trying to do too much by firing events yourself with code and that is hard to do. Better to have the user fire events when they are ready.

    Sunday, June 17, 2018 1:56 PM
  • I think the Debug.Print should be like this:

    TB01.Enter  (When click)

    TB01.Leave

    TB02.Enter


    But, where another events come from???? (unfocus in Validating) 

    Hi

    If you run the code I posted, you will see almost the same as you posted - except, you have also coded for the Validating event and so it also shows up in the list.

    Using my code, this is the result:

    TextBox1 Enter
    TextBox1 Leave
    TextBox1 Validating
    TextBox2 Enter

    which is correct,even though you didn't list the Validating event firing.


    Regards Les, Livingston, Scotland

    Sunday, June 17, 2018 2:02 PM
  • tommytwotrain,

    Thank you for your suggestion but please focus in the bug of textbox that I try to understand.

    I think you will wonder too if you get my code to compile.

    Sunday, June 17, 2018 2:05 PM
  • I think the Debug.Print should be like this:

    TB01.Enter  (When click)

    TB01.Leave

    TB02.Enter


    But, where another events come from???? (unfocus in Validating) 

    Hi

    If you run the code I posted, you will see almost the same as you posted - except, you have also coded for the Validating event and so it also shows up in the list.

    Using my code, this is the result:

    TextBox1 Enter
    TextBox1 Leave
    TextBox1 Validating
    TextBox2 Enter

    which is correct,even though you didn't list the Validating event firing.


    Regards Les, Livingston, Scotland

    Thanks Les,

    But your code lost the "TextBox2.Select()" inside the TextBox1_Enter that is my problem.

    My question is that why TextBox2.Select() inside the TextBox1_Enter make many Event after that?

    Sunday, June 17, 2018 2:13 PM
  • Try the next workaround:

        Private Sub TB01_Enter(sender As Object, e As EventArgs) Handles TB01.Enter

            Debug.Print("TB01 Enter ")

            BeginInvoke(Sub()

                            TB02.Select()

                        End Sub)

        End Sub


    • Marked as answer by NewbieMe Sunday, June 17, 2018 2:54 PM
    Sunday, June 17, 2018 2:43 PM
  • Try the next workaround:

        Private Sub TB01_Enter(sender As Object, e As EventArgs) Handles TB01.Enter

            Debug.Print("TB01 Enter ")

            BeginInvoke(Sub()

                            TB02.Select()

                        End Sub)

        End Sub


    Thank you very much Viorel,

    I spent very long time to find the answer.

    I have never seen "BeginInvoke" before. What about this!!

    Thank you Viorel.

    Sunday, June 17, 2018 2:57 PM
  • tommytwotrain,

    Thank you for your suggestion but please focus in the bug of textbox that I try to understand.

    I think you will wonder too if you get my code to compile.


    Ok. I did what you asked. I added the formload and shown debug prints.

    Image 1: I started the form image. Focus is on tb02.

    Image 2: I clicked the mouse in tb1 and focus moves to tb01.

    Image 3: I remove the TB02.Select() (first clear immediate) and run focus tb01.

    Image4 I click tb02 and focus moves to tb02.

    Seems like what should happen.

    Note the RMB clear all between starts.

    So why don't you explain to me now what the problem is and the result you wanted?

    Sunday, June 17, 2018 3:03 PM
  • tommytwotrain,

    Thank you for your suggestion but please focus in the bug of textbox that I try to understand.

    I think you will wonder too if you get my code to compile.


    Ok. I did what you asked. I added the formload and shown debug prints.

    Image 1: I started the form image. Focus is on tb02.

    Image 2: I clicked the mouse in tb1 and focus moves to tb01.

    Image 3: I remove the TB02.Select() (first clear immediate) and run focus tb01.

    Image4 I click tb02 and focus moves to tb02.

    Seems like what should happen.

    Note the RMB clear all between starts.

    So why don't you explain to me now what the problem is and the result you wanted?


    Thank you for the reply,

    Why don't you wonder in Image2?

    Why you click the TB02 1 time but "the Event follows" have many?

    For the question form you: What the problem is? >>

    Why TextBox2.Select() inside the TextBox1_Enter make many Event after that?

    The result I want? >>

    TB01.Enter  (When click)

    TB01.Leave

    TB02.Enter

    Anyway, thank you very much again for trying to find the answer for me.

    Now I already found the answer from Viorel.

    Let try in his code..

    Sunday, June 17, 2018 3:23 PM
  • Why don't you wonder in Image2?

    Why you click the TB02 1 time but "the Event follows" have many?

    Let try in his code..

    From the docs

    https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k%28System.Windows.Forms.Control.Enter%29%3Bk%28TargetFrameworkMoniker-.NETFramework,Version%3Dv4.5%29%3Bk%28DevLang-VB%29&rd=true&f=255&MSPPError=-2147217396

    --------------------------------------------------------------------------------


    When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Select or SelectNextControl methods, or by setting the ContainerControl.ActiveControl property to the current form, focus events occur in the following order:

    1.Enter


    2.GotFocus


    3.Leave


    4.Validating


    5.Validated


    6.LostFocus


    So these are in the events after you select tbo2. Then when you finally leave tb01 you see the more enters.

    So all those events you wonder about are the nested events from calling the select tb02 and it being validated. YOu have the focus events of enter and leave and the select events and the validate events 1 to 6 as shown. Events in events.

    I find it best not to look too long its like looking at the sun.

    :)

    Sunday, June 17, 2018 3:32 PM
  • Why don't you wonder in Image2?

    Why you click the TB02 1 time but "the Event follows" have many?

    Let try in his code..

    From the docs

    --------------------------------------------------------------------------------


    When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Select or SelectNextControl methods, or by setting the ContainerControl.ActiveControl property to the current form, focus events occur in the following order:

    1.Enter


    2.GotFocus


    3.Leave


    4.Validating


    5.Validated


    6.LostFocus


    So these are in the events after you select tbo2. Then when you finally leave tb01 you see the more enters.


    So all those events you wonder about are the nested events from calling the select tb02 and it being validated. YOu have the focus events of enter and leave and the select events and the validate events 1 to 6 as shown. Events in events.

    I find it best not to look too long its like looking at the sun.

    :)

    Thank you for the reply,

    But form the document, isn't it should be once for every event? 

    Why TB02 Enter 4times, Leave 3 Times?, And TB01 Enter 1Time (for?)

    The red box that you focus is validating 1-6 , but what about "back to tb02"?.

    I already found the document before but I think it not answer my question.

    Regard,


    • Edited by NewbieMe Sunday, June 17, 2018 3:48 PM
    Sunday, June 17, 2018 3:47 PM
  • Thank you for the reply,

    But form the document, isn't it should be once for every event? 

    Why TB02 Enter 4times, Leave 3 Times?, And TB01 Enter 1Time (for?)

    The red box that you focus is validating 1-6 , but what about "back to tb02"?.

    I already found the document before but I think it not answer my question.

    Regard,


    Our posts crossed so I made a new one and deleted one.

    PS So what Viorel did is skip the select in form load. Same as if Les used the started bool and etc.

    But what I want to know from you is why do you want to move to tb2 with the selecttb2???

    That's the problem. Why do that?

    Seems every time you enter tb01 you immediately jump to tb02? That select should not be in the enter event as you show in your first post?

    That's what is wrong. Don't do that?

    Comprende?

    Or maybe I don't understand?

    Well I don't care if you don't.

    :)

    PS You are not counting the nested events. When the tb leaves it looses focus and validating and then it enters and validated and leaves again. Nested events.

    I am no expert either so I cant explain it all. Maybe the others can.



    Sunday, June 17, 2018 3:55 PM