Thread: EditBox control style change

  1. #1
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768

    EditBox control style change

    I've been trying to change a style of an editbox control, to add and remove ES_PASSWORD to be exact.

    After searching this board and the msdn I've come up with the next line of code, but it doesn't do anything:

    Code:
    SendDlgItemMessage(hDlg, IDD_CUSTOMLOGIN_PASSWORD, EM_SETPASSWORDCHAR, (WPARAM) (UINT) '*', 0);
    Thank you.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    SetWindowLong (or SetWindowLongPtr) with GWL_STYLE as the nIndex parameter ought to do it. The EM_SETPASSWORDCHAR message serves only to change the displayed replacement character.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    I was afraid of that, because I tried that as well:

    Code:
    HWND hPass;
    
    hPass = GetDlgItem(hDlg, IDD_CUSTOMLOGIN_PASSWORD);
    SetWindowLong(hPass, GWL_STYLE, (LONG)GetWindowLong(hPass, GWL_STYLE) | ES_PASSWORD);
    Same result, nothing changed.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Do both, ie.
    Code:
    SetWindowLong(hPass, GWL_STYLE, (LONG)GetWindowLong(hPass, GWL_STYLE) | ES_PASSWORD);
    SendMessage(hPass, EM_SETPASSWORDCHAR, (WPARAM)_T('*'), 0);
    InvalidateRect(hPass,0,0);
    Last edited by Ken Fitlike; 11-12-2004 at 06:24 PM. Reason: Added 'InvalidateRect' line.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You cant change the ES_PASSWORD style with SetWindowLong().

    Use this to toggle the character style:

    Code:
    void ToggleStyle(HWND hEdit)
    {
        if(SendMessage(hEdit,EM_GETPASSWORDCHAR,0,0) == '*')
            SendMessage(hEdit,EM_SETPASSWORDCHAR,0,0);
        else
            SendMessage(hEdit,EM_SETPASSWORDCHAR,(WPARAM)'*',0);
        SetFocus(hEdit);
    }

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Quote Originally Posted by bithub
    You cant change the ES_PASSWORD style with SetWindowLong().
    I beg to differ. The return value of SetWindowLong is non-zero indicating success. However, the change is not manifest until a subsequent EM_SETPASSWORDCHAR message is sent setting/resetting the password character. The control may also need to be invalidated to update the appearance.

    Your method works, too.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    According to MSDN, the only styles that can be changed by SetWindowLong() are:
    ES_LOWERCASE
    ES_NUMBER
    ES_OEMCONVERT
    ES_UPPERCASE
    ES_WANTRETURN

    Read here for details on how to change the other styles.

  8. #8
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Nevertheless, and equally according to msdn, SetWindowLong returns non-zero if successful (the previous value, in fact). The style bit (with respect to ES_PASSWORD, that is) is changed but that change may not be immediately manifested.

    However, I do accept that an EM_SETPASSWORDCHAR message is required to make that change apparent.
    Last edited by Ken Fitlike; 11-12-2004 at 06:53 PM.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    SetWindowLong returning non-zero just means that the actual style bits were set. This has absolutely nothing to do whether or not the window style is actually changed.

    From MSDN:
    After the control has been created, these styles cannot be modified, except as noted.
    Here are the notes for ES_PASSWORD:
    To change the characters that is displayed, or set or clear this style, use the EM_SETPASSWORDCHAR message.
    You can believe what you want, but I choose to believe the MSDN documentation, unless you can show me a test program which demonstrates otherwise.

  10. #10
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Quote Originally Posted by bithub
    You cant change the ES_PASSWORD style with SetWindowLong().
    Quote Originally Posted by Ken Fitlike
    The style bit (with respect to ES_PASSWORD, that is) is changed...
    Quote Originally Posted by bithub
    SetWindowLong returning non-zero just means that the actual style bits were set.
    This was my point - and there is no controversy beyond it. Please recognise that,
    Quote Originally Posted by Ken Fitlike
    I do accept that an EM_SETPASSWORDCHAR message is required to make that change apparent.
    I trust that clarifies things?
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    That's fine. I was under the impression that you were saying that SetWindowLong() made the change, and EM_SETPASSWORDCHAR made the change apparent. This of course isnt true, but I didn't realize you already knew that.

    If you read your posts again, you can probably see where my confusion came from. That's the problem with message board posts sometimes - there can be ambiguity. Anyways, glad to hear we were actually both on the same page

  12. #12
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Quote Originally Posted by bithub
    You cant change the ES_PASSWORD style with SetWindowLong().

    Use this to toggle the character style:

    Code:
    void ToggleStyle(HWND hEdit)
    {
        if(SendMessage(hEdit,EM_GETPASSWORDCHAR,0,0) == '*')
            SendMessage(hEdit,EM_SETPASSWORDCHAR,0,0);
        else
            SendMessage(hEdit,EM_SETPASSWORDCHAR,(WPARAM)'*',0);
        SetFocus(hEdit);
    }
    well that exactly what i did... except you added SetFocus(), why?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  13. #13
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Because the change isnt made apparent until the control regains focus. For instance, if you write "test" into the edit box, and then toggled the style, it wouldn't change until you either clicked in the edit box, or you called SetFocus().

  14. #14
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    got it, thanks
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Change cursor on single edit control
    By Niara in forum Windows Programming
    Replies: 3
    Last Post: 01-11-2009, 09:52 AM
  2. Changing the Style of a child control
    By filler_bunny in forum Windows Programming
    Replies: 2
    Last Post: 04-28-2004, 05:46 PM
  3. \n\r in an editbox control???
    By Devil Panther in forum Windows Programming
    Replies: 1
    Last Post: 07-18-2003, 04:18 AM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. Replies: 2
    Last Post: 09-04-2001, 02:12 PM