?
Solved

Form Focus

Posted on 2008-02-08
5
Medium Priority
?
1,915 Views
Last Modified: 2013-11-17
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?

0
Comment
Question by:lisa_mc
5 Comments
 
LVL 8

Expert Comment

by:Anthony2000
ID: 20851198
I noticed that you are testing 'X", are you pressing a lower case 'x' instead of an upper case one?
0
 
LVL 3

Author Comment

by:lisa_mc
ID: 20851763
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
 
LVL 16

Accepted Solution

by:
George Tokas earned 300 total points
ID: 20852192
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
 
LVL 25

Assisted Solution

by:kode99
kode99 earned 300 total points
ID: 20854922
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
 
LVL 3

Author Comment

by:lisa_mc
ID: 20883132
Thanks guys got it sorted have accepted both solutions
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

594 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