Sign in to follow this  
Followers 0
FuryCell

Text in input control is automatically selected.

6 posts in this topic

#1 ·  Posted (edited)

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 295, 71, 192, 124)
$Input1 = GUICtrlCreateInput("Input1", 13, 27, 273, 21)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

Why is the text in $Input1 selected when the script is executed? I have tested this on both my Windows 7 Host OS and in a virtual machine running Windows XP. Is this a bug or am I do something wrong?

Edited by P5ych0Gigabyte

HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.

Share this post


Link to post
Share on other sites



I'm quite sure that has always been the default behavior of an input control that has focus.

EDIT: I assume it is to make it easier for the user to change the text in the input.

:D doh. I need some sleep.


HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.

Share this post


Link to post
Share on other sites

P5ych0Gigabyte,

A quick way to place the cursor at the end of the Input text if you would prefer that: :D

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>


$Form1 = GUICreate("Form1", 295, 71, 192, 124)
$Input1 = GUICtrlCreateInput("Input1", 13, 27, 273, 21)
GUISetState(@SW_SHOW)

GUICtrlSendMsg($Input1, $EM_SETSEL, -1, -1)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

That solution will only work the initial time the edit gets focus. If you wanted to prevent the selection of the text every time the edit control gains focus as you tab around the GUI, you would need to subclass the control which is rather more work. If you are interested I could take a shot at it for you - just let me know. :huggles:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Thanubis,

Why not just use WM_COMMAND?

Why not indeed? :D

As often, there are lots of ways to solve a problem.

M23

Edit:

Here is the subclassing method that I spoke of above. Tab through the controls and see both Inputs highlight all text each time. Then press the "SubClass" button and see that the right-hand Input no longer highlights all the text. If you press the "Revert" button, the right-hand Input returns to the default behaviour:

#include <GUIConstantsEX.au3>
#include <EditConstants.au3>
#include <WinAPI.au3>

Global $pOriginal_EditProc, $hNew_EditProc = 0

$hGUI = GUICreate("Subclassed Edit", 200, 100)

GUICtrlCreateInput("Example text", 10, 10, 80, 20)

$hInput = GUICtrlCreateInput("Example text", 100, 10, 80, 20)

$hButton = GUICtrlCreateButton("SubClass", 60, 50, 80, 30)

GUISetState()

GUICtrlSendMsg($hInput, 0xB1, -1, -1)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ; Must delete subclassed controls before freeing DLLCallbacks
            GUIDelete($hGUI)
            ; Now free DLLCallbacks
            DllCallbackFree($hNew_EditProc)
            Exit
        Case $hButton
            If GUICtrlRead($hButton) = "SubClass" Then
                GUICtrlSetData($hButton, "Revert")
                _EditSubClass()
                GUICtrlSetState($hInput, $GUI_FOCUS)
            Else
                GUICtrlSetData($hButton, "SubClass")
                _EditRevert()
                GUICtrlSetState($hInput, $GUI_FOCUS)
            EndIf
    EndSwitch

WEnd

Func _EditSubClass()

    ; Create callback
    $hNew_EditProc = DllCallbackRegister("_EditProc", "int", "hwnd;uint;wparam;lparam")
    ; get pointer
    Local $pNew_EditProc = DllCallbackGetPtr($hNew_EditProc)
    ; Subclass the Input
    $pOriginal_EditProc = _WinAPI_SetWindowLong(GUICtrlGetHandle($hInput), -4, $pNew_EditProc)
    If @error Then Return SetError(1, 0, 0)
    If $pOriginal_EditProc = 0 Then Return SetError(1, 0, 0)

EndFunc   ;==>_EditSubClass

Func _EditRevert()

    _WinAPI_SetWindowLong(GUICtrlGetHandle($hInput), -4, $pOriginal_EditProc)
    If @error Then Return SetError(1, 0, 0)

EndFunc

Func _EditProc($hWnd, $iMsg, $wParam, $lParam)

    If $iMsg = $EM_SETSEL And $lParam <> -1 Then
        GUICtrlSendMsg($hInput, 0xB1, -1, -1) ; Use StringLen(GUICtrlRead($hInput)), -1) if you want the caret at the end of the input
        Return 0
    Else
        ; Other messages go to default WindowProc
        Return _WinAPI_CallWindowProc($pOriginal_EditProc, $hWnd, $iMsg, $wParam, $lParam)
    EndIf

EndFunc   ;==>_EditProc

A bit of overkill for this particular case I will be the first to admit, but I hope it is instructive!

M23

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

P5ych0Gigabyte,

A quick way to place the cursor at the end of the Input text if you would prefer that: :D

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>


$Form1 = GUICreate("Form1", 295, 71, 192, 124)
$Input1 = GUICtrlCreateInput("Input1", 13, 27, 273, 21)
GUISetState(@SW_SHOW)

GUICtrlSendMsg($Input1, $EM_SETSEL, -1, -1)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

That solution will only work the initial time the edit gets focus. If you wanted to prevent the selection of the text every time the edit control gains focus as you tab around the GUI, you would need to subclass the control which is rather more work. If you are interested I could take a shot at it for you - just let me know. :huggles:

M23

Thanks that is perfect for what I am working on.

HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.

Share this post


Link to post
Share on other sites

One final method - just to show that you do not need the fancy stuff at all! :D

#include <GUIConstantsEX.au3>
#include <EditConstants.au3>
#include <WinAPI.au3>

Global $pOriginal_EditProc, $hNew_EditProc = 0

$hGUI = GUICreate("Input Caret Position", 200, 100)

$hInput_1 = GUICtrlCreateInput("Caret to front", 10, 10, 80, 20)
$hHandle_1 = GUICtrlGetHandle(-1)

$hInput_2 = GUICtrlCreateInput("Caret to end", 100, 10, 80, 20)
$hHandle_2 = GUICtrlGetHandle(-1)

GUISetState()

$hCurrInput = 0

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    $hCurrFocus = _WinAPI_GetFocus()

    If $hCurrFocus <> $hCurrInput Then

        Switch $hCurrFocus
            Case $hHandle_1
                GUICtrlSendMsg($hInput_1, 0xB1, 0, 0)
                $hCurrInput = $hCurrFocus
            Case $hHandle_2
                GUICtrlSendMsg($hInput_2, 0xB1, StringLen(GUICtrlRead($hInput_2)), -1)
                $hCurrInput = $hCurrFocus
        EndSwitch

    EndIf

WEnd

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
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
Sign in to follow this  
Followers 0