Jump to content

Set default text for an edit


Mat
 Share

Recommended Posts

One of the many things on my (long) to do list that i have not been able to do recently due to a rather pleasant surprise, it was very quick and easy though.

Everyone has seen those forms that already have text in the inputs such as "Search for..." that then disappears when the edit gains focus. Thats what this does, all you need to do is add one line after the edit/input, and thats it!

the code:

Global $EDIT_DEF_ITEMS[1][2] = [[0, 0]]

Func _GUIEditSetDefault ($hEdit, $sDef)
    If $hEdit = 0 Then Return SetError (1, 0, 0)

    If $EDIT_DEF_ITEMS[0][0] = 0 Then GUIRegisterMsg (0x0111, "EDIT_DEF_WM_COMMAND")

    If GUICtrlRead ($hEdit) = "" Then
        GUICtrlSetColor ($hEdit, 0x444444)
        GUICtrlSetData ($hEdit, $sDef)
    EndIf

    ReDim $EDIT_DEF_ITEMS[UBound ($EDIT_DEF_ITEMS) + 1][2]
    $EDIT_DEF_ITEMS[UBound ($EDIT_DEF_ITEMS) - 1][0] = $hEdit
    $EDIT_DEF_ITEMS[UBound ($EDIT_DEF_ITEMS) - 1][1] = $sDef

    Return 1
EndFunc ; ==> _GUIEditSetDefault

Func EDIT_DEF_WM_COMMAND ($hWnd, $msgID, $wParam, $lParam)
    Local $n = EDIT_DEF_GETINDEX (BitAND ($wParam, 0xFFFF))
    If $n = -1 Then Return "GUI_RUNDEFMSG"

    Local $nMsg = BitShift($wParam, 16)
    If $nMsg = 256 Then ; Gained focus (EN_SETFOCUS)
        If (GUICtrlRead ($EDIT_DEF_ITEMS[$n][0]) == $EDIT_DEF_ITEMS[$n][1]) Then
            GUICtrlSetColor ($EDIT_DEF_ITEMS[$n][0], 0x000000)
            GUICtrlSetData ($EDIT_DEF_ITEMS[$n][0], "")
        EndIf
    ElseIf $nMsg = 512 Then ; Lost Focus (EN_KILLFOCUS)
        If GUICtrlRead ($EDIT_DEF_ITEMS[$n][0]) = "" Then
            GUICtrlSetColor ($EDIT_DEF_ITEMS[$n][0], 0x444444)
            GUICtrlSetData ($EDIT_DEF_ITEMS[$n][0], $EDIT_DEF_ITEMS[$n][1])
        EndIf
    EndIf
EndFunc ; ==> EDIT_DEF_WM_COMMAND

Func EDIT_DEF_GETINDEX ($hEdit)
    For $i = 1 to UBound ($EDIT_DEF_ITEMS) - 1
        If $EDIT_DEF_ITEMS[$i][0] = $hEdit Then Return $i
    Next
    Return -1
EndFunc ; ==> EDIT_DEF_GETINDEX

And an example:

GUICreate ("Testing")

$hEdit = GUICtrlCreateInput ("", 2, 2, 100, 20)
_GUIEditSetDefault ($hEdit, "This is the test")

$hEdit2 = GUICtrlCreateInput ("", 2, 24, 80, 20)
_GUIEditSetDefault ($hEdit2, "Take 2.")

GUISetState ()

While GUIGetMsg () <> -3
    Sleep (10)
WEnd

Mat

Edited by Mat
Link to comment
Share on other sites

  • 2 months later...

Ha, thats great. Something I needed a long time ago. -Saves-

But still, what about a default "style"?

So you could do something like "Password" :mellow:

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...