views:

225

answers:

2

How do you disable CBitmapButton? I tried using m_bitmapbutton.EnableWindow(false); but it doesn't work. It still fires an event.

What I'm trying to do is prevent Button A from firing Event A if Event B is executing (from Button B). So in Event B, I want to disable Button A.

+1  A: 

Here's a list of things to check:

  • Did you define an image for the bitmap button's disabled state?
  • Are you sure the event is fired?
  • Event B isn't in a thread is it?
  • Are you doing an m_bitmapbutton.EnableWindow(FALSE) at the start of Event B and m_bitmapbutton.EnableWindow(TRUE) at the end?
  • Are you doing a BN_CLICKED Notification ()? Or is it maybe a different notification such as WM_LBUTTONDOWN Notification ()?
  • Are you sure that the variable m_bitmapbutton is properly associated with your button A? Could you try with GetDlgItem(IDC_BUTTON_A)->EnableWindow(FALSE);

You could always just set a boolean member variable to indicate that Button A is disabled, and ignore any events sent to it when this boolean member is set.

It may be easier to use this class from CodeProject.

Brian R. Bondy
yes to both questions. is this a bug? is there other options besides using the class?
I think you'd be better off with the CodeProject button
Brian R. Bondy
Event B isn't in a thread is it? Are you doing an m_bitmapbutton.EnableWindow(FALSE) at the start of Event B and m_bitmapbutton.EnableWindow(TRUE) at the end?
Brian R. Bondy
It isn't a thread. And yes to your second question.
Are you doing a BN_CLICKED Notification ()? Or is it maybe a different notification such as WM_LBUTTONDOWN Notification ()
Brian R. Bondy
Are you sure that the variable m_bitmapbutton is properly associated with your button A? Could you try with GetDlgItem(IDC_BUTTON_A)->EnableWindow(FALSE);
Brian R. Bondy
yes it is. GetDlgItem(IDC_BUTTON_A)->EnableWindow(FALSE); --- event is still fired
Could you confirm, does button A appear as disabled?
Brian R. Bondy
In the LoadBitmap function, I associated a bitmap for its disabled state. That bitmap appears when I run my program (@ the part where the I disabled the button). I guess it's disabled but Event A is still fired (after Event B has finished executing) when I clicked Button A when Event B is executing.
Ok I think that is a bug in CBitmapbutton, please use my suggestion about the boolean variable.
Brian R. Bondy
Thanks, I will try that. I also tried displaying a MessageBox after Event B has finished executing. That seemed to fix my problem. But then I again, those MessageBoxes can get annoying. :)
I have tried your suggestion but my problem was not solved. Maybe it's really a bug.
If the flag suggestion I mentioned does not work, then it seems like it's a bug in your code. The flag should be set inside button B, and in button A it should be checked. If it is set then you should return right away from the button A event handler.
Brian R. Bondy
Hmmm.. That's exactly what I did.
ya so then the event should still be fired, but it should not reach the code that you are protecting against.
Brian R. Bondy
A: 

Hi Sal,

I suppose the problem is that while you are attending the Event B, the messages for this window are not being dispatched. So they are being queued and dispathched after you finish to attend the Event B and you have enabled the button A again. Something like that:

  1. OnBnClickedButtonB
  2. buttonA.EnableWindow(FALSE) (mouse events for the window are being queued and not dispatched)
  3. buttonA.EnableWindow(TRUE)
  4. ~OnBnClickedButtonB (the application dispatch queued events so the button is enabled again)

This is not just a problem with a CBitmapButton...it should happed with any other button (I haven't tried myself).

A possible workaround could be dispatching mouse events for this window before enabling the button again...although I didn't like too much do this kind of thigs...maybe someone can give you a better solution:

void CMyDlg::OnBnClickedButtonB()
{
    m_buttonA.EnableWindow(FALSE);

    //Process button B clicked event 

    MSG msg;
    while(PeekMessage( &msg, 
       GetSafeHwnd(), 
       WM_MOUSEFIRST, 
       WM_MOUSELAST, 
       PM_REMOVE))
    {
     TranslateMessage(&msg);
     DispatchMessage(&msg);  
    }

    m_buttonA.EnableWindow(TRUE);
}

Hope this help!

Javier

Javier De Pedro