Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. TextBox_Leave event only fired when move mouse to other control in vb.net 2005

TextBox_Leave event only fired when move mouse to other control in vb.net 2005

Scheduled Pinned Locked Moved Visual Basic
csharphelptutorialquestion
16 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R riced

    Andraw Tang wrote:

    What should I do to always have the Leave event fired?

    Leave the textbox? :) More seriously, if you check you will see that if a textbox has the focus, when you click on the form, the box does not lose focus. So you need to detect the click on the form, and if the textbox currently has focus then leave it.

    Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.

    A Offline
    A Offline
    Andraw Tang
    wrote on last edited by
    #4

    Also if I put these textbox in a Groupbox in a form, when you click the Groupbox, the form.click won't be fired, in this case Do I need to right click event for all these container?

    L 1 Reply Last reply
    0
    • A Andraw Tang

      David, Thanks for your reply. Yes, you are right, even we click the form, the textbox still has focus. I have several textbox, how can I detect which one is focused? do you have some example about it?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #5

      Try the ActiveControl[^] property :)

      I are Troll :suss:

      1 Reply Last reply
      0
      • A Andraw Tang

        Also if I put these textbox in a Groupbox in a form, when you click the Groupbox, the form.click won't be fired, in this case Do I need to right click event for all these container?

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #6

        Write a single eventhandler, and hook up all the events that need be handled. Means one actual implementation, and the other handlers pointing to that implementation.

        I are Troll :suss:

        A 1 Reply Last reply
        0
        • L Lost User

          Write a single eventhandler, and hook up all the events that need be handled. Means one actual implementation, and the other handlers pointing to that implementation.

          I are Troll :suss:

          A Offline
          A Offline
          Andraw Tang
          wrote on last edited by
          #7

          Would you please give me an example how to use it? Thanks!

          L 1 Reply Last reply
          0
          • A Andraw Tang

            Would you please give me an example how to use it? Thanks!

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #8

            Using code only;

            Public Partial Class Form1
            Inherits Form
            Public Sub New()
            InitializeComponent()

            	' Create a button
            	Dim b As New Button()
            	b.Parent = Me
            	b.Location = New Point(0, 0)
            	Controls.Add(b)
            
            	' Create a checkbox
            	Dim c As New CheckBox()
            	c.Parent = Me
            	c.Location = New Point(0, 0 + b.Height)
            	Controls.Add(c)
            
            	' Hook up both to the same handler
            	AddHandler b.Click, New EventHandler(AddressOf MyDragEventHandler)
            	AddHandler c.Click, New EventHandler(AddressOf MyDragEventHandler)
            End Sub
            
            ' You can use the sender-object to identify which control raised the event,
            ' if required.
            Private Sub MyDragEventHandler(sender As Object, e As EventArgs)
            	System.Diagnostics.Debug.WriteLine(sender.ToString() & " iz clicked")
            End Sub
            

            End Class

            Or, if you want to do it from the IDE, try these steps;

            • Drag a button and a checkbox to the form
            • Go to the properties of the button, and switch to "events" view (button looks like lightning)
            • Double-click on the Click event, and write your code
            • Go back to the designer, and select the checkbox
            • Properties, events-view; there's a combobox next to the Click event - open it, and select the item "Button1Click"

            That should do it :)

            I are Troll :suss:

            A 1 Reply Last reply
            0
            • L Lost User

              Using code only;

              Public Partial Class Form1
              Inherits Form
              Public Sub New()
              InitializeComponent()

              	' Create a button
              	Dim b As New Button()
              	b.Parent = Me
              	b.Location = New Point(0, 0)
              	Controls.Add(b)
              
              	' Create a checkbox
              	Dim c As New CheckBox()
              	c.Parent = Me
              	c.Location = New Point(0, 0 + b.Height)
              	Controls.Add(c)
              
              	' Hook up both to the same handler
              	AddHandler b.Click, New EventHandler(AddressOf MyDragEventHandler)
              	AddHandler c.Click, New EventHandler(AddressOf MyDragEventHandler)
              End Sub
              
              ' You can use the sender-object to identify which control raised the event,
              ' if required.
              Private Sub MyDragEventHandler(sender As Object, e As EventArgs)
              	System.Diagnostics.Debug.WriteLine(sender.ToString() & " iz clicked")
              End Sub
              

              End Class

              Or, if you want to do it from the IDE, try these steps;

              • Drag a button and a checkbox to the form
              • Go to the properties of the button, and switch to "events" view (button looks like lightning)
              • Double-click on the Click event, and write your code
              • Go back to the designer, and select the checkbox
              • Properties, events-view; there's a combobox next to the Click event - open it, and select the item "Button1Click"

              That should do it :)

              I are Troll :suss:

              A Offline
              A Offline
              Andraw Tang
              wrote on last edited by
              #9

              Thank you very much for your code. But sorry I didn't fully get it. If I create a GroupBox1 in Form1, and add textBox1, textBox2 and textBox3 to the GroupBox1, textBox3 is dependent to textBox1 (updated in textBox1_Leave). then I hook up these controls to MyDragEventHandler, for example I click textBox1, MyDragEventHandler handler will be called. then I input data to textBox1, if I don't click textBox2 or textBox3, instead, I click form or GroupBox1, the cursor is still in textBox1, the dependency of textBox3 cannot be updated since textBox1_Leave is not fired. If my understanding is wrong, please point it out. very appreciate.

              L 1 Reply Last reply
              0
              • A Andraw Tang

                Thank you very much for your code. But sorry I didn't fully get it. If I create a GroupBox1 in Form1, and add textBox1, textBox2 and textBox3 to the GroupBox1, textBox3 is dependent to textBox1 (updated in textBox1_Leave). then I hook up these controls to MyDragEventHandler, for example I click textBox1, MyDragEventHandler handler will be called. then I input data to textBox1, if I don't click textBox2 or textBox3, instead, I click form or GroupBox1, the cursor is still in textBox1, the dependency of textBox3 cannot be updated since textBox1_Leave is not fired. If my understanding is wrong, please point it out. very appreciate.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #10

                Your understanding is correct. A groupbox doesn't receive focus when you click it, and TextBox1 is still the active control. (The cursor is still blinking in that textbox, right?) The Leave event is fired when the input-focus changes within your application. That happens when the user clicks a button, or moves the cursor to some other textbox. There's no way to indicate that someone is "finished" with typing in a textbox except for waiting until the focus moves away. FWIW, you can set the ActiveControl property to Nothing if the user presses the enter-key. What functionality are you trying to achieve? If you want to validate the contents of the input, that has it's own event. Take a look here[^] at the order in which events are fired, perhaps there's another way to achieve what you want :)

                I are Troll :suss:

                A 1 Reply Last reply
                0
                • L Lost User

                  Your understanding is correct. A groupbox doesn't receive focus when you click it, and TextBox1 is still the active control. (The cursor is still blinking in that textbox, right?) The Leave event is fired when the input-focus changes within your application. That happens when the user clicks a button, or moves the cursor to some other textbox. There's no way to indicate that someone is "finished" with typing in a textbox except for waiting until the focus moves away. FWIW, you can set the ActiveControl property to Nothing if the user presses the enter-key. What functionality are you trying to achieve? If you want to validate the contents of the input, that has it's own event. Take a look here[^] at the order in which events are fired, perhaps there's another way to achieve what you want :)

                  I are Troll :suss:

                  A Offline
                  A Offline
                  Andraw Tang
                  wrote on last edited by
                  #11

                  Eddy, What I try to acheve it that: If user input negative value, the textBox3 will be disabled, otherwise, textBox3 is enabled. So if I input negative value to textBox1, but didn't click other controls, the textBox1_Leave not fired, then the textBox3 is still enabled. Normally how you handle this case of case?

                  L 1 Reply Last reply
                  0
                  • A Andraw Tang

                    Eddy, What I try to acheve it that: If user input negative value, the textBox3 will be disabled, otherwise, textBox3 is enabled. So if I input negative value to textBox1, but didn't click other controls, the textBox1_Leave not fired, then the textBox3 is still enabled. Normally how you handle this case of case?

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #12

                    Andraw Tang wrote:

                    Normally how you handle this case of case?

                    I'd go for the Validating event, with code similar to below;

                    Public Partial Class MainForm
                    Inherits Form
                    Private t As New TextBox()
                    Private t2 As New TextBox()

                    Public Sub New()
                    	' t2 will be disabled if t fails to validate
                    	t2.Parent = Me
                    	t2.Location = New Point(0, 0)
                    
                    	' this is the control that we're validating
                    	t.Parent = Me
                    	t.Location = New Point(0, 0 + t2.Height)
                    
                    	Controls.AddRange(New Control() {t, t2})
                    
                    	' Hook up any control that doesn't allow zero's;
                    	AddHandler t.Validating, New CancelEventHandler(AddressOf t\_Validating)
                    End Sub
                    
                    Private Sub t\_Validating(sender As Object, e As CancelEventArgs)
                    	' identify what control raised the event
                    	Dim senderAsTextBox As TextBox = TryCast(sender, TextBox)
                    
                    	' verify that the content is valid
                    	Dim IsContentValid As Boolean = senderAsTextBox.Text.Contains("-")
                    
                    	' if it ain't, cancel the moving of the focus;
                    	e.Cancel = IsContentValid
                    
                    	' your custom action goes here, we disable t2 if 
                    	' the content isn't valid
                    	t2.Enabled = Not IsContentValid
                    
                    	' included for fun :)
                    	If IsContentValid Then
                    		System.Media.SystemSounds.Beep.Play()
                    	End If
                    End Sub
                    

                    End Class

                    That will allow the user to switch to another program (to lookup the correct value on the internetz, for example), but it won't allow the user to do anything (like clicking a button in your program or closing it) until the textbox has a valid value. To achieve what you requested, remove line #29.

                    I are Troll :suss:

                    A 1 Reply Last reply
                    0
                    • L Lost User

                      Andraw Tang wrote:

                      Normally how you handle this case of case?

                      I'd go for the Validating event, with code similar to below;

                      Public Partial Class MainForm
                      Inherits Form
                      Private t As New TextBox()
                      Private t2 As New TextBox()

                      Public Sub New()
                      	' t2 will be disabled if t fails to validate
                      	t2.Parent = Me
                      	t2.Location = New Point(0, 0)
                      
                      	' this is the control that we're validating
                      	t.Parent = Me
                      	t.Location = New Point(0, 0 + t2.Height)
                      
                      	Controls.AddRange(New Control() {t, t2})
                      
                      	' Hook up any control that doesn't allow zero's;
                      	AddHandler t.Validating, New CancelEventHandler(AddressOf t\_Validating)
                      End Sub
                      
                      Private Sub t\_Validating(sender As Object, e As CancelEventArgs)
                      	' identify what control raised the event
                      	Dim senderAsTextBox As TextBox = TryCast(sender, TextBox)
                      
                      	' verify that the content is valid
                      	Dim IsContentValid As Boolean = senderAsTextBox.Text.Contains("-")
                      
                      	' if it ain't, cancel the moving of the focus;
                      	e.Cancel = IsContentValid
                      
                      	' your custom action goes here, we disable t2 if 
                      	' the content isn't valid
                      	t2.Enabled = Not IsContentValid
                      
                      	' included for fun :)
                      	If IsContentValid Then
                      		System.Media.SystemSounds.Beep.Play()
                      	End If
                      End Sub
                      

                      End Class

                      That will allow the user to switch to another program (to lookup the correct value on the internetz, for example), but it won't allow the user to do anything (like clicking a button in your program or closing it) until the textbox has a valid value. To achieve what you requested, remove line #29.

                      I are Troll :suss:

                      A Offline
                      A Offline
                      Andraw Tang
                      wrote on last edited by
                      #13

                      Eddy, Thank you very much, I will try tomorrow and let you know the result.

                      L 1 Reply Last reply
                      0
                      • A Andraw Tang

                        Eddy, Thank you very much, I will try tomorrow and let you know the result.

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #14

                        You're welcome, and sleep well :)

                        1 Reply Last reply
                        0
                        • A Andraw Tang

                          Hi, dear all, I have several textbox in my form. Among them there are two have dependency. For example (not same but the idea is same), user need to input name, age and action. I add Leave event for age textbox, if age < 18 years old, the action dropdown box will be disabled. Everything is fine if I move to other control after input age, the action dropdown box will be enable/disabled at once I leave the textbox. My problem is that if I input age and didn't click other control in the form, instead I click the form itself, the Leave event won't get fired, so the action dropdown box cannot be updated (enable or disable). What should I do to always have the Leave event fired? Thanks!

                          N Offline
                          N Offline
                          nikhilhegde007
                          wrote on last edited by
                          #15

                          leave event is fired only when ur focus is left from that control

                          A 1 Reply Last reply
                          0
                          • N nikhilhegde007

                            leave event is fired only when ur focus is left from that control

                            A Offline
                            A Offline
                            Andraw Tang
                            wrote on last edited by
                            #16

                            Yes, when focus only leave the control when I click other control, do you know how can I let focus leave control when I click other place instead of control on the form? Thanks!

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups