Jump to content

Cursor position in Edit Control


Recommended Posts

I need to get cursor position from an edit control to put on my status bar. I get line index with _GUICtrlEdit_LineFromChar($EDIT,-1) but nothing work for column index. I tried also some tricks with _GUICtrlEdit_SetSel(),_GUICtrlEdit_GetSel() without any result. Have anyone an idea?

When the words fail... music speaks.

Link to comment
Share on other sites

I need to get cursor position from an edit control to put on my status bar. I get line index with _GUICtrlEdit_LineFromChar($EDIT,-1) but nothing work for column index. I tried also some tricks with _GUICtrlEdit_SetSel(),_GUICtrlEdit_GetSel() without any result. Have anyone an idea?

Try this.

;
$iLineNum = ControlCommand("Edit Line From Char", "", $hEdit, "GetCurrentLine", "")
$iCharPos = ControlCommand("Edit Line From Char", "", $hEdit, "GetCurrentCol", "")
_GUICtrlStatusBar_SetText($StatusBar, "Line: " & $iLineNum & ",  Col: " & $iCharPos)
;
Link to comment
Share on other sites

  • Moderators

Andreik,

You did not try hard enough - Malkey's code works perfectly! :)

#include <GUIConstantsEx.au3>
#Include <GuiEdit.au3>
#Include <WinAPI.au3>

$hGUI = GUICreate("Test", 500, 500)

$hEdit = GUICtrlCreateEdit("", 10, 10, 480, 380)

For $i = 1 To 100
    _GUICtrlEdit_AppendText($hEdit, "Line " & $i & @CRLF)
Next

GUICtrlCreateLabel("Line", 40, 420, 80, 20)
$hLine_Num = GUICtrlCreateLabel("", 10, 420, 20, 20)
GUICtrlCreateLabel("Column", 40, 450, 80, 20)
$hColumn_Num = GUICtrlCreateLabel("", 10, 450, 20, 20)

GUISetState()

$iCurr_Line = 0
$iCurr_Col = 0

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    If _GUICtrlEdit_LineFromChar($hEdit) <> $iCurr_Line Then
        GUICtrlSetData($hLine_Num, _GUICtrlEdit_LineFromChar($hEdit) + 1)
        $iCurr_Line = _GUICtrlEdit_LineFromChar($hEdit)
    EndIf

    $iIndex = ControlCommand($hGUI, "", $hEdit, "getCurrentCol", "" )
    If $iIndex <> $iCurr_Col Then
        GUICtrlSetData($hColumn_Num, $iIndex)
        $iCurr_Col = $iIndex
    EndIf

WEnd

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

  • Moderators

Andreik,

I have also found another scroll method (credit to Rasim) I have modifed his code a bit to match the other examples:

#include <GUIConstantsEx.au3>
#Include <GuiEdit.au3>
#include <ScrollBarConstants.au3>

$hGUI = GUICreate("Test", 500, 500)

$hEdit = GUICtrlCreateEdit("", 10, 10, 480, 380)

For $i = 1 To 100
    _GUICtrlEdit_AppendText($hEdit, "Line " & $i & @CRLF)
Next

GUICtrlCreateLabel("Line", 40, 420, 80, 20)
$hLine_Num = GUICtrlCreateLabel("", 10, 420, 20, 20)
GUICtrlCreateLabel("Column", 40, 450, 80, 20)
$hColumn_Num = GUICtrlCreateLabel("", 10, 450, 20, 20)

$hInput = GUICtrlCreateInput("", 250, 410, 40, 20)

$hButton = GUICtrlCreateButton("GoTo", 320, 410, 80, 30)

GUISetState()

$iCurr_Line = 0

$iCurr_Col = 0

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hButton
            $iNew_Line = Number(GUICtrlRead($hInput) - 1)
            GUICtrlSetData($hInput, "")
            _EditScroll($iNew_Line)
        _GUICtrlEdit_Scroll($hEdit, $SB_SCROLLCARET)
    EndSwitch

    If _GUICtrlEdit_LineFromChar($hEdit) <> $iCurr_Line Then
        GUICtrlSetData($hLine_Num, _GUICtrlEdit_LineFromChar($hEdit) + 1)
        $iCurr_Line = _GUICtrlEdit_LineFromChar($hEdit)
    EndIf

    $iIndex = ControlCommand($hGUI, "", $hEdit, "getCurrentCol", "" )
    If $iIndex <> $iCurr_Col Then
        GUICtrlSetData($hColumn_Num, $iIndex)
        $iCurr_Col = $iIndex
    EndIf

WEnd

Func _EditScroll($iLine)
    Local $iIndex = GUICtrlSendMsg($hEdit, $EM_LINEINDEX, $iLine, 0)
    If $iIndex = -1 Then Return 0
    GUICtrlSetState($hEdit, $GUI_FOCUS)
    GUICtrlSendMsg($hEdit, $EM_LINESCROLL, 0, $iLine)
    GUICtrlSendMsg($hEdit, $EM_SETSEL, $iIndex, $iIndex)
EndFunc   ;==>_TextScroll

M23

Edit: better example script.

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

The exemple code work fine but just try to replace $cEdit = GUICtrlCreateEdit("", 10, 10, 280, 150) with $cEdit = _GUICtrlEdit_Create($hGUI,"", 10, 10, 280, 150) and in this case doesn't work.

PS: I am tired, maybe I do something wrong.

When the words fail... music speaks.

Link to comment
Share on other sites

The exemple code work fine but just try to replace $cEdit = GUICtrlCreateEdit("", 10, 10, 280, 150) with $cEdit = _GUICtrlEdit_Create($hGUI,"", 10, 10, 280, 150) and in this case doesn't work.

PS: I am tired, maybe I do something wrong.

Here is an example of getting the line and column position of the caret within a _GUICtrlEdit_Create() type edit control.

;
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <GuiEdit.au3>
#include <WinAPI.au3> ; used for Lo/Hi word
#include <WindowsConstants.au3>
#include <GuiConstantsEx.au3>

Opt('MustDeclareVars', 1)

Global $hEdit, $StatusBar

_Example1()

Func _Example1()
    Local $hGUI, $aPartRightSide[2] = [378, -1]

    $hGUI = GUICreate("Edit Create", 400, 300)
    $hEdit = _GUICtrlEdit_Create($hGUI, FileRead("C:\Program Files\AutoIt3\SciTE\install.log"), 2, 2, 394, 268)

    $StatusBar = _GUICtrlStatusBar_Create($hGUI, $aPartRightSide)

    GUISetState()

    Do
        StatusInfo()
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

    GUIDelete()
EndFunc   ;==>_Example1

;Line-Col Info
Func StatusInfo()
    Local $iLineNum = _GUICtrlEdit_LineFromChar($hEdit, -1)
    Local $aCharPosNdx = _GUICtrlEdit_GetSel($hEdit)
    Local $iFrstCharOfLineNdx = _GUICtrlEdit_LineIndex($hEdit, -1)
    _GUICtrlStatusBar_SetText($StatusBar, "Ln: " & $iLineNum & ",  Col: " & $aCharPosNdx[0] - $iFrstCharOfLineNdx)
    Return
EndFunc   ;==>StatusInfo
;
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...