Results 1 to 8 of 8

Thread: [RESOLVED] Event Lost_Focus

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2004
    Posts
    1,345

    Resolved [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

  2. #2
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,208

    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.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2004
    Posts
    1,345

    Re: Event Lost_Focus

    Hi,

    THanks...work great

  4. #4
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,208

    Re: Event Lost_Focus

    Quote Originally Posted by sacramento View Post
    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

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2004
    Posts
    1,345

    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?

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,440

    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.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2004
    Posts
    1,345

    Re: [RESOLVED] Event Lost_Focus

    Hi,

    TryParse it's OK and work very well

    Thanks

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,016

    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
  •  



Click Here to Expand Forum to Full Width