Jump to content

Edit control selection changing and I don't want it to.


therks
 Share

Recommended Posts

This is some example code, but it gets my point across.

I want to use the button at the top of the window as an accelerator/hotkey [Alt+B]. The problem is that when I press Alt+B while I'm focussed on the edit control the selection changes. I am looking for a way to keep the selection from changing when I use the hotkey to access the button. In the example the button just randomizes the GUI background color, but in my actual application it's not so silly.

Any suggestions? (I'm looking at you Gary :P)

#include <GUIConstants.au3>

$gui = GUICreate('', 200, 200)
$bt = GUICtrlCreateButton('&Button', 0, 0, 100, 20)
    ;~ GUICtrlSetState(-1, $GUI_HIDE) ; When you hide this control, the accelerator still works
$ed = GUICtrlCreateEdit(StringFormat('The above button will be hidden so I can use it as an accelerator/hotkey.\r\n' & _
    'The problem is that when I press Alt+B, the selection in this edit control changes.\r\n' & _
    'I am trying to prevent that from happening.'), 0, 20, 200, 180, BitOR($WS_VSCROLL, $ES_AUTOVSCROLL, $ES_MULTILINE, $ES_WANTRETURN))
GUISetState()

GUIRegisterMsg($WM_COMMAND, 'WM_COMMAND')

While 1
    $gm = GUIGetMsg()
    Switch $gm
        Case $bt
            GUISetBkColor(Random(0, 0xffffff, 1))
            ControlFocus($gui, '', $ed)
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

Func WM_COMMAND($hWnd, $iMsg, $iWParam, $iLParam)
    $iIDFrom = BitAND($iWParam, 0xffff)
    $iCode = BitShift($iWParam, 16)
    ;~ This code prevents the edit selection from changing when the window loses then regains focus
    Switch $iIDFrom
        Case $ed
            Switch $iCode
                Case $EN_SETFOCUS, $EN_KILLFOCUS
                    Return 0
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc
Link to comment
Share on other sites

Why not just use a hot key?

#include <GUIConstants.au3>

HotKeySet("!b", "_SetBackColor")

$gui = GUICreate('', 200, 200)
$ed = GUICtrlCreateEdit(StringFormat('The above button will be hidden so I can use it as an accelerator/hotkey.\r\n' & _
    'The problem is that when I press Alt+B, the selection in this edit control changes.\r\n' & _
    'I am trying to prevent that from happening.'), 0, 20, 200, 180, BitOR($WS_VSCROLL, $ES_AUTOVSCROLL, $ES_MULTILINE, $ES_WANTRETURN))
GUISetState()

While 1
    $gm = GUIGetMsg()
    Switch $gm
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
 WEnd
 
Func _SetBackColor()
    GUISetBkColor(Random(0, 0xffffff, 1))
EndFunc

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

Anybody else have any ideas?

Only bumping once.

Maybe something like this.

#include <GUIConstants.au3>
#include <guiedit.au3>
#include <misc.au3>
Global $EdSel,$alted = False
$gui = GUICreate('', 200, 200)
$bt = GUICtrlCreateButton('&Button', 0, 0, 100, 20)
;~ GUICtrlSetState(-1, $GUI_HIDE); When you hide this control, the accelerator still works
$ed = GUICtrlCreateEdit(StringFormat('The above button will be hidden so I can use it as an accelerator/hotkey.\r\n' & _
        'The problem is that when I press Alt+B, the selection in this edit control changes.\r\n' & _
        'I am trying to prevent that from happening.'), 0, 20, 200, 180, BitOR($WS_VSCROLL, $ES_AUTOVSCROLL, $ES_MULTILINE, $ES_WANTRETURN))
GUISetState()


While 1
    ALtThing()
    
    $gm = GUIGetMsg()
    Switch $gm
        Case $bt
            GUISetBkColor(Random(0, 0xffffff, 1))
            ControlFocus($gui, '', $ed)
            If IsArray($EdSel) Then _GUICtrlEdit_SetSel($ed, $EdSel[0], $EdSel[1])
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

Func AltThing()
    If _IsPressed("12") Then
        If Not $alted Then
            $Alted = true
            $EdSel = _GUICtrlEdit_GetSel($ed)
        EndIf
    Else
        If $alted And IsArray($EdSel) Then _GUICtrlEdit_SetSel($ed, $EdSel[0], $EdSel[1])
        $alted = False
        
    EndIf
    
EndFunc
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I had a pretty good idea, but either GetLineCount is broken or .. well I don't know. Now it works but having the amount of times to send "Down" hardcoded sucks hard, ofcourse.

#include <GUIConstants.au3>

$gui = GUICreate('', 200, 200)
$bt = GUICtrlCreateButton('&Button', 0, 0, 100, 20)
    ;~ GUICtrlSetState(-1, $GUI_HIDE) ; When you hide this control, the accelerator still works
$ed = GUICtrlCreateEdit(StringFormat('The above button will be hidden so I can use it as an accelerator/hotkey.\r\n' & _
    'The problem is that when I press Alt+B, the selection in this edit control changes.\r\n' & _
    'I am trying to prevent that from happening.'), 0, 20, 200, 180, BitOR($WS_VSCROLL, $ES_AUTOVSCROLL, $ES_MULTILINE, $ES_WANTRETURN))
GUISetState()

GUIRegisterMsg($WM_COMMAND, 'WM_COMMAND')

While 1
    $gm = GUIGetMsg()
    Switch $gm
    Case $bt
        GUISetBkColor(Random(0, 0xffffff, 1))       
        ControlFocus($gui, '', $ed)
        ControlSetText($gui,"",$ed, GUICtrlRead($ed))
        $n_lines = ControlCommand($gui,"", $ed, "GetLineCount", "")     
        #cs
        For $i = 0 To $n_lines-1
            ControlSend($gui, "", $ed, "{DOWN}") ; ==> Usual format as Send("{DOWN 6}") to send Down 6 times apparently doesn't work with variables.
        Next
        #ce
        Send("{DOWN 10}")
        Sleep(10)
        Send("{END}")
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

Func WM_COMMAND($hWnd, $iMsg, $iWParam, $iLParam)
    $iIDFrom = BitAND($iWParam, 0xffff)
    $iCode = BitShift($iWParam, 16)
    ;~ This code prevents the edit selection from changing when the window loses then regains focus
    Switch $iIDFrom
        Case $ed
            Switch $iCode
                Case $EN_SETFOCUS, $EN_KILLFOCUS
                    Return 0
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc

The caret is placed at the start of the Edit after the ControlSetText() function. Therefore, I wanted to ask the Edit how many lines ($n) it holds using GetLineCount, then sending $n-1 (cause for 1 line, no down needs to sent, for 2 lines, 1 down has to be sent etc) times a "{DOWN}", then send "{END}" once to place the caret at the end again. Anyways, getlinecount always returns 1...

Edited by D4ni
Link to comment
Share on other sites

I had a pretty good idea, but either GetLineCount is broken or .. well I don't know. Now it works but having the amount of times to send "Down" hardcoded sucks hard, ofcourse.

#include <GUIConstants.au3>

$gui = GUICreate('', 200, 200)
$bt = GUICtrlCreateButton('&Button', 0, 0, 100, 20)
    ;~ GUICtrlSetState(-1, $GUI_HIDE) ; When you hide this control, the accelerator still works
$ed = GUICtrlCreateEdit(StringFormat('The above button will be hidden so I can use it as an accelerator/hotkey.\r\n' & _
    'The problem is that when I press Alt+B, the selection in this edit control changes.\r\n' & _
    'I am trying to prevent that from happening.'), 0, 20, 200, 180, BitOR($WS_VSCROLL, $ES_AUTOVSCROLL, $ES_MULTILINE, $ES_WANTRETURN))
GUISetState()

GUIRegisterMsg($WM_COMMAND, 'WM_COMMAND')

While 1
    $gm = GUIGetMsg()
    Switch $gm
    Case $bt
        GUISetBkColor(Random(0, 0xffffff, 1))       
        ControlFocus($gui, '', $ed)
        ControlSetText($gui,"",$ed, GUICtrlRead($ed))
        $n_lines = ControlCommand($gui,"", $ed, "GetLineCount", "")     
        #cs
        For $i = 0 To $n_lines-1
            ControlSend($gui, "", $ed, "{DOWN}") ; ==> Usual format as Send("{DOWN 6}") to send Down 6 times apparently doesn't work with variables.
        Next
        #ce
        Send("{DOWN 10}")
        Sleep(10)
        Send("{END}")
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

Func WM_COMMAND($hWnd, $iMsg, $iWParam, $iLParam)
    $iIDFrom = BitAND($iWParam, 0xffff)
    $iCode = BitShift($iWParam, 16)
    ;~ This code prevents the edit selection from changing when the window loses then regains focus
    Switch $iIDFrom
        Case $ed
            Switch $iCode
                Case $EN_SETFOCUS, $EN_KILLFOCUS
                    Return 0
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc

The caret is placed at the start of the Edit after the ControlSetText() function. Therefore, I wanted to ask the Edit how many lines ($n) it holds using GetLineCount, then sending $n-1 (cause for 1 line, no down needs to sent, for 2 lines, 1 down has to be sent etc) times a "{DOWN}", then send "{END}" once to place the caret at the end again. Anyways, getlinecount always returns 1...

It's not obvious to me this is related to the OP but anyway, why not just send "^{END}"?

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

It's not obvious to me this is related to the OP but anyway, why not just send "^{END}"?

OP?

In answer to your question; I simply didn't know Control + END served this purpose :P Then it's fixed, though still pretty weird. Anyway, in my opinion changing focus to an edit should not select all the text inside that edit. It really should just focus the control, as in making the caret active at the last known position.

Link to comment
Share on other sites

It really should just focus the control, as in making the caret active at the last known position.

That's what I was looking for. :P

Also OP means "Original Poster" so in this case, me! ;)

@martin: Thanks, I'm not sure if that will be a catch-all for what I need, but it definitely gives me something to work from. Good job.

Edited by Saunders
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...