Solved

how to validate data in edit control ?

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
isEverywhere  challenge 19 77
Window placement 17 83
post4 challenge 28 100
zeroMAx challenge 20 104
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa‚Ķ
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.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

809 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