Jump to content
Sign in to follow this  
zackrspv

[SOLVED] Manipulate text in edit box

Recommended Posts

zackrspv

Ok,

Let's say i have an edit box:

$EditSend = GUICtrlCreateEdit("", 8, 550, 539, 51, $WS_VSCROLL)
$Send = GUICtrlCreateButton("Send", 550, 550, 57, 53, $BS_DEFPUSHBUTTON)
GUICtrlSetState($EditSend, $GUI_FOCUS)

And i have the following routine:

case $imgButton
       $olddata = GUICtrlRead($EditSend, 1)
       guictrlsetdata($EditSend, $olddata & " " & "[img] " & "[/img]")
       GUICtrlSetState($EditSend, $GUI_FOCUS); <--- Reset focus
            
case $codeButton
    $olddata = GUICtrlRead($EditSend, 1)
    guictrlsetdata($EditSend, $olddata & " " & "[code] " & "[/ code]")
    GUICtrlSetState($EditSend, $GUI_FOCUS); <--- Reset focus
            
case $linkButton
    $olddata = GUICtrlRead($EditSend, 1)
    guictrlsetdata($EditSend, $olddata & " " & "[link] " & "[/link]")
    GUICtrlSetState($EditSend, $GUI_FOCUS); <--- Reset focus
    
case $emailButton
    $olddata = GUICtrlRead($EditSend, 1)
    guictrlsetdata($EditSend, $olddata & " " & "[email] " & "[/email]")
    GUICtrlSetState($EditSend, $GUI_FOCUS); <--- Reset focus
    
case $quoteButton
    $olddata = GUICtrlRead($EditSend, 1)
    guictrlsetdata($EditSend, $olddata & " " & "[quote]" & "[/quote]")
    GUICtrlSetState($EditSend, $GUI_FOCUS); <--- Reset focus

NOTE: I know the above part '[/ code]' looks funny, but it's the only way to keep it working in the code box on this forum :D obviously in the script it's written properly.

Now, as you can see from the above routine, it will grab the text that is already in there, and insert quick text into the box and still keep the text that was there before; while this does ALMOST what I want it to do, I want to know if it is possible to:

a.) Grab the text that is selected

B.) insert the tags AROUND the selected tag (i can accomlish this with regex's as i have already done something similiar in another part of the script:

$noffset = 1
    $linktext = StringRegExp($msg, "(?i)(?s)(?:link])(.*?)(?:\[/link)", 1, $noffset)
    if @error = 1 Then
    Else
        $msg = StringReplace($msg, "[link]", '<a href="')
        $msg = StringReplace($msg, "[/link]", '" target="_blank">'&$linktext[0]&'</a>')
    EndIf

c.) keep the text that is not selected in it's current position.

It's very similar to how this forum uses bb code within the posts. Ie you can select what you want 'bolded' and then hit the 'bold' button and it puts the tags around it without messing with the surrounding text.

Is that possible w/ edit controls?

Edited by zackrspv

-_-------__--_-_-____---_-_--_-__-__-_ ^^€ñ†®øÞÿ ë×阮§ wï†høµ† ƒë@®, wï†høµ† †ïmë, @ñd wï†høµ† @ †ïmïdï†ÿ ƒø® !ïƒë. €×阮 ñø†, bµ† ïñ§†ë@d wï†hïñ, ñ@ÿ, †h®øµghøµ† †hë 맧ëñ§ë øƒ !ïƒë.

Share this post


Link to post
Share on other sites
zackrspv

So, I figured i'd use the controlcommand thing:

$olddata = GUICtrlRead($EditSend, 1)
            $selectedData = ControlCommand("", "", $EditSend, "GetSelected", "")
            $noffset = 1
            $omitdata = StringRegExp($olddata, "(?i)(?s)(.*?)("&$selectedData&")(.*?)(?:\z)", 1, $noffset)
            if @error = 1 Then
            Else
            ConsoleWrite("(?i)(?s)(.*?)("&$selectedData&")(.*?)(?:\z)"&@CRLF)
                guictrlsetdata($EditSend, $omitdata[0]& "[img] " &$omitdata[1]& "[/img]"&$omitdata[2])
                GUICtrlSetState($EditSend, $GUI_FOCUS); <--- Reset focus
            EndIf

But, as you can see, it seems to not keep the text selected. For example, if the text in the box was written as:

this is my test you see, and i like it

