Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button disable and enable

I have a vb.net based windows application, where when "GO" button is clicked a bunch of data is loaded into DB. So in my application as soon as "GO" button is clicked I want to just disable it and would like to enable it back when the uploading has completed. Now in my specific method for btnGo_Click() I have:

btnGo.Enabled = False

as first line and

btnGo.Enabled = True

as last line in the same method.

But I fail to understand why the "GO" though appears as being disabled still allows click when processing is going on. Also if I remove the last line, it gets disabled permanently and doesn't allow the click event.

Kindly suggest what am I doing wrong?

Edit (Dated: 25th Jan 2012): I made changes as suggested by our collegues, but I am facing a new issue here. I am facing an issue where the textbox gets updated but not always. I have updated my textbox in "_ProgressChanged" event of the background worker thread. In my case if there is 10 records uploaded. Then there are 10 lines of updates that are expected in the texbox. But only few lines are shown in the textbox. Is it the repaint issue again? Kindly suggest...Because all other things are done as per your suggestion

like image 807
Justin Samuel Avatar asked Jan 23 '12 17:01

Justin Samuel


People also ask

How do you change a button from enable to disable after click?

To disable a submit button, you just need to add a disabled attribute to the submit button. $("#btnSubmit"). attr("disabled", true); To enable a disabled button, set the disabled attribute to false, or remove the disabled attribute.

Which method is used to disable a button?

In UI Dialog box, button as default class called ui-button so focus on it. Create a function that should trigger dialog box in ready that is on page load. Then use jQuery method prop('disabled', true) to disable that button with class ui-button.


2 Answers

You're not doing anything wrong. The problem is that the UI doesn't get updated until the code inside of your event handler method finishes executing. Then, the button is disabled and immediately enabled in rapid sequence.

That explains why if you forget to reenable the button control at the end of the event handler method, it is still disabled—because you told it to disable the button in the first line of the method.

This is a classic case of why you should never perform long-running computational tasks inside of an event handler method, because it blocks the UI from being updated. The computation actually needs to happen on a separate thread. But don't try to create the thread manually, and definitely don't try to update your UI from a separate thread. Instead, use the BackgroundWorker component to handle all of this for you automatically. The linked MSDN documentation has a great sample on how to use it.

Disable the button before starting the BackgroundWorker, and then re-enable it in its Completed event, signaling the completion of your database load.

like image 137
Cody Gray Avatar answered Oct 01 '22 15:10

Cody Gray


Since you're trying to execute a function that can take some time, I'd advise you to make use of threading. In .NET there's a BackgroundWorker component which is excellent for performing tasks asynchronous.

On button click, invoke the BackgroundWorker like this:

if not bgwWorker.IsBusy then
   btnGo.enabled = false
   bgwWorker.RunWorkerAsync()
end if 

And use the completed event to enable the button again:

Private Sub bgwWorker_DoWork(ByVal sender As System.Object, _
                 ByVal e As System.ComponentModel.DoWorkEventArgs) _
                 Handles bgwWorker.DoWork
' Do your things    
End Sub

Private Sub bgwWorker_RunWorkerCompleted(ByVal sender As System.Object, _
                         ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _
                         Handles bgwWorker.RunWorkerCompleted
' Called when the BackgroundWorker is completed.
btnGo.enabled = true
End Sub

In the example above, I've used bgwWorker as the instance of a BackgroundWorker.

like image 38
Rhapsody Avatar answered Oct 01 '22 13:10

Rhapsody