Solved

Push Buttons

Posted on 2003-10-27
10
862 Views
Last Modified: 2013-11-20
Hi everybody,

I made a simple MFC Dialog based application. In this i created two push buttons. I want to toggle the state of the button if it is being clicked.

I added these to command handlers to act on each click on the button.

void CDialogDlg::OnBtn1()
{
  static bool b_Btn1 = !b_Btn1 ;  // to trace the state of the button

  m_Btn_Right.SetState(bBtn1) ; // I tried 0x004 and FALSE also
}

void CDialogDlg::OnBtn2()
{
  static bool b_Btn2 = !b_Btn2 ;  // to trace the state of the button

  m_Btn_Right.SetState(bBtn2) ; // I tried 0x004 and FALSE also
}

when you click on one button it is working fine.... But first clicking on Btn1 and then clicking on Btn2 (or vice-versa) changes the state of both buttons.......

Can anybody figure it out why it happens so ???


Dennis
0
Comment
Question by:dennis_george
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 5

Author Comment

by:dennis_george
ID: 9625986
hi,

 I am using VC 6

Dennis
0
 
LVL 16

Expert Comment

by:_nn_
ID: 9626010
You're referencing the same object, so it's normal. I would try :

void CDialogDlg::OnBtn1()
{
  static BOOL b_Btn1 = !b_Btn1 ;  // to trace the state of the button

  m_Btn_Left.SetState(bBtn1) ;  // the LEFT button
}

void CDialogDlg::OnBtn2()
{
  static BOOL b_Btn2 = !b_Btn2 ;  // to trace the state of the button

  m_Btn_Right.SetState(bBtn2) ; // the RIGHT one
}
0
 
LVL 5

Author Comment

by:dennis_george
ID: 9626168
Hi.....

 I like to add one more thing.......

 If I change the state of a button, as pressed (or DOWN), and then make the dialog out of focus then the state of the button changes automatically(i.e. UP state) .....

(e.g.  if Btn1 is DOWN and I pressed Btn2 then the OnBtn1() is called first and then OnBtn2() is called ....
 Can anybody tell me from where and who and Why does Btn1() is called...
 )

 I think during the drawing of the button either the buttons state is changed or drawn with wrong state...

 I don't know whether i am doing something wrong or there is some BUG here in handling setstate of the button.......



Dennis
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:_nn_
ID: 9626244
See :
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_cbutton.3a3a.setstate.asp

<quote>
A button control is automatically highlighted when the user clicks and holds the left mouse button. The highlighting is removed when the user releases the mouse button.
</quote>

If you need to change this behavior, I'm afraid it's gonna be somewhat more complicated than just using CButton::SetState()... What are you trying to do exactly here ?

0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9626292
void CDialogDlg::OnBtn1()
{
 static bool b_Btn1 = false;
 b_Btn1 = ! b_Btn1;

 m_Btn_Right.SetState(bBtn1) ;
}

void CDialogDlg::OnBtn2()
{
 static bool b_Btn2 = false;
 b_Btn2 = !b_Btn2 ;  // to trace the state of the button

 m_Btn_Right.SetState(bBtn2) ;
}

I am not sure that this is highlighting you want: highlighting affects the exterior of a button control.
0
 
LVL 5

Author Comment

by:dennis_george
ID: 9626326
Then what is the purpose of SetState ?? If "A button control is automatically highlighted when the user clicks and holds the left mouse button. The highlighting is removed when the user releases the mouse button".

This is the default action of a button.... From the MSDN i got only that  you can set the highlighting state of a button control using SetState....

I want two or more buttons with the following behaviours........
(A button can be in two state either UP or DOWN)
-- If the button is in UP state (Not highlighted) and I pressed it, then its state should change from UP to DOWN (Highlighted)... and it should remain in that state only until i press it again.....
-- If the button is in DOWN state (highlighted) and I pressed it, then its state should change from DOWN to UP ...
and it should remain in that state only until i press it again....

Dennis
0
 
LVL 13

Accepted Solution

by:
SteH earned 150 total points
ID: 9626396
Use a Checkbox and tick the box Push-like under styles on the property page. Now you have a button that holds its state until you click on it.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9626422
As SteH says use a checkbox with the push style (resource editor).  You can then access it like a normal button.
eg ((CButton*)GetDlgItem(IDC_CHECK1))->
or create a member CButton variable linked to the control m_btn_Left
0
 
LVL 5

Author Comment

by:dennis_george
ID: 9626514
Thanx SteH.......

    I think my problem is solved ......

But Stil i don't get the use of SetState of Push Button (CButton)......

Dennis
0
 
LVL 13

Expert Comment

by:SteH
ID: 9626540
For a push button you can use it to change its state by program code instead of a mouse click. For a normal button I can't really see a use for it. It is needed if you can use a shortcut key or a menu item to issue the same command. SetState allows to synchronize all values.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question