Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

String Size!

Posted on 1998-12-17
3
Medium Priority
?
432 Views
Last Modified: 2012-08-13
Hi... if I have a dialog based MFC and I stick on an edit box that is a string,then I set it to have a max number of characters of 10. How do I do the same thing except with a minimum number of characters of 3. So if I type 1 character and I press apply or OK, then a message box pops up, gives a message, the edit box has the focus, and the dialog is not closed, but rather waits for me to fix the error.
0
Comment
Question by:rose3377
[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
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
Answers2000 earned 150 total points
ID: 1180392
Answer in my comment below
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1180393
1. Press Ctrl-W, go to member variables tab, double click on the edit control's name (e.g. IDC_EDIT),  add a member variable type, "Control", "CEdit", name "m_edit"

2. Add a handler for the OK button (IDOK BN_CLICKED in first tab of class wiz)

Will look something like

void CMyDialog::OnOK()
{
CDialog::OnOK() ;
}


edit this code between { and } to look like

{
int len = m_edit.GetWindowTextLength() ;
if ( len < 3 )
{
AfxMessageBox( "Too short" ) ;
m_edit.SetFocus() ;
} else
{
CDialog::OnOK() ;
}
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1180394
A nicer way to do it is to write your own DDV routine that checks for both min and max size.  Just get the code for the MFC DDX_MaxChars and rename and modify it accordingly.  DDV will do all the appropriate things about messages and seting focus etc.

Here is MFC code...

void AFXAPI DDV_MaxChars(CDataExchange* pDX, CString const& value, int nChars)
{
      ASSERT(nChars >= 1);        // allow them something
      if (pDX->m_bSaveAndValidate && value.GetLength() > nChars)
      {
            TCHAR szT[32];
            wsprintf(szT, _T("%d"), nChars);
            CString prompt;
            AfxFormatString1(prompt, AFX_IDP_PARSE_STRING_SIZE, szT);
            AfxMessageBox(prompt, MB_ICONEXCLAMATION, AFX_IDP_PARSE_STRING_SIZE);
            prompt.Empty(); // exception prep
            pDX->Fail();
      }
      else if (pDX->m_hWndLastControl != NULL && pDX->m_bEditLastControl)
      {
            // limit the control max-chars automatically
            ::SendMessage(pDX->m_hWndLastControl, EM_LIMITTEXT, nChars, 0);
      }
}

make a copy and change like this (haven't tested .. that is left as an exercise for the reader):

void AFXAPI DDV_MinMaxChars(CDataExchange* pDX, CString const& value, int nMinChars, int nMaxChars)
{
      ASSERT(nMaxChars >= nMinChars);        // allow them something
      if (pDX->m_bSaveAndValidate && (value.GetLength() > nMaxChars || value.GetLength() < nMinChars))
      {
            CString prompt;
            prompt.Format("Must be between %d and %d characters",nMinChars,nMaxChars);
            AfxMessageBox(prompt, MB_ICONEXCLAMATION, AFX_IDP_PARSE_STRING_SIZE);
            prompt.Empty(); // exception prep
            pDX->Fail();
      }
      else if (pDX->m_hWndLastControl != NULL && pDX->m_bEditLastControl)
      {
            // limit the control max-chars automatically
            ::SendMessage(pDX->m_hWndLastControl, EM_LIMITTEXT, nChars, 0);
      }
}

NOTE: You can reuse this and even make it available in the CLassWizrd (just like the standard max characters version).  There is a KB article about how to do this (or ask me and I'll try to look it up).


0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

715 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