Solved

how to validate data in edit control ?

Posted on 2001-09-02
6
462 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: 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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…

729 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