and you only selected 'test you see', the script would return:

his is my test you see, and i like it

Instead of the actual selected text. note also, I can not seem to get it to keep doing that whenever i re-select the text, it just stays the same; probabyl because i didn't clear the $olddata before, but who knows. I thought i had it, but then i don't :/ Frustrating. Edited by zackrspv

-_-------__--_-_-____---_-_--_-__-__-_ ^^€ñ†®øÞÿ ë×阮§ wï†høµ† ƒë@®, wï†høµ† †ïmë, @ñd wï†høµ† @ †ïmïdï†ÿ ƒø® !ïƒë. €×阮 ñø†, bµ† ïñ§†ë@d wï†hïñ, ñ@ÿ, †h®øµghøµ† †hë 맧ëñ§ë øƒ !ïƒë.

Share this post


Link to post
Share on other sites
Squirrely1

This works and is ready to test - replaces the selected text, surrounding it with code tags:

; Example showing how to replace the selected text of an edit control,
;   with the same text enclosed with code tags.
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <GuiEdit.au3>

Global $OpenTag
Global $CloseTag

$sqGUI = GUICreate("SquireBiz")
$EditSend = GUICtrlCreateEdit("", 0, 0, 350, 350, $WS_VSCROLL)
$codeButton = GUICtrlCreateButton("Code", 11, 360, 57, 31)
$boldButton = GUICtrlCreateButton("Bold", 73, 360, 57, 31)
GUICtrlSetState($EditSend, $GUI_FOCUS)
GUISetState()
While 1
    $msg = GUIGetMsg()
    Switch $msg
        
        Case $codeButton
            $OpenTag = "[" & "code]"
            $CloseTag = "[" & "/code]"
            Replace_MySelection()
            
        Case $boldButton
            $OpenTag = "[" & "b]"
            $CloseTag = "[" & "/b]"
            Replace_MySelection()
            
        Case $GUI_EVENT_CLOSE
            ExitLoop
            
    EndSwitch
    Sleep(30)
WEnd

Func Replace_MySelection()
    $SelectedArr = _GUICtrlEdit_GetSel($EditSend)
    $SelectedArr[0] += 1
    $count = $SelectedArr[1] - $SelectedArr[0] + 1
    If $count > 0 Then
        $allText = _GUICtrlEdit_GetText($EditSend)
        $selectedData = StringMid($allText, $SelectedArr[0], $count)
        _GUICtrlEdit_ReplaceSel($EditSend, $OpenTag & $selectedData & $CloseTag)
    EndIf
EndFunc ;==>Replace_MySelection

Thanks to the developer GaryFrost for the UDF's in <GuiEdit.au3>

Edit: Improved code above, adding a function.

Edited by Squirrely1

Das Häschen benutzt Radar

Share this post


Link to post
Share on other sites
zackrspv

This works and is ready to test - replaces the selected text, surrounding it with code tags:

; Example showing how to replace the selected text of an edit control,
;   with the same text enclosed with code tags.
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <GuiEdit.au3>

Global $OpenTag
Global $CloseTag

$sqGUI = GUICreate("SquireBiz")
$EditSend = GUICtrlCreateEdit("", 0, 0, 350, 350, $WS_VSCROLL)
$codeButton = GUICtrlCreateButton("Code", 11, 360, 57, 31)
$boldButton = GUICtrlCreateButton("Bold", 73, 360, 57, 31)
GUICtrlSetState($EditSend, $GUI_FOCUS)
GUISetState()
While 1
    $msg = GUIGetMsg()
    Switch $msg
        
        Case $codeButton
            $OpenTag = "[" & "code]"
            $CloseTag = "[" & "/code]"
            Replace_MySelection()
            
        Case $boldButton
            $OpenTag = "[" & "b]"
            $CloseTag = "[" & "/b]"
            Replace_MySelection()
            
        Case $GUI_EVENT_CLOSE
            ExitLoop
            
    EndSwitch
    Sleep(30)
WEnd

Func Replace_MySelection()
    $SelectedArr = _GUICtrlEdit_GetSel($EditSend)
    $SelectedArr[0] += 1
    $count = $SelectedArr[1] - $SelectedArr[0] + 1
    If $count > 0 Then
        $allText = _GUICtrlEdit_GetText($EditSend)
        $selectedData = StringMid($allText, $SelectedArr[0], $count)
        _GUICtrlEdit_ReplaceSel($EditSend, $OpenTag & $selectedData & $CloseTag)
    EndIf
