Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Button Enable/Disable Problem 5

Status
Not open for further replies.

ahlahkeet

Programmer
Joined
Sep 24, 2002
Messages
4
Location
US
Hello All,

In my software, I have several buttons which the code may enable and disable at various times. My problem is, while a button is disabled (button.enabled = FALSE), if the button is clicked, the code in the button's Click() event will execute once the button has been re-enabled.

So, how can I get the code to 'ignore' or 'forget' these clicks that occur when a button is disabled?

Thanks,
Dave

 
I think you may be mistaken about the behavior of the command button. The click event is not queued during the time the button is diabled. Maybe you have another piece of code that is executing the click event code or some similar code.
 
Hmmm. . .

Here's a simple example of what I'm talking about.

Consider a form with 2 command buttons, command1 and command2. Code follows:

Private Sub Command1_Click()
If Command1.Enabled Then
MsgBox "You clicked me!!!"
End If

End Sub

Private Sub Command2_Click()
Dim i As Long

If Command1.Enabled Then
Command1.Enabled = False

For i = 1 To 100000000
Next i

Command1.Enabled = True
End If

End Sub


So when this executes, pressing the command2 button disables button command1. Command1 stays disabled for a few seconds (I simulated time passing with the for loop), then re-enables. While Command1 is disabled, if you click it, the message box will appear once the button has been re-enabled.

This is the type of problem I'm having in my actual code.
 
Try using modular level flags to determine whether or not to execute certain code. Below is a 30 solution to the problem you presented in your last post.

Private m_bolSkipIt As Boolean

Private Sub Command1_Click()
If Command1.Enabled Then
If m_bolSkipIt Then
m_bolSkipIt = False
Else
MsgBox "You clicked me!!!"
End If
End If

End Sub

Private Sub Command2_Click()
Dim i As Long

If Command1.Enabled Then
m_bolSkipIt = True
Command1.Enabled = False

For i = 1 To 100000000
Next i

Command1.Enabled = True
End If

End Sub
Thanks and Good Luck!

zemp
 
Ah - slightly different from what you originally suggested. What you are encountering here is an issue related to the single threaded nature of VB.

Basically, you've got a tight loop in Command2 right after disabling Command1, and then you reenable it right after the loop.

The mouse click gets placed into the Operating Systems message queue, but the operating system doesn't get a chance to insert it into the application's message queue whilst this loop is running. Once the loop is over, and the app is idling again (which is once the whole Command2_CLick code is exitted) the OS inserts any pending messages into the application's queue.

In your example, this happens to be a click on Command1, which has been reenabled by the time the message actually arrives.

You can fix this by inserting a DoEvents into the loop (loathe though I am to advise on using DoEvents)
 

A simple doevents in the loop will allow your application to process the messages that are passed to it by the system. Which in turn means that the click event will be processed while the command button is disabled (enabled=false) instead of being placed upon the stack to be processed when the program becomes responsive again.

I hope this helps. Good Luck
 
Thanks everyone for the quick responses! I think I'm starting to understand.

Will the DoEvents function clear out the events queue?

I was just using the for loop as an example; perhaps that wasn't the best choice. My actual software is an HMI for an industrial lathe. Basically, while the machine is homing itself, I disable most buttons on the main control panel so the operator doesn't interfere with the homing. Still, if the operator clicks on the screen a few times, I don't want those button(s)' functions automatically running after the buttons are re-enabled.

So I guess my question really is how do I not allow clicks on the disabled buttons from entering the event queue? Will DoEvents do the trick?

Thanks,
Dave

 
Why not hide the buttons? Can't click what ya cant see :)
 
All,

I've come up with a satisfactory explaination with all the info everyone has provided. Thanks everyone!

Dave

 
Just want to add a general tip in this area. If I need to disable several buttons on a form while something like this is happening, I place the buttons on a frame control and just disable the frame instead of going through each button. You can even hide the frame to hide all of the buttons at once. If you add buttons to the frame, you dont' have to change your code to hide or disable the new buttons. Also, the frames border type and background colors can be set so that the user doesn't even see it, you just use it to ease control of the buttons (or any other controls placed on the frame for that matter)

 

dragnut: The same thing probably would happen, as the control may not have a chance to hide until the loop is done. You would still need a DoEvents.

ahlahkeet: I think some stars are due here..... [/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 

Thankyou for doing so![thumbsup2]

But, ah, I didn't mean for me...I didn't offer much help[ponder]

[/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
I think
command1.value=false

will do what you want :-)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top