5 Replies - 323 Views - Last Post: 29 September 2011 - 06:38 AM Rate Topic: -----

#1 Xposure  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-September 11

Subtract to 0, not past.

Posted 29 September 2011 - 06:18 AM

I recently registered here and am looking for a solution to a small problem I am having.

I am making a small application that keeps track of life points for a game I play. I have most of the code assembled, but I am having problems with the subtraction part. When I subtract to 0, I want it to stop there and not pass into the negative numbers.

I'm not really sure which part of the code I should post.

Currently, I only have this (For my subtraction button):

    Private Sub btn2min_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn2min.Click
        lblLP2.Text = lblLP2.Text - txtb2.Text
    End Sub


Thanks to anyone who can help, and if you need anymore information I'll post it.

Is This A Good Question/Topic? 0
  • +

Replies To: Subtract to 0, not past.

#2 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Subtract to 0, not past.

Posted 29 September 2011 - 06:21 AM

A simple if to check if the value is below zero should do the trick. If the value is less than zero, just set it to zero.
Was This Post Helpful? 0
  • +
  • -

#3 Xposure  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-September 11

Re: Subtract to 0, not past.

Posted 29 September 2011 - 06:28 AM

Oh wow. I feel silly.

I guess I just needed to hear someone say it, in order for it to make sense.

Thank you! :)
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Subtract to 0, not past.

Posted 29 September 2011 - 06:30 AM

And another thing to take care of is that label values are strings and it is not advices to do math operation with them. So first parse them to appropriate numeric data type or make a Val() sub to work with them...
 If Val(lblLP2.Text) - Val(txtb2.Text) >= 0 Then
     lblLP2.Text = Val(lblLP2.Text) - Val(txtb2.Text)
End If


Just joke question: So in your game someone click on a button to reduce its life?!
Was This Post Helpful? 0
  • +
  • -

#5 Xposure  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-September 11

Re: Subtract to 0, not past.

Posted 29 September 2011 - 06:35 AM

The goal of the game, is to simply reduce the other players life to 0. It's a card game called Yugioh. I was getting tired of using a calculator to keep track of the life points.
Was This Post Helpful? 0
  • +
  • -

#6 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: Subtract to 0, not past.

Posted 29 September 2011 - 06:38 AM

Still got all my yugioh cards. ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1