Track/Prevent button clicks

prevent button from being clicked twice or more in succession

Page 1 of 1

4 Replies - 2621 Views - Last Post: 15 June 2008 - 09:11 AM Rate Topic: -----

#1 Locuranis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-June 08

Track/Prevent button clicks

Posted 14 June 2008 - 07:47 AM

Hello. Beginner student to VB. I've actually two problems, but the first is more important. I would like to prevent some code from firing more than once if a button is clicked multiple times, say accidentally or so. The original assignment is to create a calculator, which I've done for the most part, but I want to include error prevention even if not needed for the overall assignment. Currently I'm guessing that if one of the operation buttons (+, -, *, or /) is clicked twice or more in a row, that code will fire again, which will likely lead to run errors. Below is the code for the add button, since the others are all basically the same.

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

   'If no other operation button is clicked, store display into variable
   If (Calculated = False) Then

	  'Check to see if anything is in the display and convert to double
	  If (tboxDisplay.Text = Nothing) Or (tboxDisplay.Text = "-") Then
		 FirstValue = 0
	  Else
		 FirstValue = CDbl(tboxDisplay.Text)
	  End If

   Else 'if another operation was clicked, perform that operation first

	  'ditto the check/convert
	  If (tboxDisplay.Text = Nothing) Or (tboxDisplay.Text = "-") Then
		 tmpStore = 0
	  Else
		 tmpStore = CDbl(tboxDisplay.Text)
	  End If

	  FirstValue = Calculations(FirstValue, tmpStore, Operation)
   End If

   Operation = "+"
   Calculated = True
   tboxDisplay.Text = ""
End Sub


So I'm wondering how to prevent the code from executing if the add button, (or any operation button) is clicked again before the user clicks a number button.

The second problem is purely aesthetics and more for my own curiosity. Currently as soon as a user clicks an operation button, the first value is cleared from the display. I know I could probably store that to a secondary display, but I was wondering if there was a way to keep that value or the calculated value in case of multiple operations in the display until a number button is pressed. Again, not a priority, just wondered if/how it would be possible. The code for one of the number buttons is below.

Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click

   'buttons just append numbers to what is already in the display
   'so check if the equal sign was pressed to erase the previous value first
   If (Reset = True) Then
	  tboxDisplay.Text = "1"
	  Reset = False
   Else
	  tboxDisplay.Text &= "1"
   End If
End Sub


Any help on this matter is greatly appreciated. Thanks.
- LN

Is This A Good Question/Topic? 0
  • +

Replies To: Track/Prevent button clicks

#2 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Track/Prevent button clicks

Posted 14 June 2008 - 11:10 AM

Use a flag to ensure that your code runs only once.
The implementation will be something like this:

Private flag as Boolean

Private Sub Form_Load()
   flag = False
End Sub

Private Sub TheProcThatNeedsToBeExecutedOnce()
   If flag= True Then Exit Sub

   ' Your Code Goes Here
   flag = True
End Sub


You can then use a Reset Button (C or CE for a calculator) to set flag to False to enable the code again.
Was This Post Helpful? 0
  • +
  • -

#3 Locuranis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-June 08

Re: Track/Prevent button clicks

Posted 14 June 2008 - 09:13 PM

I understand the concept with the flag, but I was afraid doing it as suggested would cause a problem is a user is trying to use multiple operations, i.e. 2 - 2 + 1. But looking at it again, perhaps I can set a flag or variable to true after the operation button is clicked, and have the number buttons set it to false, preventing the code from running until a number button is pressed.

In any case, your suggestion gave me a new perspective to think about. Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Track/Prevent button clicks

Posted 15 June 2008 - 08:57 AM

or you could redesign the program in such a way that clicking the operator button multiple times does not make a difference.

Use a operator global variable and whenever an operator button is clicked, set the global variable to the appropriate operator.

If the user clicks the button, nothing happens as the same operator is set to the operator variable.

Write your calculator along these lines.
Almost all basic calculators use this technique.
Was This Post Helpful? 0
  • +
  • -

#5 Locuranis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-June 08

Re: Track/Prevent button clicks

Posted 15 June 2008 - 09:11 AM

I wound up doing something akin to that after my post. I also streamlined it a bit as I took the operations out of the button subs where I originally had them and put it into a separate, single sub using a global Boolean variable. So not only will a double click just exit the button sub, but in the case of hitting a different operation button before a number one, it changes the last operation function to use. During that I wound up solving my other, less important problem as well. I just needed to get turned onto the right train of thought. Again, your suggestion was most helpful in that, and I do thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1