Solved

vb.net - pressing button

Posted on 2011-03-15
9
355 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

914 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

16 Experts available now in Live!

Get 1:1 Help Now