Solved

Push Buttons

Posted on 2003-10-27
10
845 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
Comment Utility
hi,

 I am using VC 6

Dennis
0
 
LVL 16

Expert Comment

by:_nn_
Comment Utility
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
Comment Utility
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
 
LVL 16

Expert Comment

by:_nn_
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 5

Author Comment

by:dennis_george
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ADO Memory leak with DELPHI 2007 37 153
Rewrite URL With Encoded Characters 6 64
Error on moodle after upgrade 3 105
firstChar challenge 13 83
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now