Jump to content

solved: GUICtrlRead from GUICtrlCreateEdit not working & assigning values to strings


Recommended Posts

hey folks,

as the topic says, I can't get the Read function to respond to the stuff appearing in Edit. Could anyone point me at what I'm overlooking here?

Here's a short example of how I'm going about it (I've stripped most of the unnecessary code away and such for this example)

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>

$hGUI = GUICreate("Test", 500, 500)
$Edit = GUICtrlCreateEdit("", 10, 10, 480, 300, BitOR($WS_VSCROLL, $ES_MULTILINE, $WS_EX_TRANSPARENT))
$Input = GUICtrlCreateInput("", 60, 330, 375, 20)
GUISetState()

While 1
    $msg = GUIGetMsg()
    If GuiCtrlRead($Edit) = "test" Then
        MsgBox (-1, "test", "it works!")
    EndIf
    Switch $msg
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $Input
            GUICtrlSetData($Edit, GUICtrlRead($Input) & @CRLF, 1)
            GuiCtrlSetData($Input, "")
        
    EndSwitch
WEnd

The eventual goal of all this is that I want the Read function to pick up on every individual word typed by the user in the input box, and I was trying to use GUICtrlRead for this. If there's a more efficient function for this I'm all ears :huh2:

edit: oddly enough when I change If GuiCtrlRead($Edit) to If GuiCtrlRead($Input) I actually do get the messagebox to appear, but then it does so as soon as you finish typing, which is kinda what I don't want.

Edited by mortog
Link to comment
Share on other sites

Hi mortog,

Here's a quick example :huh2:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Demo1", 400, 250)
$Edit1 = GUICtrlCreateEdit("", 5, 5, 390, 210)
$Button1 = GUICtrlCreateButton("Check", 5, 220, 80, 25, $WS_GROUP)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            Local $sEditData = GUICtrlRead($Edit1)
            MsgBox(64, @ScriptName, "Contents of Edit1: " & @CR & $sEditData)
            MsgBox(64, @ScriptName, "The word ""test"" is in Edit1: " & Not (StringInStr($sEditData, "test") = 0))
    EndSwitch
WEnd

Hope this gives you some ideas,

-smartee

Link to comment
Share on other sites

@Mortog

Please remember

If you fill the edit box with an external text file and this external file is large you will need to set the Limit of the Editbox

For Example :

Check the size of c:\temp\large.txt and fill the editbox with large.txt

GUICtrlSetLimit($Edit1, FileGetSize("c:\temp\large.txt"))

GUICtrlSetData($Edit1, FileRead(("c:\temp\large.txt"))

Best regards,Emiel Wieldraaijer

Link to comment
Share on other sites

Hey Emiel, thanks for the tip, I'll keep that in mind. However at the moment I wasn't planning to paste .txt contents into the edit box.

There was another thing I was curious about and perhaps you could point me in the right direction.

I intend to use only input from the user appearing in the edit box, however this input can be up to 4-5 words.

I was wondering if it would be possible to assign a value to each individual string entered.

For example if you have the line: "this is an example"

Is there a function that allows me to assign a numeric value to each of the individual words? So let's say this=1 is=2 an=3 example=4

Is there perhaps something along the line of: a value is assigned to each bunch of strings between spaces?

I've been going over the autoit documentation for quite a while now without finding a solution.

The request may seem a bit odd, but it's for a linguistic project I wanted to pursue.

edit: I'm currently checking up _StringBetween to determain if it can spot a space and then assign a numeric value to each word before each space in the string.

Edited by mortog
Link to comment
Share on other sites

You can use StringSplit() to split the user input at every space to get an array.

Or, you can use StringRegExp() to pull an array of words.

And/Or, you can construct a loop and use the Assign function to put the words into variables like $1, $2 if you really want that :huh2:

Link to comment
Share on other sites

What would I do without you? :huh2: I was looking at _StringExplode and _StringBetween but didn't manage to get the results I wanted.

StringSplit seems to be spot on and I will definately look into using Assign to add values. However when I tried to test the Stringsplit there was 1 tiny problem, the last word in the line has 2 boxes behind it. Is there a way to get them to disappear?

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <Array.au3>

$hGUI = GUICreate("Test", 500, 500)
$Edit = GUICtrlCreateEdit("", 10, 10, 480, 300, BitOR($WS_VSCROLL, $ES_MULTILINE, $WS_EX_TRANSPARENT))
$Input = GUICtrlCreateInput("", 60, 330, 375, 20)
GUISetState()

While 1
    $msg = GUIGetMsg()
    If GuiCtrlRead($Edit) = "test" Then
        MsgBox (-1, "test", "it works!")
    EndIf
    Switch $msg
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $Input
            GUICtrlSetData($Edit, GUICtrlRead($Input) & @CRLF, 1)
            GuiCtrlSetData($Input, "")
        Case $Edit
            $text = GUICtrlRead($Edit)
            $array = StringSplit($text, " ")
            _ArrayDisplay($array)   
    EndSwitch
WEnd
Link to comment
Share on other sites

Hmm, odd, but i guess you can try $text=StringTrimRight($text,2) before you split it :huh2:

I'm guessing they're the carriage return and linefeed characters. What text are you entering into the Edit?

Link to comment
Share on other sites

Actually I found another solution to work around it that works perfectly.

After the StringSplit I've used _ArrayToString which should allow me to get all individual inputs and assign values to them. (kinda working from A to B to A ;))

So in a quick test I modified it to:

Case $Edit
            $text = GUICtrlRead($Edit)
            $array = StringSplit($text, " ")
            MsgBox(0,"test", _ArrayToString($array, @CR, 1, 40))

Obviously that's just to test if it works, but I believe from here I should be able to assign values to each individual string with a little bit of tinkering :huh2: Thanks again for all your help, I owe you one.

Link to comment
Share on other sites

It took me a while to figure out why this is behaving strangely. Wherever I click on the input control, other than the one I am typing in, I get an array display. That's very confusing, lol.

This is also confusing:

MsgBox (-1, "test", "it works!")

Although it seems to work, I can't find any reference to message box flag = -1

Link to comment
Share on other sites

Aye, the array display was just to make sure I was getting the output I was looking for since a msgbox would just display blank info, I've now completely ditched that part though ;)

As for the messagebox, I believe I ran into it in a helpfile, but you're right -1 is kinda pointless, which is also why in my last reply I changed it to 0.

I'm still quite a noob when it comes to autoit, but I'm learning thanks to this great community :huh2:

Link to comment
Share on other sites

no worries Czardas :huh2:

However unfortunately I run into a new problem. It seems the string length seems to be messing up bigtime. When I try to add functions later such as creating an array and then try to do an array search (or w/e really) it always seems to think the string is 2 longer then it really is.

To clarify why the code below is the way it is: I'm trying to split all words in a typed sentense and compare it to various arrays such as the $Subject one. However I can't find any method to get a positive confirmation that the input matches the $subject so if I only type "I" (thus I becomming $1) and then use for example _ArrayFindAll to compare the $subject vs $1 to see if I get a match, it will always fail.

Any thoughts? I've scrapped the comparing bit of code and replaced it with a StringLen result to show what I mean.

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <Array.au3>


$hGUI = GUICreate("Test", 500, 500)
$Edit = GUICtrlCreateEdit("", 10, 10, 480, 300, BitOR($WS_VSCROLL, $ES_MULTILINE, $WS_EX_TRANSPARENT))
$Input = GUICtrlCreateInput("", 60, 330, 375, 20)
GUISetState()

Local $Subject[7] = ["I", "you", "he", "she", "it", "we", "they"]

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $Input
            GUICtrlSetData($Edit, GUICtrlRead($Input) & @CRLF, 1)
            GuiCtrlSetData($Input, "")
        Case $Edit
            $text = GUICtrlRead($Edit)
            $array = StringSplit($text, " ")
            $1 = _ArrayToString($array, "", 1, 1)
            $2 = _ArrayToString($array, "", 2, 2)
            $3 = _ArrayToString($array, "", 3, 3)
            $4 = _ArrayToString($array, "", 4, 4)
            $5 = _ArrayToString($array, "", 5, 5)
            $6 = _ArrayToString($array, "", 6, 6)
            $7 = _ArrayToString($array, "", 7, 7)
            $8 = _ArrayToString($array, "", 8, 8)
            $9 = _ArrayToString($array, "", 9, 9)
            $StringLen = StringLen($1)
            MsgBox(0, "test", $StringLen)
    EndSwitch
WEnd
Edited by mortog
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...