Sign in to follow this  
Followers 0
mlewis412

String Replace with input box?

5 posts in this topic

Hello,

I was wondering if I can get some assistance with this script.  I put the $input1 in the StringReplace to give a better understanding on what I am trying to accomplish. Long story short, I am trying to have input box 1 be free text and any space that is input will be replaced with a  "-" (minus). 

Thanks you all!

#include <MsgBoxConstants.au3>

; Replace a blank space (' ') with a - (minus) character.
Local $sString = StringReplace("$input1", " ", "-")
Local $iReplacements = @extended
$parentgui=GUICreate("Convert", 600, 620)
$input1= GUICtrlCreateInput ("",10,10,200,200)
$input2= GUICtrlCreateInput ("",300,10,200,200)
$convert= GUICtrlCreatebutton ("Convert",220,100,50,50)
GUISetState(@SW_SHOW)



While 1
    $iMsg = GUIGetMsg()



      GUISetState(@SW_SHOW)


              Select
              Case $iMsg = $convert
            ControlSetText ("Convert","","[CLASS:Edit; INSTANCE:2]", $sString)
            ;($MB_SYSTEMMODAL, "", $iReplacements & " replacements were made and the new string is:" & @CRLF & @CRLF & $sString)

 Endselect
WEND

Share this post


Link to post
Share on other sites



You're almost there.

What you need to change is that the $sString needs to be filled with the text in $input1 when you click the button, not at the start of your script. Also, "$input1" does not give you the contents of the input control stored in $input1, you need ControlGetText for that.

This is your code with these changes. They're minimal, but I guess this is what you're looking for.

Also, please tidy your code by pressing ctrl+t (if you're using SciTE 4 autoit3), it helps to make the code readable and can detect some errors before running.

#include <MsgBoxConstants.au3>

; Replace a blank space (' ') with a - (minus) character.
Local $iReplacements = @extended
$parentgui = GUICreate("Convert", 600, 620)
$input1 = GUICtrlCreateInput("", 10, 10, 200, 200)
$input2 = GUICtrlCreateInput("", 300, 10, 200, 200)
$convert = GUICtrlCreateButton("Convert", 220, 100, 50, 50)
GUISetState(@SW_SHOW)

While 1
    $iMsg = GUIGetMsg()

    GUISetState(@SW_SHOW)

    Select
        Case $iMsg = $convert
            Local $sString = StringReplace(ControlGetText("","", $input1), " ", "-")
            ControlSetText("Convert", "", "[CLASS:Edit; INSTANCE:2]", $sString)
            ;($MB_SYSTEMMODAL, "", $iReplacements & " replacements were made and the new string is:" & @CRLF & @CRLF & $sString)
    EndSelect
WEnd

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

instead of using ControlGetText use GuiCtrlRead ($input1)

and instead of ControlSetText use GuiCtrlSetData

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

You are doing a declaration of something in this line

Local $sString = StringReplace("$input1", " ", "-")

that isn't created until this line

$input1= GUICtrlCreateInput ("",10,10,200,200)

It doesn't work like that, you need to create that variable value first.

It needs to be in the correct order like the following

Local $sString
$input1 = GUICtrlCreateInput("", 10, 10, 200, 200)
$sString = StringReplace($input1, " ", "-")

It appears to me, that you are probably just needing to use - GUICtrlRead on your Input, and doing your replacement on the value returned.

Local $sString
$input1 = GUICtrlCreateInput("", 10, 10, 200, 200)

$sString = GUICtrlRead($input1)
$sString = StringReplace($sString, " ", "-")

You could use those last two lines in your button code.

Case $iMsg = $convert
     $sString = GUICtrlRead($input1)
     $sString = StringReplace($sString, " ", "-")
     ControlSetText("Convert", "", "[CLASS:Edit; INSTANCE:2]", $sString)

I've not tested it of course, but that is the idea roughly, in simple terms.

Edited by TheSaint

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites

Thank you all for the input I really appreciate it so much! You're the best evaaaa!

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  
Followers 0