Search code examples
vb.netbuttontimer

how to disable button for 3 seconds after button click and enable the button again


how to disable button for 3 seconds after button click and enable the button again by itself? VB.net

I want to disable the button for 3 seconds so users wont abuse the system.


Solution

  • how to disable button for 3 seconds after button click and enable the button again by itself?

    In its simplest form:

    Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Button1.Enabled = False
    
        ' ... do something ...
    
        Await Task.Delay(3000)
    
        Button1.Enabled = True
    End Sub
    

    If the "do something" part is time consuming, then maybe something more like:

    Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Button1.Enabled = False
    
        Await Task.Run(Sub()
    
                           ' ... do something ...
    
                           System.Threading.Thread.Sleep(3000)
                       End Sub)
    
        Button1.Enabled = True
    End Sub