Jump to content


Photo

[fn][L] Placeholder() - placeholder text for edit controls


  • Please log in to reply
13 replies to this topic

#1 infogulch

infogulch
  • Moderators
  • 717 posts

Posted 18 October 2011 - 01:22 AM

Note: For single-line edit controls SetEditCueBanner() would be better suited.

For edit controls with multiple lines and other isolated cases, this function will work better.

Function:
; Placeholder() - by infogulch for AutoHotkey v1.1.05+
; 
; to set up your edit control with a placeholder, call: 
;   Placeholder(hwnd_of_edit_control, "your placeholder text")
; 
; If called with only the hwnd, the function returns True if a 
;   placeholder is being shown, and False if not.
;   isPlc := Placeholder(hwnd_edit)
; 
; to remove the placeholder call with a blank text param
;   Placeholder(hwnd_edit, "")
; 
; http://www.autohotkey.com/forum/viewtopic.php?p=482903#482903
; 

Placeholder(wParam, lParam = "`r", msg = "") {
    static init := OnMessage(0x111, "Placeholder"), list := []
    
    if (msg != 0x111) {
        if (lParam == "`r")
            return list[wParam].shown
        list[wParam] := { placeholder: lParam, shown: false }
        if (lParam == "")
            return "", list.remove(wParam, "")
        lParam := wParam
        wParam := 0x200 << 16
    }
    if (wParam >> 16 == 0x200) && list.HasKey(lParam) && !list[lParam].shown ;EN_KILLFOCUS  :=  0x200 
    {
        GuiControlGet, text, , %lParam%
        if (text == "")
        {
            Gui, Font, Ca0a0a0
            GuiControl, Font, %lParam%
            GuiControl,     , %lParam%, % list[lParam].placeholder
            list[lParam].shown := true
        }
    }
    else if (wParam >> 16 == 0x100) && list.HasKey(lParam) && list[lParam].shown ;EN_SETFOCUS := 0x100 
    {
        Gui, Font, cBlack
        GuiControl, Font, %lParam%
        GuiControl,     , %lParam%
        list[lParam].shown := false
    }
}

Example:
gui, add, edit, w300 hwndhEdit
    gui, add, edit, wp r4 hwndhEdit2
    gui, add, button, vButton, Exit
    
    Placeholder(hEdit, "Enter some text")
    Placeholder(hEdit2, "Another text entry point")
    gui, show
    guicontrol, focus, button
return

ButtonExit:
ExitApp

Uses WM_COMMAND notifications for edit controls EN_KILLFOCUS and EN_SETFOCUS to determine when a control gets/loses focus, and based on whether the control's text is empty displays a placeholder in a gray color.

TODO:
Ability to specify the exact fonts you want for each state.

#2 Nazzal

Nazzal
  • Members
  • 250 posts

Posted 22 October 2011 - 06:56 PM

That is a very nice function, but I think both colors should be optional in the function.

#3 tidbit

tidbit
  • Moderators
  • 2283 posts

Posted 22 October 2011 - 07:02 PM

Yes, a very nice function. I'll be using this for sure! thank you.

#4 infogulch

infogulch
  • Moderators
  • 717 posts

Posted 23 October 2011 - 12:07 AM

thanks, tidbit!

Nazzal: I completely agree. (note my TODO at the end of the post)

I just spent several hours and it's almost working.

Unfortunately, it turns out that last "almost" makes it unreachable.

I need a method of determining the current font so I can save it and restore it before putting the user's font options into effect.

The user's font and font options specified to Placeholder() would logically be relative to the font at the time of the the call to Placeholder(), but in reality the options are relative to the most recent Gui, Font setting, which is very possibly not the same thing.

In fact this can be demonstrated with the example above. To break it, change the font size for just the button and start clicking around.

Edit: I made a post in the wishlist forum for something that would fix this problem.

#5 Nazzal

Nazzal
  • Members
  • 250 posts

Posted 23 October 2011 - 05:24 PM

Thanks infogulch,
I will be using your function in a lot of my scripts as I don't like edit controls to have descriptive text controls, appreciate your effort and will be watching this thread for future improvements.

#6 nimda

nimda
  • Members
  • 4301 posts

Posted 01 February 2012 - 12:12 PM

<!-- m -->http://www.autohotke... ... 022#510022<!-- m --> :arrow: EM_SETCUEBANNER :shock:

#7 aaffe

aaffe
  • Members
  • 1024 posts

Posted 01 February 2012 - 02:22 PM

Sorry, but whats the difference between EM_SETCUEBANNER and this function?

#8 infogulch

infogulch
  • Moderators
  • 717 posts

Posted 01 February 2012 - 05:57 PM

Hmm, apparently the difference between them is that EM_SETCUEBANNER is better than this function and has the advantage of being built into the edit control. *facepalm*

You know, I've always enjoyed reinventing the wheel. :lol:

Perhaps since it's built into windows we could get a new GuiControl option to change it?

#9 nimda

nimda
  • Members
  • 4301 posts

Posted 01 February 2012 - 11:53 PM

Hmmm... GuiControl, or maybe right in the Gui, Add... (though how could that be done without breaking compatibility? v2?)

It is much better than a Text control; if I knew C++ I'd add it myself :lol:

#10 aaffe

aaffe
  • Members
  • 1024 posts

Posted 02 February 2012 - 08:36 AM

Perhaps we could use:
Gui, Add, Text, vText, This is text., This is shown when no text is there.

#11 GuiUser

GuiUser
  • Guests

Posted 02 February 2012 - 01:42 PM

Sorry, but whats the difference between EM_SETCUEBANNER and this function?

The difference is, that EM_SETCUEBANNER does not work on multi-line edit controls, while this method by Infogulch allows this. 8)

#12 aaffe

aaffe
  • Members
  • 1024 posts

Posted 02 February 2012 - 01:59 PM

Ah, ok, thanks!

Sorry, but whats the difference between EM_SETCUEBANNER and this function?

The difference is, that EM_SETCUEBANNER does not work on multi-line edit controls, while this method by Infogulch allows this. 8)



#13 hoppfrosch

hoppfrosch
  • Members
  • 339 posts

Posted 03 February 2012 - 06:13 AM

Sorry, but whats the difference between EM_SETCUEBANNER and this function?

One more difference: EM_SETCUEBANNER restores the cue text when you delete the complete contents of the edit box - and placeholder leaves an empty edit box ... ( :? or :) - depends on what you need ..)

EDIT Incorrect - see Infogulchs reply below ...

#14 infogulch

infogulch
  • Moderators
  • 717 posts

Posted 04 February 2012 - 09:45 PM

No, the difference is that Placeholder() only checks when the edit control itself is focused or unfocused.

After you do: ^a{Del}, the focus is still in the edit control. It will not add placeholder text until you've moved to a different control.