Solved

how to validate data in edit control ?

Posted on 2001-09-02
6
401 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now