Solved

How to MAX chars in EDIT Control

Posted on 2013-06-23
3
374 Views
Last Modified: 2013-06-24
How would I put a limit on the number of characters used in an Edit control?

Is there some sort of MAX CHARACTER attribute I can set? I can't find this in the docs.

Thanks!
0
Comment
Question by:edvinson
3 Comments
 
LVL 31

Assisted Solution

by:Zoppo
Zoppo earned 250 total points
ID: 39270403
Hi edvinson,

you can send the control a EM_LIMITTEXT message (or, if you use MFC, call CEdit::LimitText) to limit the amount of text which the user can enter to an edit control.

Hope that help,

ZOPPO
0
 
LVL 33

Accepted Solution

by:
sarabande earned 250 total points
ID: 39271215
to add to above comment:

the code for winapi (not mfc) is like

HWND hwnd = GetDlgItem(hwndDialog, IDC_EDIT1);
if (hwnd != NULL)
{
    SendMessage(hwnd, EM_LIMITTEXT, (WPARAM)max_chars_allowed, 0); 
}

Open in new window


assuming hwndDialog is the windows handl of your form, IDC_EDIT1 is the resource id of the control and max_chars_allowed is an integer variable containing the maximum length.

you would send the message in the handler for WM_INITDIALOG message after loading the resources.

Sara
0
 
LVL 1

Author Closing Comment

by:edvinson
ID: 39273533
Perfect solution, thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c++ mixing int and strings in multi array 8 71
Fully specialized class template function 21 129
How to convert MFC::CString to UTF8 wchar_t* 10 137
max float value 3 21
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

932 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now