• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 409
  • Last Modified:

CFormView and first control is button

I created a dialog box where the class is derived from CFormView, and the first control is a button.  When this dialog box comes up, the button had the focus.  The problem is when I hit "enter" nothing happens until I go to the next field, hit "shift+tab" to go back to the button, and then hit enter.  It appears that there is a bug in Microsoft where it does not set the button properly to the "default button" if it is the first control in the view.  Why is that and how should one program around that?
0
sgerlach
Asked:
sgerlach
1 Solution
 
cdesignerCommented:
catch the ENTER key in PreTranslateMsg
and get the control with focus
CFormView can't made this work for you
0
 
akalmaniCommented:
Hi sgerlach !!
   Could u be a little bit clear what u want to do , and what's happening or post some code which u have written ?
0
 
cdesignerCommented:

....
BOOL CDlg::PreTranslateMessage(MSG *pMsg)
{
    if (pMsg->message==WM_KEYDOWN && pMsg->wParam==VK_ENTER)
    {
      if (youButtonCtrl->GetState() & 0x0008) //button has focus
return TRUE;
    }
    return CDialog::PreTranslateMessage(pMsg);
}
....
0
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
sgerlachAuthor Commented:
cdesigner, Your solution assumes that I know the ID number of the button.  In my circumstance, I do not.  The reason is that I am writing my own CFormView class that many other people will be deriving from.  One other point is that this "bug" does not appear in "CDialog", only in "CFormView".

I actually implemented a solution that seems to work, but I am not sure that it is the best solution.  Basically, when a field gets focus, I check to see if it is a button. If so, I set that as the "default" button.  Then, when the button no longer as focus, I set it back to what it was originally.

akalmani, You asked for more of a clarification.  To recreate the error that I am experiencing, create a "Dialog" based application.  Then instead of "CDialog", derive the dialog from "CFormView".  Now create a button as the first field and attach a message to it that says something like "Hello World" when the button is clicked.  Now start up the application and the button should have focus.  Try to hit enter and you will see nothing happens.  (Note: I currently use Visual C++ 5.0.)
0
 
yazdchiCommented:
Hi sqerlach,

You are right, I tried it and it didn't work, but I found that if you set the button as the Default button and also set the focus to that button it works.

You can either do this from the Resource Editor or programitically by:


((CButton*)GetDlgItem(IDC_BUTTON1)) ->SetButtonStyle(BS_DEFPUSHBUTTON);
((CButton*)GetDlgItem(IDC_BUTTON1)) ->SetFocus();

NOTE: You must set both the style and the focus or it will not work. I've tried the above code in my own program just now and it worked.


Hope this helps.

Regards,
Mohsen
0
 
sgerlachAuthor Commented:
Thanks for the comments.  

Mohsen, I could not implement your suggestion, because many times I want another button to be the default button.  However, I did change the code so the button that has focus gets temporarily set to be the default button.  
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now