Captain Caboose Captain Caboose - 1 month ago 6
Vb.net Question

Keeping it in comma order

I am trying to get the value to stay in a comma order like 123,456,789 when subtracting the value. Here is the current code:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim health As Integer
health = lblHPPlayer.Text
lblHPPlayer.Text = Val(health) - 1000
End Sub


'That is the value of what I wanna change*

lblHPPlayer.Text = "9,850,000"

Answer

Create a form-level property called Health. You can make it private. Now any time you need to update Heath then also update the text in lblHPPlayer. That way you never need to parse the text - it only ever just shows you the value of a proper integer property.

Something like this:

Private _health As Integer

Public Property Health() As Integer
    Get
        Return _health
    End Get
    Set(ByVal value As Integer)
        _health = value
        lblHPPlayer.Text = _health.ToString("N0")
    End Set
End Property

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Me.Health -= 1000
End Sub