Removing leading zeroes from a textfield when it loses focus

20,390
FirstTableTxt.Text = FirstTableTxt.Text.TrimStart("0"c)
Share:
20,390
Paul Williams
Author by

Paul Williams

Updated on February 27, 2020

Comments

  • Paul Williams
    Paul Williams about 4 years

    I'm trying to have a Textfield lose it's leading zeroes when the user leaves the field. I made the following "Leave" event but it's not working. It works for the first half however to set it back to 1 if it's blank or if a user entered 0.

    I tried following the advice on this answer, but it didn't work:

    Removing leading zeroes from a string

    Private Sub FirstTableTxt_Leave(sender As System.Object, e As System.EventArgs) Handles FirstTableTxt.Leave
        If FirstTableTxt.Text = "" Or FirstTableTxt.Text = "0" Then
            FirstTableTxt.Text = "1"
        End If
    
        FirstTableTxt.Text = Convert.ToString(CInt(FirstTableTxt.Text))
    End Sub
    

    EDIT: I see where I went wrong with this. I was thinking that Convert.Toxxxx returns it to the same variable.

  • Joel Coehoorn
    Joel Coehoorn about 12 years
    @PaulWilliams - forgot the .Text property. check it again.