Jump to content

Problem with button, edit and _GUICtrlEdit_GetSel()


Darklord
 Share

Recommended Posts

Hi, i was just writing a little script that creats a gui with some buttons and a edit in it and i wanted it to get the selection of the edit and place some bbcodes around the selection.

That was working fine in the old version of autoit i think it was 3.2.8.1 and today i updated to 3.2.10.0 and now the script isnt working any more.

When i select the text and try to press the button the selection is removed and the text is added to the start of the string.

#include <GUIConstants.au3>
#include <GuiEdit.au3>
#include <String.au3>
Opt("WinTitleMatchMode", 4)
$Form1 = GUICreate("AllyPAgeStyler", 885, 765, 150, 132)
$Edit1 = GUICtrlCreateEdit("", 10, 60, 860, 660, $WS_VSCROLL)
$Button1 = GUICtrlCreateButton("[b]", 20, 20, 31, 21, 0)
$Button2 = GUICtrlCreateButton("[i]", 60, 20, 31, 21, 0)
$Button3 = GUICtrlCreateButton("[s]", 100, 20, 31, 21, 0)
$Button4 = GUICtrlCreateButton("[center]", 140, 20, 45, 21, 0)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            inserttag("[b]", "[/b]")
            
        Case $Button2
            inserttag("[i]", "[/i]")
            
        Case $Button3
            inserttag("[s]", "[/s]")
            
        Case $Button4
            inserttag("[center]", "[/center]")
    EndSwitch
WEnd

Func inserttag($tagS, $tagE)
    $aSel = _GUICtrlEdit_GetSel($Edit1)
    $sEdit1 = _GUICtrlEdit_GetText($Edit1)
    If $aSel[0] = $aSel[0] Then
        $sEdit1_2 = StringLeft($sEdit1, $aSel[0])
        $sEdit1 = $sEdit1_2 & $tagS & $tagE & StringRight($sEdit1, StringLen($sEdit1) - $aSel[0])
        _GUICtrlEdit_SetText($Edit1, $sEdit1)
        ControlFocus("[TITLE:AllyPAgeStyler; CLASS:AutoIt v3 GUI;]", "", "[CLASS:Edit; INSTANCE:1]")
        _GUICtrlEdit_SetSel($Edit1, $aSel[0] + StringLen($tagS), $aSel[0] + StringLen($tagS))
    ElseIf $aSel[0] <> $aSel[1] Then
        $sEdit1_2 = StringLeft($sEdit1, $aSel[0])
        $sEdit1_3 = StringMid($sEdit1, $aSel[0] + 1, $aSel[1] - $aSel[0])
        $sEdit1 = StringReplace($sEdit1, $sEdit1_3, "")
        $sEdit1 = $sEdit1_2 & $tagS & $sEdit1_3 & $tagE & StringRight($sEdit1, StringLen($sEdit1) - $aSel[0])
        
        _GUICtrlEdit_SetText($Edit1, $sEdit1)
        ControlFocus("[TITLE:AllyPAgeStyler; CLASS:AutoIt v3 GUI;]", "", "[CLASS:Edit; INSTANCE:1]")
        
    EndIf
    
EndFunc;==>inserttag

can anyone help me? :)

Edited by Darklord
Link to comment
Share on other sites

ok i solved the problem myself :)

That's pretty neat. First post gives problem. No one replies. Second post says it's fixed.

Maybe you could make a third post and welcome yourself to the forums!

But no, I'm going to beat you to it.

Welcome. ^_^

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

  • 4 months later...

I have this same problem..

Can you post what you fixt?

Try this:

#include <GUIConstants.au3>
#include <GuiEdit.au3>

$Form1 = GUICreate("AllyPAgeStyler", 885, 765, 150, 132)

$Edit1 = GUICtrlCreateEdit("", 10, 60, 860, 660, $WS_VSCROLL)

$Button1 = GUICtrlCreateButton("[b]", 20, 20, 31, 21, 0)
$Button2 = GUICtrlCreateButton("[i]", 60, 20, 31, 21, 0)
$Button3 = GUICtrlCreateButton("[s]", 100, 20, 31, 21, 0)
$Button4 = GUICtrlCreateButton("[center]", 140, 20, 45, 21, 0)

GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            inserttag("[b]", "[/b]")
            
        Case $Button2
            inserttag("[i]", "[/i]")
            
        Case $Button3
            inserttag("[s]", "[/s]")
            
        Case $Button4
            inserttag("[center]", "[/center]")
    EndSwitch
WEnd

Func inserttag($tagS, $tagE)
    Local $aSel
    $aSel = _GUICtrlEdit_GetSel($Edit1)
    
    If $aSel[0] = $aSel[1] Then Return False
    
    Local $iText = _GUICtrlEdit_GetText($Edit1)
    
    $iText = StringRegExpReplace($iText, "^.{" & $aSel[0] & "}", "")
    $iText = StringRegExpReplace($iText, "(^.{" & $aSel[1] - $aSel[0] & "}).*", "\1")
    
    _GUICtrlEdit_ReplaceSel($Edit1, $tagS & "" & $iText & "" & $tagE)
EndFunc;==>inserttag
:D
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...