Thread: Limiting Characters in Edit Box :: MFC

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    Limiting Characters in Edit Box :: MFC

    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

  2. #2
    Unregistered
    Guest
    In your message routine, you could try:
    Code:
    case EDIT_ID_BLAH:
    if (strlen(zipcode) == 5) // zipcode is variable which holds zip code
    {
    break;
    }
    break;
    Or something like that. It might give you some ideas. Or maybe this:
    Code:
    case EDIT_ID_BLAH:
    if (strlen(zipcode) == 5) // zipcode is variable which holds zip code
    {
    GetDlgItemText(.....); // get contents of edit box
    SetDlgItemText(....); // disallow anymore than 5 chars/nums whatever
    break;
    }
    break;
    Not sure about that either but yo!!

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Okay. Thanks.

    Kuphryn

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    There is a EM_SETLIMITTEXT message that you can send to an edit....try that.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    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<LPCTSTR>(m_MyEditBox)) >= 10000 &&
    atoi(static_cast<LPCTSTR>(m_MyEditBox)) <= 91000)
    SetModified(TRUE);

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

    The code above works well.

    Kuphryn

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    63
    try giving this limitation:
    min: 10000
    max: 99999
    tudehopet uses Borland Compiler 5.5
    and Visual C++ 6.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_HSCROLL in ES_READONLY edit box error
    By Homunculus in forum Windows Programming
    Replies: 4
    Last Post: 02-13-2006, 08:46 AM
  2. Messages from edit control in MFC
    By VirtualAce in forum Windows Programming
    Replies: 0
    Last Post: 01-08-2006, 01:11 AM
  3. Multiline Edit Box Parser
    By The Brain in forum Windows Programming
    Replies: 6
    Last Post: 11-01-2005, 07:15 PM
  4. setting fixed floats in edit box
    By WaterNut in forum Windows Programming
    Replies: 4
    Last Post: 08-13-2004, 09:13 AM
  5. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM