Solved

vb.net - pressing button

Posted on 2011-03-15
9
347 Views
Last Modified: 2012-05-11
hello there,
I would like to create a little function that can help me with pressing button many times..
for example if I press a button more than 5 times in 30 seconds to tell me "you cant press the button more than X times in less than X seconds"
how can I do something like that?
0
Comment
Question by:XK8ER
  • 5
  • 4
9 Comments
 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35143993
Hi,

You can use 2 variables to control that,

In your Form Declarations

Public intPressedTimes as Integer = 0
Public dteStartTime as Datetime

In you button  CLICK  event, add this code

If intPressedTimes = 0 Then 'is the first time the button is pressed or is another cycle
    dteStartTime = Datetime.Now  'Get the start Time of the first time the buttons was pressed
End if

If intPressedTimes > 5 Then 'Validates the Maximun Number of Time
  If (Datetime.Now - dteStartTime).TotalSeconds > 30 Then 'Validate the maximun number of seconds
      Msgbox("you cannot press the button more than 5 times in less than 30 seconds")
      Exit sub
  End if
Else
   'Finally reset the variable if the Seconds are more than 30 and the Pressed Times is not reach the   Maximum
   If (Datetime.Now - dteStartTime).TotalSeconds > 30 Then
      intPressedTimes = 0
   End if
End If

Hope that helps
0
 
LVL 13

Accepted Solution

by:
gamarrojgq earned 500 total points
ID: 35144001
Sorry one thing was missed, increment the intPressedTimes variable, here is the code of the CLICK event fixed

If intPressedTimes = 0 Then 'is the first time the button is pressed or is another cycle
    dteStartTime = Datetime.Now  'Get the start Time of the first time the buttons was pressed
End if

intPressedTimes += 1

If intPressedTimes > 5 Then 'Validates the Maximun Number of Time
  If (Datetime.Now - dteStartTime).TotalSeconds > 30 Then 'Validate the maximun number of seconds
      Msgbox("you cannot press the button more than 5 times in less than 30 seconds")
      Exit sub
  End if
Else
   'Finally reset the variable if the Seconds are more than 30 and the Pressed Times is not reach the   Maximum
   If (Datetime.Now - dteStartTime).TotalSeconds > 30 Then
      intPressedTimes = 0
   End if
End If
0
 
LVL 1

Author Comment

by:XK8ER
ID: 35144010
its not really showing the msgbox..
0
 
LVL 1

Author Comment

by:XK8ER
ID: 35144030
okay its supposed to be like this


>>If (DateTime.Now - dteStartTime).TotalSeconds < 30 Then 'Validate the maximun number of seconds
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35144076
Ok you are right, sorry for the wrong symbol

With that change, it was what you were looking for?
0
 
LVL 1

Author Comment

by:XK8ER
ID: 35144082
well for example if i set it to 30 seconds.. and I click 10 times every 45 seconds then click 8 times within 5 seconds it wont show the msgbox..
0
 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35144106
ok, do you want that the button can be pressend 5 times every 30 seconds? this will mean that in a minute, you can pressed 4 times from 1 to the second number 30 and then other 4 times from second 31 to 60, and only if you try to press more than 5 times each 30 seconds cycle the messages shows up?

Or do you want that if the button was pressed 4 times before 30 seconds, and then is pressed 2 more times since 30 seconds has pass already, it will show the message?
0
 
LVL 1

Author Comment

by:XK8ER
ID: 35144122
ok got it.. had to change this too

>>If (DateTime.Now - dteStartTime).TotalSeconds <= 30 Then
0
 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35144150
ok, that works the way you want it ?
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now