Solved

how to validate data in edit control ?

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

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

Suggested Solutions

Title # Comments Views Activity
Creating Labels and Frequency list style reports in SAS code 9 105
fizzArray2 challenge 1 91
either24  challenge 19 101
scoreUp challenge 14 64
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

829 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