Question Button Count limit

DukeMeister44

Member
Joined
Sep 14, 2020
Messages
6
Programming Experience
1-3
Hi,

Does anyone know how to define the acceptable range of button presses in Visual Studio, say that I click a button ten times(depending on the upper limit is, 10 is an example) It should tell me that I have reached the limit of acceptable clicks before either returning a message or disabling the button with the user-code controlling the button, until another button enables it again, etc?

I want to define a range and either wrap this in a Try-Catch Statement or an if-statement or better to reduce the chances of my application crashing.
  1. [FONT=courier new]Label1.text = Val(label1.text) + 1 'counts button clicks shown in label[/FONT]
Along with the range in mind, I've been trying to figure out how to better use the TabControl and TabPages with Button clicks. (like an app install wizard in Windows) and stepping it with each click from 1 to the upper limit defined in a 32-bit signed variable/integer.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
You don't use controls for data storage. They are for interaction with the user. A Label is specifically for displaying text to the user and that is all it should be used for. As a result, you should NEVER be pulling data out of a Label; only putting it in.

If you want to store a number then use a numeric variable. You should declare a field of type Integer and named something like clickCount. You initialise it to zero and then increment it in the Click event handler of your Button. You then just use a simple If statement to determine whether it has reached your threshold. You should obviously not be throwing any exceptions because there's nothing exceptional here. I would suggest that, when the user makes their final permitted click, you display a message at that point and disable the Button.
 

DukeMeister44

Member
Joined
Sep 14, 2020
Messages
6
Programming Experience
1-3
You don't use controls for data storage. They are for interaction with the user. A Label is specifically for displaying text to the user and that is all it should be used for. As a result, you should NEVER be pulling data out of a Label; only putting it in.

If you want to store a number then use a numeric variable. You should declare a field of type Integer and named something like clickCount. You initialise it to zero and then increment it in the Click event handler of your Button. You then just use a simple If statement to determine whether it has reached your threshold. You should obviously not be throwing any exceptions because there's nothing exceptional here. I would suggest that, when the user makes their final permitted click, you display a message at that point and disable the Button.

Do you have some sample code for that, I just don't know what I am supposed to be doing. Maybe I'm just a bit stupid, to be honest.

I tried this:

Buton click disables button after one click:
 'changing the number from 1 to 5 does nothing, if statements don't do anything'
 Static hits As Integer
        hits += 1
        Button1.Enabled = False
 
Last edited by a moderator:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
The first thing you should do is not use profanity in public posts. The second thing you should do is read what I said to do and do it. I said this:
You then just use a simple If statement to determine whether it has reached your threshold.
I don't see any sign of that in your code so why would it work?

One of the biggest problems beginners face is that they treat code like it is some kind of magic with no connection to the real world instead of a formal implementation of logic. If you treat it like the implementation of logic that it is then, instead of trying to pluck code out of the air, you come up with the logic first and then you write code to implement each individual step of that logic. The logic would be exactly the same if you were doing this as a completely manual process, so start by thinking about that, i.e. how would you do this manually. You might have a piece of paper on which you write the number times the user has clicked the button. Each time they clicked the button, you would increment the number on the paper. You would then check the number on the paper to see whether it had reached your threshold value and, if it had, you would disable the button. That 's the logic of the entire process and it took exactly zero programming experience. Now that you have the logic, you can look at it one step at a time and think about what VB code is equivalent to that logical step. If you take the time to write out your algorithm in pseudo-code then your logic will be even closer to actual code. One of the features of VB is that it reads much like an English sentence, so the step from pseudo-code to VB is not that big.

The lesson here is to do things in steps and always consider logic before code. You don't need any programming experience to come up with the logic so being a beginner is no impediment. If you are having trouble writing code and posting here for help, you should always be able to provide us with your logic and the specific point in that logic that you're having trouble.
 
Top Bottom