TextBox_Leave event only fired when move mouse to other control in vb.net 2005
-
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!
-
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!
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.
-
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.
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?
-
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.
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?
-
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?
-
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?
-
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:
Would you please give me an example how to use it? Thanks!
-
Would you please give me an example how to use it? Thanks!
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:
-
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:
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.
-
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.
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 theActiveControl
property toNothing
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:
-
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 theActiveControl
property toNothing
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:
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?
-
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?
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:
-
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:
Eddy, Thank you very much, I will try tomorrow and let you know the result.
-
Eddy, Thank you very much, I will try tomorrow and let you know the result.
-
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!
leave event is fired only when ur focus is left from that control
-
leave event is fired only when ur focus is left from that control
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!