Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to check and edit box after cursor leaves it

Status
Not open for further replies.

nbgoku

Programmer
May 25, 2004
108
US
is there a way to check whether an edit box has some text written in it before the cursor leaves it to go to the next edit box?

there are 2 edit boxes, and i want the program to check and make sure something is written in the 1st edit box before the cursor reaches the 2nd edit box
 

The easy way...

1. Go to "Resource Editor"
2. Open up your dialog window where the edit controls are
3. Select the edit control
4. Bring up the Class Wizard (or press Ctrl+W)
5. On the message map tab, you should see the various messages that your edit control should receive.
6. Choose the message that you want to process and click "Add function". Then, edit the codes there.

Note: You must already set up a CEdit variable which represents the particular edit control you want to process. Otherwise, you need to setup CEdit variable that points to the particular control first.

Alternatively, the hard way ...

1. Look up your MSDN library about CEdit class.
2. Good luck.
 
when the user tries to set focus on the the second edit box then do a check on the data in the first.

i.e.

you need a variable (CString editBox1) associated with the first edit box.

add an event handler to the second edit box (something like OnSetFocus() or OnGetFocus() Note: I havn't checked what the actual event handler function name is) to capture the user trying to access the second edit box, this might be via the mouse or the tab key.

In your OnGetFocus() code do an UpdateData( true ), and then check the value of you editBox1 variable. If it is empty or not the correct number of characters or whatever the criteria is then automatically switch the focus back to the first edit box.

Hope you can follow by description? and good luck.
 
ohhhhhhh i think i see what ur saying, it makes sense, but what function would be used, i tried looking information on OnSetFocus() or OnGetFocus() but got no results

i guess the only problem for me now is finding the write function, anyone know what that may be?
 
ok wait sorry, i take that back, ive found the function

CWnd::OnSetFocus();


but my question now is, how do i write it out so lets say, when it captures the event, it does MessageBox("hi");
 
or how would i do it using EN_SETFOCUS
 
Add the event handler message "EN_SETFOCUS", this will put

Code:
BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
    ON_EN_SETFOCUS(IDC_EDIT, OnEnSetfocusEdit)
END_MESSAGE_MAP()
into your message map. You should have the function below which you can put your "Hi" Message in.


Code:
void CMyDlg::OnEnSetfocusEdit()
{
  MessageBox ( "HI" );
}
 
holly cow, i must say im a rookie in this, and u just made my day :) thanks shetlandbob!!
 
oh, something interesting happens, instead of just 1 message box appearing, there are like 5 or 10 that pop up after one another, and then stop, and then i can resume typign something into editbox2, isnt it just suppoused to show the message box once?
 
hmm, not sure whats going on there??? Had a look myself but dont really know? Assume its something to do with the fact a message box is being generated as the edit box is getting focus, so the window focus is actually moving round?? (this is just a hunch?)

Not sure how to get round this?

 
ah cool, ive found the ON_EN_KILLFOCUS to useful in this problem, hehe *smirk*

thanks for help !

:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top