Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Push Buttons

Posted on 2003-10-27
10
Medium Priority
?
887 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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 600 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 45

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

927 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