EndFunc;==>Replace_MySelection

Thanks to the developer GaryFrost for the UDF's in <GuiEdit.au3>

Edit: Improved code above, adding a function.

Thanks for the fast reply; but now that I have modified my script to do the following:

case $imgButton
            $OpenTag = "[" & "img]"
            $CloseTag = "[" & "/img]"
            call("Replace_MySelection")
            GUICtrlSetState($EditSend, $GUI_FOCUS); <--- Reset focus

AND

Func Replace_MySelection()
    $SelectedArr = _GUICtrlEdit_GetSel($EditSend)
    $SelectedArr[0] += 1
    $count = $SelectedArr[1] - $SelectedArr[0] + 1
    If $count > 0 Then
        $allText = _GUICtrlEdit_GetText($EditSend)
        $selectedData = StringMid($allText, $SelectedArr[0], $count)
        _GUICtrlEdit_ReplaceSel($EditSend, $OpenTag & $selectedData & $CloseTag)
    EndIf
EndFunc;==>Replace_MySelection

It still only returns the same result as the commandcontrol argument above. It seems that no matter what I select the _GUICtrlEdit_GetSel($EditSend) still only returns the first and last character of the string, not of the selection.

So, as an example:

String to test Note that the word 'another' is selected.

this is another test.

Result

Positions returned:  Start:  1 End: 21
Text Returned: t[img]his is another test.[/img]

So, not working :/

Edited by zackrspv

-_-------__--_-_-____---_-_--_-__-__-_ ^^€ñ†®øÞÿ ë×阮§ wï†høµ† ƒë@®, wï†høµ† †ïmë, @ñd wï†høµ† @ †ïmïdï†ÿ ƒø® !ïƒë. €×阮 ñø†, bµ† ïñ§†ë@d wï†hïñ, ñ@ÿ, †h®øµghøµ† †hë 맧ëñ§ë øƒ !ïƒë.

Share this post


Link to post
Share on other sites
Squirrely1

The function "_GUICtrlEdit_GetSel" returns a one-dimensional array having two elements both of which contain numbers. The StringMid function call in the user function I wrote is pretty important.

Look at my code again and run it - I didn't include Case structures for ALL of the buttons you need - but you should be able to base your code on my code.

Just be sure you have no overlapping controls in your version of the running GUI - use the Window Info tool to double-check your code - but I changed the position and size of controls for my GUI.

And instead of this:

GUICtrlSetState($EditSend, $GUI_FOCUS); <--- Reset focus

which didn't work very well, you should try this untested code:

ControlClick ($sqGUI, "", $EditSend)

I think that for images, you enclose its URL in the img tags - similar as with the "Bold", or "Code" buttons.

In order to handle some of the tags used on the AutoIt forum, you may need another function which you can build which is like my user function called "Replace_MySelection" - formatting the second parameter of its "_GUICtrlEdit_ReplaceSel" function as may be required.

Edited by Squirrely1

Das Häschen benutzt Radar

Share this post


Link to post
Share on other sites
zackrspv

The function "_GUICtrlEdit_GetSel" returns a one-dimensional array having two elements both of which contain numbers. The StringMid function call in the user function I wrote is pretty important.

Look at my code again and run it - I didn't include Case structures for ALL of the buttons you need - but you should be able to base your code on my code.

Just be sure you have no overlapping controls in your version of the running GUI - use the Window Info tool to double-check your code - but I changed the position and size of controls for my GUI.

And instead of this:

GUICtrlSetState($EditSend, $GUI_FOCUS); <--- Reset focus

which didn't work very well, you should try this untested code:

ControlClick ($sqGUI, "", $EditSend)

I think that for images, you enclose its URL in the img tags - similar as with the "Bold", or "Code" buttons.

In order to handle some of the tags used on the AutoIt forum, you may need another function which you can build which is like my user function called "Replace_MySelection" - formatting the second parameter of its "_GUICtrlEdit_ReplaceSel" function as may be required.

All i did was copy and paste your code EXACTLY as you had written it in your origonal post. And this is the array it gives:

Posted Image

As you can see from the image, it only shows the START and END of the string, not what is selected. If it was what was selected, it would show me like 16 to 21 instead of 1 to 21. i didn't change your code from what you used, so i'm not sure where you want me to change it at.

Either that, or i'm just not awake enough to get what you are saying lol in either case i'd like to know why the routine isn't grabbing the selected text.

