如何在vb.net中实现按钮按下时间限制
我想要一个代码,当我按下按钮,然后按钮是不可能点击 24小时后,按钮再次可用。例如:如何在vb.net中实现按钮按下时间限制,vb.net,Vb.net,我想要一个代码,当我按下按钮,然后按钮是不可能点击 24小时后,按钮再次可用。例如: 单击按钮时,禁用按钮并启动计时器(计时器应具有24小时间隔),当它滴答作响时,启用按钮并停止计时器。如上所述,根据您的需要,有许多方法可以做到这一点。下面只是一个简单的例子,应该会有所帮助 Private ButtonTimer As New Timer Private ButtonCountDown As Integer Private Sub Button1_Click(sender As Object,
单击按钮时,禁用按钮并启动计时器(计时器应具有24小时间隔),当它滴答作响时,启用按钮并停止计时器。如上所述,根据您的需要,有许多方法可以做到这一点。下面只是一个简单的例子,应该会有所帮助
Private ButtonTimer As New Timer
Private ButtonCountDown As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Disable Button
Button1.Enabled = False
'Set Countdown
ButtonCountDown = 24
'Setup Timer
AddHandler ButtonTimer.Tick, AddressOf ButtonTimer_Tick
ButtonTimer.Interval = 1000 * 60 * 60 'Every 1 Hour
ButtonTimer.Start()
End Sub
Private Sub ButtonTimer_Tick(ByVal obj As Object, ByVal e As EventArgs)
'Decrement ButtonCountDown and if not zero we can just leave and do nothing.
ButtonCountDown -= 1
If Not ButtonCountDown = 0 Then Exit Sub
'We have reached zero, stop timer and clean up.
ButtonTimer.Stop()
RemoveHandler ButtonTimer.Tick, AddressOf ButtonTimer_Tick
ButtonTimer.Dispose()
'Enable Button
Button1.Enabled = True
End Sub
以下是重要的几点:
ButtonCountDown = 24
ButtonTimer.Interval = 1000 * 60 * 60 'Every 1 Hour
上述示例将每小时检查一次计时器,并从24开始倒计时,因此为24小时
出于测试目的,更改为分钟:
ButtonCountDown = 2
ButtonTimer.Interval = 1000 * 60 'Every 1 Minute
现在该按钮将禁用2分钟(计时器每分钟检查一次)
出于测试目的,更改为秒:
ButtonCountDown = 20
ButtonTimer.Interval = 1000 'Every 1 Second
现在,该按钮将禁用20秒(计时器每秒检查一次)。您是否尝试过使其无法单击5秒?这将更容易在一开始开发。请花几分钟阅读。其他重要信息在您的上下文中呈现?这是网络吗?始终打开的应用程序?将随机打开和关闭的表单?与数据库相关的东西?根据上下文的不同,有许多不同的答案。