Form Focus

I am using C++ builder 6.0 and windows XP

I have a form with numerous edit boxes when the user inputs information to the edit box then presses X (i have used the keypress method of the edit box) I want to give focus to the form so that when the user presses X again something else will happen.

void __fastcall Tentermeters1form::MaskEdit1KeyPress(TObject *Sender,
      char &Key)
{
        if (Key == 'X')  // if user presses X the program will show the totals on the screen
        {
             //DO OTHER CLACULATIONS HERE  
             entermeters1form->SetFocus();

        }// end if Key == X
}

after all the calculations are done I want the form to receive focus but the above code doesnt work could anyone explain why?

LVL 3
lisa_mcAsked:
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.

Anthony2000Commented:
I noticed that you are testing 'X", are you pressing a lower case 'x' instead of an upper case one?
0
lisa_mcAuthor Commented:
No i am def pressing uppercase X because I have a mask over the edit box which only accepts numbers so when the user presses anything other than X and a number the program doesnt do anything
0
George TokasCommented:
Create a global boolean flag at your header file.
Set the flag after the first 'X' press....
Before testing for 'X' check the value of the flag and if set jump to the next function as follows:

if(XPressed){do the next thing and return}
if (Key == 'X')  // if user presses X the program will show the totals on the screen
        {
             //DO OTHER CLACULATIONS HERE  
             entermeters1form->SetFocus();
             XPressed = true;
        }// end if Key == X

Don't forget to reset the value of the bool after work is done...

George Tokas.
0

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
kode99Commented:
I'm assuming you are calling set focus on the same form that the edit control is on?  SetFocus changes the input focus to the form (in this case it does nothing as the form all ready has input) but it will not change the active control within the form.

To change the windowed control you would have to set the form's ActiveControl or use a specific control's SetFocus,  not the form's.

I am thinking that you want the X to recalc if pressed while focus is on a specific control.

If you set the form's KeyPreview to true then you can catch the keystrokes at the form level before the control events fire.  Then you can check the focused control or whatever conditions you need to determine what the X keystroke will actually do.  So now as long as your form has input focus you can use the keystrokes irrelevant of which specific control is active.

example,

void __fastcall TForm3::FormKeyPress(TObject *Sender, char &Key)
{
  if((Key == 'X')&&(ActiveControl == MaskEdit1))
  {
     <run your calc here>
     // change focus to different control on form
     ActiveControl = Button1;   OR Button1->SetFocus();
 
     Key = NULL;  // clear key
  }
  else
  {
   <do something different>
  }
}

Along the lines of George's flag suggestion you could use a OnChange event from your edits to 'arm' the recalculation if something has been changed.

One other thing,  may or may not be an issue.  If you let mask edit stop the X you will get a bad keystroke beep.  If you clear the key value this is avoided.  

0
lisa_mcAuthor Commented:
Thanks guys got it sorted have accepted both solutions
0
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
Editors IDEs

From novice to tech pro — start learning today.