-
Mar 12th, 2020, 03:25 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] Event Lost_Focus
Hi to all,
I have this code in a event (Lost_focus):
Code:
Private Sub TextBox178_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox178.LostFocus
If TextBox178.Text <> "" Then
If Len(TextBox178.Text) = 1 Then
Dim valor As Single = TextBox178.Text
Dim strValor As String
strValor = valor.ToString("0#.#")
TextBox178.Text = strValor
End If
End If
End Sub
I have a lot of textboxes to put this code inside in each event Lost_focus.
Exist a way to do this more fast?
Thanks
-
Mar 12th, 2020, 05:10 PM
#2
Re: Event Lost_Focus
If the event was triggered by textbox178 (btw you really might want to consider naming things with a more meaningful name) then sender will be textbox178 that means you could have written
Code:
Private Sub TextBox178_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox178.LostFocus
dim textbox = DirectCast(sender, TextBox)
If textbox.Text <> "" Then
If Len(textbox.Text) = 1 Then
Dim valor As Single = textbox.Text
Dim strValor As String
strValor = valor.ToString("0#.#")
textbox.Text = strValor
End If
End If
End Sub
If you have multiple textboxes you could add them to the Handles clause e.g.
Code:
Private Sub TextBox178_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox178.LostFocus, TextBox179.LostFocus 'and so on
Alternatively https://docs.microsoft.com/en-us/dot...dler-statement is probably worth a read.
-
Mar 12th, 2020, 06:09 PM
#3
Thread Starter
Frenzied Member
-
Mar 12th, 2020, 07:14 PM
#4
Re: Event Lost_Focus
Originally Posted by sacramento
Hi,
THanks...work great
You might want to also look at turning on Option Strict, lines like
Code:
Dim valor As Single = textbox.Text
will crash if the textbox doesn't contain something that can be converted to a number.
https://docs.microsoft.com/en-us/dot...rict-statement
https://docs.microsoft.com/en-us/dot...tframework-4.8
-
Mar 13th, 2020, 02:06 AM
#5
Thread Starter
Frenzied Member
Re: [RESOLVED] Event Lost_Focus
Hi,
You are right...if i enter a strange number like "8.62.862" the textbox crash, but if I put "ON" Option Strict in properties a lot of errors shoot on code.
Any other way to resolve this?
-
Mar 13th, 2020, 04:46 AM
#6
Re: [RESOLVED] Event Lost_Focus
The errors were already there, you just weren't being told about them.
The issues that came up are all bugs rather than errors, so they will only cause problems in some circumstances, eg: things like the strange number in the textbox can happen accidentally, or the user might have got confused and typed a word.
Bugs are worse than errors for a variety of reasons, including that they are much harder to find, and that in some circumstances your code can be doing the wrong thing without anyone even realising there is a problem (so they think everything is fine and the data is safe to use, but it isn't).
Each issue you are seeing after setting Option Strict On is something that you should be fixing somehow, because all of them can/will cause problems at some point - and dealing with the problems takes much more effort than fixing the code in advance.
How you fix each issue varies, but thankfully Visual Studio is good at giving guidance: when you put the cursor on one of the lines with an issue, you should get a little pop-up message which you can use to see options for possibly solving it.
The automatic guidance isn't perfect, and may not include the best options. In the case of the textbox, using TryParse as PlausiblyDamp suggested is a good way - but a better way might be to use a NumericUpDown (with the arrows turned off if you want) for entering the number, because that will automatically make sure only numbers are entered.
-
Mar 13th, 2020, 07:13 AM
#7
Thread Starter
Frenzied Member
Re: [RESOLVED] Event Lost_Focus
Hi,
TryParse it's OK and work very well
Thanks
-
Mar 13th, 2020, 10:02 AM
#8
Re: [RESOLVED] Event Lost_Focus
The documentation clearly states that application developers should not be handling the LostFocus event.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|