Solved

Push Buttons

Posted on 2003-10-27
10
854 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
Decoding 32 bit binary streams 6 49
Adapt this command to show who installed 29 111
fizzArray2 challenge 1 91
zeroMAx challenge 20 114
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 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