CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2016
    Posts
    2

    help with limiting a textboxs max chars

    this is my code, if anyone can help it would be greatly appreciated
    Code:
    ConsoleField = CreateWindowEx(NULL, "EDIT", "", WS_CHILD | WS_BORDER | WS_VSCROLL | ES_MULTILINE | WS_VISIBLE | ES_READONLY | ES_AUTOVSCROLL, 12, 34, 343, 234, MainWindow, (HMENU)CONSOLE_WINDOW, HInstance, 0);
    	HWND consoleFieldLabel = CreateWindowEx(NULL, "STATIC", "", WS_CHILD | WS_VISIBLE, 12, 9, 100, 20, MainWindow, NULL, HInstance, NULL);
    	InputField = CreateWindowEx(10, "EDIT", "", WS_CHILD | WS_BORDER | ES_MULTILINE | WS_VISIBLE, 362, 34, 144, 234, MainWindow, (HMENU)INPUT_FIELD, HInstance, 0);
    	HWND inputFieldLabel = CreateWindowEx(NULL, "STATIC", "", WS_CHILD | WS_VISIBLE, 359, 9, 60, 20, MainWindow, NULL, HInstance, NULL);
    	SendMessage(inputFieldLabel, WM_SETTEXT, NULL, (LPARAM)"Input");
    	SendMessage(consoleFieldLabel, WM_SETTEXT, NULL, (LPARAM)"Output");
    	SendMessage(InputField, EM_SETLIMITTEXT, INPUT_CHAR_LIMIT, NULL);

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: help with limiting a textboxs max chars

    From MSDN:
    Remarks

    The EM_SETLIMITTEXT message limits only the text the user can enter. It does not affect any text already in the edit control when the message is sent, nor does it affect the length of the text copied to the edit control by the WM_SETTEXT message. If an application uses the WM_SETTEXT message to place more text into an edit control than is specified in the EM_SETLIMITTEXT message, the user can edit the entire contents of the edit control.
    Best regards,
    Igor

  3. #3
    Join Date
    Dec 2016
    Posts
    2

    Re: help with limiting a textboxs max chars

    Quote Originally Posted by Igor Vartanov View Post
    From MSDN:
    So what would I need to change to get it to limit the characters?

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: help with limiting a textboxs max chars

    This issue only occurs when there is existing text in an edit box before the user enters text. If the need is to limit the total umber of chars in a text box (why?) then one way would be on the text box getting input focus to determine how many chars are already present in the text box and subtract that from the required max total to get the value for EM_SETLIMITTEXT.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: help with limiting a textboxs max chars

    You could handle the EN_UPDATE or/and EN_CHANGE notification to correct the text and its length.
    https://www.google.ch/webhp?sourceid...e+vs+en_change
    Victor Nijegorodov

  6. #6
    Join Date
    Dec 2016
    Posts
    16

    Re: help with limiting a textboxs max chars

    VictorN, excellent information. Thanks.

    Here's how in a partially implemented form dialog I made one phone field have max length of three, and automatically tab to the next textbox. The header file has more, but the max length is in here:

    Code:
    void CEmployeeDlg::DoDataExchange(CDataExchange* pDX)
    {
    	CDialogEx::DoDataExchange(pDX);
    	DDX_Text(pDX, IDC_EMAIL_EDIT, m_EmailAddress);
    	DDX_Text(pDX, IDC_PHONENUMBER_EDIT, m_Phone);
    	DDV_MaxChars(pDX, m_Phone, 3);
    	*** TRIM ***
    }
    
    
    BEGIN_MESSAGE_MAP(CEmployeeDlg, CDialogEx)
    	ON_EN_MAXTEXT(IDC_PHONENUMBER_EDIT, &CEmployeeDlg::OnEnMaxtextPhonenumberEdit) ****
    	*** trim ***
    END_MESSAGE_MAP()
    
    
    // CEmployeeDlg message handlers
    
    
    void CEmployeeDlg::OnEnMaxtextPhonenumberEdit()
    {
    	// TODO: SELECT SECOND PHONE NUMBERfIELD
    	//AfxMessageBox(_T("Eneterd 3 digits, will tabstop to next control!"));
    	NextDlgCtrl();
    }
    In holiday spirits (lol), anyone got a killer reference to CDataExchange information, and other robust code implementation techniques as VictorN pointed?

    (books, links.. or downloadable code, etc)

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: help with limiting a textboxs max chars

    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured