• Advertisement

Archived

This topic is now archived and is closed to further replies.

Limiting Characters in Edit Box :: MFC

This topic is 5713 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. I would like to limit the number of characters a user make enter at any given time to a finite integer. For example, let say I have an edit box in a dialog box for entering zip code. I would like to limit the number of characters to *exactly five*. I have tried using DDV verification feature in a dialog box. However, I found that the user can overflow the edit box. For example: limit: 500 User: 500000 If the user overflow the edit box, for DDV will consider the data entered as being valid. I believe the problem has something to do with overflowing a datatype. Nonetheless, what is the best way to limit the number of characters/digits entered in an edit box? Thanks, Kuphryn

Share this post


Link to post
Share on other sites
Advertisement
Okay.

The solution is to use a CString variable holding the value of the edit box. I am referring to the example of the five digit zip code. For example, lets consider that the variable is an int. If the variable is declared as an int, there will be an overflow if the user enters 99999999999999. The solution I came up with is to define it as a CString. Here is the verification part.

-----
void CMyDlg::OnEnChangeMyEditBox()
{
UpdateData(TRUE);


// the length must be exactly five
// the value must be between 10000 and 91000

if (m_EditPortListen.GetLength() == 5 &&
atoi(static_cast(m_MyEditBox)) >= 10000 &&
atoi(static_cast(m_MyEditBox)) <= 91000)
SetModified(TRUE);

else
{
m_MyEditBox.Format("%d", 10000);
UpdateData(FALSE);
SetModified(FALSE);
}
}
-----

The code above works well.

Kuphryn

Share this post


Link to post
Share on other sites
Are you talking about limiting the length or limiting the value? There are two different ways to validate data in a control, by max-length (if it''s a string) or by min/max value if it''s an integer.

In VS6 there''s access to that in the ClassWizard, in VS.NET in the ''Add variable'' dialog box, check the ''control variable'' option, then select the right control, then change category to ''value''. Depending on the type you enter into ''Variable Type'' it will allow you to change either the ''Max Chars'' field or the ''Min Value'' and ''Max Value'' fields.

Share this post


Link to post
Share on other sites
I tried using the mix_max DDV for a value. It does work, but continue prompting the user until the user enters the correct value. I am implementing a property page. I need a more user-friendly approach. The approach I posted works well.

Kuphryn

Share this post


Link to post
Share on other sites

  • Advertisement