Solved

vb.net - pressing button

Posted on 2011-03-15
9
383 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

691 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