Solved

how to validate data in edit control ?

Posted on 2001-09-02
6
446 Views
Last Modified: 2013-11-20

 Hello there,
  I have a msflexgrid over which i am moving one edit box and a combobox according to the row number.these edit box and combobox are created at the run time. I want to validate the data in the edit control after whole data is entered,and if the data is invalid,after showing the error mesg, set the focus back to the same.I tried to write it into OnKillFocus() and OnUpdate()..but it doesn't work properly.Any other event where i can validate the data which will set the focus back to edit control. Pls reply soon.

Narendra
0
Comment
Question by:Narendra_bt
6 Comments
 
LVL 7

Accepted Solution

by:
peterchen092700 earned 50 total points
ID: 6450072
OnKillFocus is a good place to add validation, but not for changing the focus back. This must be delayed until the current focus move is complete, e.g. using PostMessage.

define your custom woindows message by
#define WM_VALIDATIONFALIED WM_APP+1

In OnKillFocus, add validation code. If it fails, call PostMessage(WM_VALIDATIONFAILED, 0, (LPARAM)(wndvf->GetSafeHWnd()))

add a Handler for WM_VALIDATIONFAILED
Implement as follows:
LRESULT CWnd::OnValidationFailed(WPARAM, LPARAM lp)
{
  HWND hwnd = (HWND) lp;
  _ASSERTE(::IsWindow(hwnd));
  CWnd * wnd = CWnd::FromHandle(hwnd);
  if (wnd)
    wnd->SetFocus();
  AfxMessageBox("Hey! Don't cheat!");// or whatever..
  return 0;
}


Good luck!
Peter
0
 

Author Comment

by:Narendra_bt
ID: 6452396

 Hello peter,
   I tried the option u have given, but it's not working.
Following is code i tried out.
 

LRESULT CEditBox::OnValidationFailed(WPARAM, LPARAM lp)
{
 HWND hwnd = (HWND) lp;

 _ASSERTE(::IsWindow(hwnd));

 CWnd *wnd = CWnd::FromHandle(hwnd);

 if (wnd->FromHandle(hwnd))
   wnd->SetFocus();
 AfxMessageBox("invalid data",MB_OK,0);
 return 0;
}

void CEditBox::OnKillFocus(CWnd* pNewWnd)
{
 CString csStr;
 GetWindowText(csStr);

 if( csStr.Find('\"') >= 0 ) // validity condition
 {
  PostMessage(WM_VALIDATIONFAILED, 0,
              (LPARAM)(this->GetSafeHwnd()));
 }

 CEdit::OnKillFocus(pNewWnd);
}


 I have a class CEditBox derived from CEdit. In OnKillFocus function though the given condition is true it's not going into the function OnValidationFailed.
if u have any other code which will work perfectly pls send..or pls explain how this code works..I am new in mfc programming..Hoping for ur cooperation.

Regards
Narendra
0
 
LVL 7

Expert Comment

by:peterchen092700
ID: 6452517
Do you have added
MESSAGE_HANDLER(WM_VALIDATIONFAILED, OnValidationFailed) to the message map of the edit?
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9531981
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: peterchen

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Roshan Davis
EE Cleanup Volunteer
0
 

Expert Comment

by:SureshBaby
ID: 22068436
Hi Narendra,
                  i have requirement of getting a combobox over the MSFlexgrid on a particular cell.
Got stuck up with many days..can you please help me on this
I have also raised a question on the same,You can reply to that also..
Expecting your reply soon
Very urgent :((

Thnks in advance
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Whole sheet autoscrub still needed 19 54
How to determine file rename from EventLog ? 2 58
matchUp  challenge 9 126
Path of Workbook 3 87
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli‚Ķ
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

730 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