Jump to content

Insert Text to Line Number in Edit Control - ?


atomman
 Share

Recommended Posts

Need to insert text at a specific line number (already existing) in an edit control, something like _GUICtrlEdit_InsertText, except that does character positioning.

I know how to do it with caret position, but that's not reliable in my case.

Link to comment
Share on other sites

Need to insert text at a specific line number (already existing) in an edit control, something like _GUICtrlEdit_InsertText, except that does character positioning.

I know how to do it with caret position, but that's not reliable in my case.

Might look at _GUICtrlEdit_LineIndex in conjunction with _GUICtrlEdit_InsertText

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

#include <GuiEdit.au3>

Opt('MustDeclareVars', 1)

$Debug_Ed = False ; Check ClassName being passed to Edit functions, set to True and use a handle to another control to see it work

_Example_Internal()

Func _Example_Internal()
    Local $hGUI, $StatusBar, $hEdit
    Local $sFile = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\AutoIt v3\AutoIt", "InstallDir") & "\include\changelog.txt"
    Local $aPartRightSide[2] = [378, -1], $iRandom

    ; Create GUI
    $hGUI = GUICreate("(Internal) Edit Line Index", 400, 300)
    $hEdit = GUICtrlCreateEdit("", 2, 2, 394, 268, BitOR($ES_WANTRETURN, $WS_VSCROLL))
    GUISetState()

    ; Set Margins
    _GUICtrlEdit_SetMargins ($hEdit, BitOR($EC_LEFTMARGIN, $EC_RIGHTMARGIN), 10, 10)

    ; Set Text
    _GUICtrlEdit_SetText ($hEdit, FileRead($sFile))

    ; Line Index
    $iRandom = Random(0, _GUICtrlEdit_GetLineCount ($hEdit) - 1, 1)
    ConsoleWrite("Inserting Text at Line: " & $iRandom & @LF)
    _GUICtrlEdit_InsertText($hEdit, "MY INSERTED TEXT", _GUICtrlEdit_LineIndex ($hEdit, $iRandom))

    ; Loop until user exits
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
EndFunc   ;==>_Example_Internal

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

That's from the manual and i read about the functions used in that example and played with the script. Still, i don't understand how to use '_GUICtrlEdit_GetLineCount' and '_GUICtrlEdit_InsertText' to insert text to a predetermined line number in an edit control. And i'm not seeing how 'Random' can be used to generate the current line number from the caret position. I can convert 'WinGetCaretPos' to a line number, but now i need to write to that line number.

I've been toying with different methods of doing this for days and days and none are reliable/work. I'm so close to being done with this script but being able to simply write to a line number like "_FileWriteToLine" does is driving me :)

Link to comment
Share on other sites

That's from the manual and i read about the functions used in that example and played with the script. Still, i don't understand how to use '_GUICtrlEdit_GetLineCount' and '_GUICtrlEdit_InsertText' to insert text to a predetermined line number in an edit control. And i'm not seeing how 'Random' can be used to generate the current line number from the caret position. I can convert 'WinGetCaretPos' to a line number, but now i need to write to that line number.

I've been toying with different methods of doing this for days and days and none are reliable/work. I'm so close to being done with this script but being able to simply write to a line number like "_FileWriteToLine" does is driving me :)

Your looking to hard at it, it's simple, the 'Random' and _GUICtrlEdit_GetLineCount represent the line you want to insert text in, I have no idea what line you want to insert at.

For all intents and purposes you only really need to look at

_GUICtrlEdit_InsertText($hEdit, "MY INSERTED TEXT", _GUICtrlEdit_LineIndex ($hEdit, 'your line number goes here'))

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

I spoke too soon...

I still need to find a way to insert text at an edit control line number.

Right now i'm converting WinGetCaretpos() to a line number then using something like this, inside a while loop, to write:

If WinActive($Form1) Then
    $Caret = WinGetCaretPos()
    $Line = (($Caret[1] - 3) / 13)

  Case $Button1
    _GUICtrlEdit_InsertText($Edit1, $Text1 & @CRLF & $Text2 & @CRLF & @CRLF, _GUICtrlEdit_LineIndex($Edit_TextEditor, $Line))

It seems very finicky... works much of the time, then fails, and it's now frustrating the daylights out of me.

Link to comment
Share on other sites

I think you have to stop saying "specified". :)

To me, specified means you are giving it a specific number, which Gary has given you code for (which works in my tests): _GUICtrlEdit_InsertText($hEdit, "MY INSERTED TEXT" & @CRLF, _GUICtrlEdit_LineIndex ($hEdit, LINENUMBER))

It seems to me that the real problem you're having is retrieving the current line number.

Link to comment
Share on other sites

Yeah, for the most part i think you're right, but i had problems with both. The biggest problem is everything getting selected in an edit control when it's focused.

For now i *think* i have it working, but the nature of edit controls (selecting everything) makes it annoying. I asked in another post if there was a way to prevent this and didn't get an answer. If you can help me out, i'd certainly appreciate it.

Link to comment
Share on other sites

Yeah, for the most part i think you're right, but i had problems with both. The biggest problem is everything getting selected in an edit control when it's focused.

For now i *think* i have it working, but the nature of edit controls (selecting everything) makes it annoying. I asked in another post if there was a way to prevent this and didn't get an answer. If you can help me out, i'd certainly appreciate it.

Would look at using _GUICtrlEdit_SetSel and set the start and end to the same position where you want to insert the text

Or instead of using the built-in edit control you could use the _GUICtrlEdit_Create, doesn't select all when it gets focus.

Edited by GaryFrost

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

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...