dpeneireiro

Hello everyone, i'm a begginer in vb programation and i need to know how to really disable a button. The next example shows what i pretend to do... I have a form with 2 buttons and when i press the button 1, the button 2 should stay disabled for about 5 seconds. When i press the button 2 a message is shown.

The problem is that if i click in the secon button when it is disable, when he passes to enable, the message is shown. Seem that the event of clicking in the button 2 whaits for the button 2 pass to the enable state.

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Me.Button2.Enabled = False

'Me.Button2.Visible = False

'Me.Button2.SendToBack()

System.Threading.Thread.Sleep(5000)

'Me.Button2.Visible = True

'Me.Button2.BringToFront()

Me.Button2.Enabled = True

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

'if Me.Button2.Enabled = True Then

MessageBox.Show("Message!!!")

'End If

End Sub

End Class

I also try to put the second button invisible and disable but when he passes to enable, the message is shown.

Sorry the errors...

Thank's



Re: Visual Basic Express Edition disable and enable a button

spotty

OK, part of you problem here is using the thread.sleep method. Don't !!!!

By putting the thread to sleep you are in effect stopping your application from doing anything and that includes processing other button requests during this time.

What you should do is use a timer. When you click button1 it would disable the Button2 and start a timer control with an interval of 5000. In the timer tick event you would enable the Button2 and stop the timer. This will then work.

If you are adamant, that this is the code you want to use and I strongly discourage use of thread.sleep then try using an application.dovevents statment between the button2.enabled = false and thread.sleep statements.





Re: Visual Basic Express Edition disable and enable a button

dpeneireiro

hello, this is only an example, my aplication dont do the sleep but processes a recognition of modules in a lan and that process have a time duration very large and not always the same, so i ¡¯dont know the time i have to wait.

During the recognition, the program can't do anything else, so i disable all other buttons but this d'ont work because when i click the button nothing happens but when the button pass to enable the event is called.





Re: Visual Basic Express Edition disable and enable a button

nobugz

The key problem here is that your UI is going dead, whether by sleeping or by spending a bunch of time running code. Windows sends your program the "Button is clicked" message but you're not picking it up because you're busy elsewhere. As soon as you're done with the lengthy task, you re-enable the button and go idle. At that point, you get the click message and it will run the Click event handler.

The solution is to prevent your UI thread from becoming unresponsive. Look at the BackgroundWorker component for an easy way to run a background thread.





Re: Visual Basic Express Edition disable and enable a button

spotty

Your example used thread.sleep which is a bad thing to do for most purposes.

You want to use events to control waits such as a delay. Example code will keep the UI active during the 5 second wait.

Code Snippet

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Interval = 5000
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Button2.Enabled = False
Timer1.Start()
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Me.Button2.Enabled = True
Timer1.Stop()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
MessageBox.Show("Foo2")
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
MessageBox.Show("Bar")

End Sub
End Class


That said, if you are talking about other processes running in background, then using a backgroundworker process will work and you would then use the RunWorkerCompleted event to re-enable the button.





Re: Visual Basic Express Edition disable and enable a button

Tall Dude

The following may not be preferred by the

experts, but will also work to keep the UI active:

Private Sub Button1_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Button1.Click

Dim t As TimeSpan = Now.TimeOfDay

Button2.Enabled = False

t = t.Add(New TimeSpan(0, 0, 5))

Do While t > Now.TimeOfDay

Application.DoEvents()

Loop

Button2.Enabled = True

End Sub






Re: Visual Basic Express Edition disable and enable a button

spotty

Wouldnt advise use of this unless you have a short duration and a very specific reason for not using events.

Application.doevents kills performance like crazy. You loop will run literally thousands of times during even a short period of time. Using events is a much better approach to go for waiting.

This approach is definately better than thread sleep but not as good as a timer in my opinion.

A timer can be used for virtually any real duration without a performance hit or detrimental UI effect. Thread sleep detrimental performance and UI effect, looping (potentially detrimental performance)

That said, it will work.





Re: Visual Basic Express Edition disable and enable a button

dpeneireiro

Hello,

thank's everyone, the sugestion of the instruction

Application.DoEvents()

works fine for now. Now, i simpli run this instruction befor make the buttons enabled again.

For now its god but i will study the backgroundworker to do this with more fiability... and beauty.

Thank's





Re: Visual Basic Express Edition disable and enable a button

spotty

Think seriously before putting doevents in code especially within a loop and do not use thread.sleep if at all possible.

Both are often misused and result is horrendously poor performance.