Edited by zackrspv

-_-------__--_-_-____---_-_--_-__-__-_ ^^€ñ†®øÞÿ ë×阮§ wï†høµ† ƒë@®, wï†høµ† †ïmë, @ñd wï†høµ† @ †ïmïdï†ÿ ƒø® !ïƒë. €×阮 ñø†, bµ† ïñ§†ë@d wï†hïñ, ñ@ÿ, †h®øµghøµ† †hë 맧ëñ§ë øƒ !ïƒë.

Share this post


Link to post
Share on other sites
zackrspv

All i did was copy and paste your code EXACTLY as you had written it in your origonal post. And this is the array it gives:

Posted Image

As you can see from the image, it only shows the START and END of the string, not what is selected. If it was what was selected, it would show me like 16 to 21 instead of 1 to 21. i didn't change your code from what you used, so i'm not sure where you want me to change it at.

Either that, or i'm just not awake enough to get what you are saying lol in either case i'd like to know why the routine isn't grabbing the selected text.

Well, i've also tried the ^C and INS thing thati 've seen on here too, but that doesn't select the text either. I've looked at the routine for _GUICtrlEdit_GetSel() too and it just seems like it only gets first and last characters, not selected.

How can this be accomplished?


-_-------__--_-_-____---_-_--_-__-__-_ ^^€ñ†®øÞÿ ë×阮§ wï†høµ† ƒë@®, wï†høµ† †ïmë, @ñd wï†høµ† @ †ïmïdï†ÿ ƒø® !ïƒë. €×阮 ñø†, bµ† ïñ§†ë@d wï†hïñ, ñ@ÿ, †h®øµghøµ† †hë 맧ëñ§ë øƒ !ïƒë.

Share this post


Link to post
Share on other sites
zackrspv

Well, i've also tried the ^C and INS thing thati 've seen on here too, but that doesn't select the text either. I've looked at the routine for _GUICtrlEdit_GetSel() too and it just seems like it only gets first and last characters, not selected.

How can this be accomplished?

Found out the problem.

I wasn't using buttons:

$imgButton = GUICtrlCreatepic("image.bmp", 8, 603, 20, 20)
$codeButton = GUICtrlCreatepic("code.bmp", 28, 603, 20, 20)
$linkButton = GUICtrlCreatepic("link.bmp", 48, 603, 20, 20)
$emailButton = GUICtrlCreatepic("email.bmp", 68, 603, 20, 20)
$quoteButton = GUICtrlCreatepic("quote.bmp", 88, 603, 20, 20)

Apparently, when using images it doesn't work.

I'll have to try doing images as buttons and see if that works.


-_-------__--_-_-____---_-_--_-__-__-_ ^^€ñ†®øÞÿ ë×阮§ wï†høµ† ƒë@®, wï†høµ† †ïmë, @ñd wï†høµ† @ †ïmïdï†ÿ ƒø® !ïƒë. €×阮 ñø†, bµ† ïñ§†ë@d wï†hïñ, ñ@ÿ, †h®øµghøµ† †hë 맧ëñ§ë øƒ !ïƒë.

Share this post


Link to post
Share on other sites
Squirrely1

I guess that you figured out that this code - all of it except the third to the last line - works together to get the selected text and store it in $selectedData -

Func Replace_MySelection()
    $SelectedArr = _GUICtrlEdit_GetSel($EditSend); returns two values, stored in $SelectedArr[0] & $SelectedArr[1]
    $SelectedArr[0] += 1; the first element in the array returned by the above line, is one too little - adding 1.
    $count = $SelectedArr[1] - $SelectedArr[0] + 1; gets a count to use in the StringMid statement 3 line below.
    If $count > 0 Then; only replace the selection if there is indeed some text selected.
        $allText = _GUICtrlEdit_GetText($EditSend); $allText now stores all the text in the edit control.
        $selectedData = StringMid($allText, $SelectedArr[0], $count); returns the part of $allText we want.
; _GUICtrlEdit_ReplaceSel($EditSend, $OpenTag & $selectedData & $CloseTag); replaces the selected text.
    EndIf
EndFunc;==>Replace_MySelection

The line that begins with a semi-colon that you can remove - the third to the last line - replaces the text selected with this one concatenated string:

$OpenTag & $selectedData & $CloseTag

Edited by Squirrely1

Das Häschen benutzt Radar

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  

×