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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

catch the ENTER key in PreTranslateMsg
and get the control with focus
CFormView can't made this work for you
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 ?

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);
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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.)
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.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.  
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.