Solved

Push Buttons

Posted on 2003-10-27
10
850 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
notReplace  challenge 53 128
countEvens challenge 2 100
twoTwo  challenge 35 103
matchUp  challenge 9 108
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

773 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