Jump to content

trouble with Case statement


Recommended Posts

I can't figure out why this case statement won't accept this variable. If I do this

Case @GUI_CtrlId = $btn & $i it only looks for $i or I try

Case @GUI_CtrlId = "$btn" & $i it fails or I try

Case @GUI_CtrlId = $btINT it fails

$arrAPP is simple .ini with keys being appnames and values being paths

ex. some app=\\server\apps\someapp.exe

Func _appOPEN()
         MsgBox(0, "func appOPEN", "button was clicked")
For $i = 1 to UBound($arrAPP) - 1
    $btINT = "$btn" & $i
    MsgBox(0, "func appOPEN", "in for loop now, btINT is: " & $btINT)
    Select  
    Case @GUI_CtrlId = $btINT
        MsgBox(0, "func appOPEN", "control id is found")
    EndSelect
Next
EndFunc

Am I missing something here? How come concating $btn & $i results in only $i? That was my first thought to try. Confused about that.

Thnx,

sul

Link to comment
Share on other sites

I can't figure out why this case statement won't accept this variable. If I do this

Case @GUI_CtrlId = $btn & $i    it only looks for $i    or I try

Case @GUI_CtrlId = "$btn" & $i    it fails                  or I try

Case @GUI_CtrlId = $btINT          it fails

$arrAPP is simple .ini with keys being appnames and values being paths

ex. some app=\\server\apps\someapp.exe

Func _appOPEN()
         MsgBox(0, "func appOPEN", "button was clicked")
For $i = 1 to UBound($arrAPP) - 1
    $btINT = "$btn" & $i
    MsgBox(0, "func appOPEN", "in for loop now, btINT is: " & $btINT)
    Select    
    Case @GUI_CtrlId = $btINT
        MsgBox(0, "func appOPEN", "control id is found")
    EndSelect
Next
EndFunc

Am I missing something here? How come concating $btn & $i results in only $i? That was my first thought to try. Confused about that.

Thnx,

sul

<{POST_SNAPBACK}>

first thing i notice is you have $btn in quotes in the line:

$btINT = "$btn" & $i

also, i'm thinking that this is only a partial code list... could we see a bigger section? just because it looks like you're using $btn prior to declaring or assigning it a value (unless i'm just tired and not reading right...)

Link to comment
Share on other sites

Certainly I can post the code. Just didn't want to clutter things up if not needed.

HotKeySet("^!x", "MyExit");exit hotkeys are Ctrl + Alt + X
#include <GUIConstants.au3>
#include <file.au3>

$FServer = "fileserver"
$Fpriv = ""
$Fpub = "test"

Global $bHT, $btn, $btn1, $btINT
$welmsg = "Generic welcome message"

Dim $arrAPP
$arrAPP = IniReadSection("\\fileserver\test\mlist.ini", "path")

Opt("GUIOnEventMode", 1)

$winWELCOME = GUICreate("menu", 550, 400);welcome window
If @error = 1 Then 
    MsgBox(0, "", "Error creating the Welcome window")
EndIf
GUISetOnEvent($GUI_EVENT_CLOSE, "_xClose")
GUICtrlCreateLabel($welmsg, 30, 10, 500, 325)
$font = "Tahoma Bold"
$btnPROCEED = GUICtrlCreateButton("PROCEED", 30, 350,65,30) 
GUICtrlSetOnEvent($btnPROCEED, "_btnPROC")
GUICtrlSetState($btnPROCEED, $GUI_FOCUS)
GUISetState(@SW_SHOW)

$winAPPS = GUICreate("HGC SHORTCUTS", 500, 500)
GUISetOnEvent($GUI_EVENT_CLOSE, "_xClose")
$btFIN = GUICtrlCreateButton("Exit", 30, 450, 50, 30)
GUICtrlSetOnEvent($btFIN, "_xClose")
GUICtrlCreateLabel("Click to open", 30, 10)

For $z = 1 to UBound($arrAPP) - 1
    $bHT = $bHT + 50
    Assign("btn" & $z, GUICtrlCreateButton($arrAPP[$z][0], 30, $bHT))
    GUICtrlSetOnEvent(-1, "_appOPEN")
Next
GUISetState(@SW_HIDE)

Sleep(2000)
While 1
    Sleep(1000)
WEnd

Func _appOPEN()
    MsgBox(0, "func appOPEN", "button was clicked")
For $i = 1 to UBound($arrAPP) - 1
    $btINT = "$btn" & $i
    MsgBox(0, "func appOPEN", "in for loop now, btINT is: " & $btINT)
    Select    
    Case @GUI_CtrlId = $btINT
        MsgBox(0, "func appOPEN", "control id is found")
    EndSelect
Next
EndFunc

Func MyExit()
    Exit 
EndFunc

Func _xClose()
    MsgBox(0, "func xClose", "")
    Exit
EndFunc

Func _btnPROC()
    GUISetState(@SW_HIDE, $winWELCOME)
    GUISetState(@SW_SHOW, $winAPPS)
EndFunc

$btINT = $btn & $i gives $i as $btn is nothing. I tried declaring globally $btn = "$btn", but that did not work either. Using $btINT = "$btn" & $i gives, in a msgbox, $btn1 (or whateve $i is).

Just a script to practice dumping an .ini to array, and then create GUI off that array and have events handled too.

Thanks for looking at this though.

sul

Note, have tried Case @GUI_CtrlId = others listed on first post as well.

Edited by sulfurious
Link to comment
Share on other sites

I have another question. Why is it that when you use iniWrite to add a line, and then when you read that line from an array it adds no CRLF. But, if you write a line to a text file, and then dump that to an array, you need to use StringTrimRight 1 to omit the CRLF character? Is that some setting in notepad or is there an option to use when writing a line that stops that. Not that I mind coding around it, just curious.

sul

Link to comment
Share on other sites

I have another question. Why is it that when you use iniWrite to add a line, and then when you read that line from an array it adds no CRLF. But, if you write a line to a text file, and then dump that to an array, you need to use StringTrimRight 1 to omit the CRLF character? Is that some setting in notepad or is there an option to use when writing a line that stops that. Not that I mind coding around it, just curious.

sul

<{POST_SNAPBACK}>

How are you reading the text file into an array? Are you using the _FileReadToArray() function? That was an issue with an older version of that function - get the latest UDFs to fix that, or use your own file-to-array routine.
Link to comment
Share on other sites

Yes, get the latest beta to update your UDF, or just paste this into your file.au3 file (replacing the current one):

;===============================================================================
;
; Description:    Reads the specified file into an array.
; Syntax:          _FileReadToArray( $sFilePath, $aArray )
; Parameter(s):  $sFilePath - Path and filename of the file to be read
;                  $aArray  - The array to store the contents of the file
; Requirement(s):   None
; Return Value(s):  On Success - Returns 1
;                  On Failure - Returns 0 and sets @error = 1
; Author(s):        Jonathan Bennett <jon at hiddensoft com>
; Note(s):        None
;
;===============================================================================
Func _FileReadToArray($sFilePath, ByRef $aArray)
;==============================================
; Local Constant/Variable Declaration Section
;==============================================
    Local $hFile
    
    $hFile = FileOpen($sFilePath, 0)
    
    If $hFile = -1 Then
        SetError(1)
        Return 0
    EndIf
    
    $aArray = StringSplit( StringStripCR( FileRead($hFile, FileGetSize($sFilePath))), @LF)
    
    FileClose($hFile)
    Return 1
EndFunc  ;==>_FileReadToArray

IIRC, the only difference is that encapsulated StringStripCR function call.

Link to comment
Share on other sites

Sure. Pretty simple at this point. I just renamed calculator to some_app#.exe.

Here it is. If I attach it correctly that is.

sul

EDIT can't seem to attach it for some reason. Tried it twice. Regardless, called Mlist.ini and contents are this

[path]

some app1=\\fileserver\test\some_app1.exe

some app2=\\fileserver\test\some_app2.exe

some app3=\\fileserver\test\some_app3.exe

Edited by sulfurious
Link to comment
Share on other sites

Sure. Pretty simple at this point. I just renamed calculator to some_app#.exe.

Here it is. If I attach it correctly that is.

sul

EDIT can't seem to attach it for some reason. Tried it twice. Regardless, called Mlist.ini and contents are this

[path]

some app1=\\fileserver\test\some_app1.exe

some app2=\\fileserver\test\some_app2.exe

some app3=\\fileserver\test\some_app3.exe

<{POST_SNAPBACK}>

I'm not having any errors or anything, and it is outputting as it should, $btn1, $btn2, $btn3
Link to comment
Share on other sites

Umm. Errr. Hmmm. Same code?

Okay, well, maybe I'd best try a beta version. But, heh heh, which one? There seems to be quite a few in the archives. Just go with the latest, I think it was 68?

Maybe that is where the problem is.

If you used the code I posted, I can't think of why it works on yours but not on mine, other than maybe you using beta version and I'm not.

sul

Link to comment
Share on other sites

Umm. Errr. Hmmm. Same code?

Okay, well, maybe I'd best try a beta version. But, heh heh, which one? There seems to be quite a few in the archives. Just go with the latest, I think it was 68?

Maybe that is where the problem is.

If you used the code I posted, I can't think of why it works on yours but not on mine, other than maybe you using beta version and I'm not.

sul

<{POST_SNAPBACK}>

the only change i made was the path to the ini
Link to comment
Share on other sites

Well, I downloaded latest beta. Installed it. Toggled it, which I gather means that engine is used for AutoIT scripts. Ran my script, and same situation. I don't understand why the exact same script works differently on two different rigs. I know it reads the ini because if I hard code the Case @GUI to $btn1, it works fine.

Does the setup for beta replace the include files? Or maybe I have to do that manually.

CameronsDad, which version are you using?

sul

One more thing. Are you sure that the message box came up that said

"func appOPEN", "control id is found"? Which indicates that the Case expression was fulfilled. That is what I cannot get to work.

Link to comment
Share on other sites

In that code, if you add to this line -

Case @GUI_CtrlId = $btn1 or 2 or 3,

whichever one of the three buttons clicked will pop up the "Control ID found" text. Hardcoding it works fine. I had thought that making $btINT = one of those would have worked as well. But apparently not. Perhaps I do not understand exactly what is happening when I use that $btINT variable there. I am thinking it does not or cannot look at what is in that variable, but because of it being either in a Select statement, or because the function @GUI_CtrlId it cannot see the variable for what it is.

Or perhaps I should do something simple, like work on a worm farm or something.

Thnx fer the help tho.

sul

Link to comment
Share on other sites

Well, I downloaded latest beta. Installed it. Toggled it, which I gather means that engine is used for AutoIT scripts. Ran my script, and same situation. I don't understand why the exact same script works differently on two different rigs. I know it reads the ini because if I hard code the Case @GUI to $btn1, it works fine.

Does the setup for beta replace the include files? Or maybe I have to do that manually.

CameronsDad, which version are you using?

sul

One more thing. Are you sure that the message box came up that said

"func appOPEN", "control id is found"? Which indicates that the Case expression was fulfilled. That is what I cannot get to work.

<{POST_SNAPBACK}>

ah, no i was getting the 'in for loop' messages. you're not going to match your ctrl id with the string "btn" and a number. the macro you're comparing against returns a number only. i changed your btnINT line to this just now:

$btINT = 4103 + $i

and that seems to be working. i got the 4103 by adding a msgbox out to check the value of the macro at click, and it showed 4104 for the first button, 4105 for the second etc... change that line and you should be all set

Link to comment
Share on other sites

Okay, since nobody else pointed it out... (*Edit: Until after I started writing this, making me look like a nerd)

$btINT = "$btn" & $i

That's going to make $btINT equal to a STRING "$btn1"

$btn1 (the VARIABLE) is going to equal a control ID.

That's why if you use

Case @GUI_CtrlId = $btn1 it works fine.

Just try it, do MsgBox(0, 'button 1', $btn1)

What you're probably looking for is something like this:

$btINT = Eval('btn' & $i)

Eval is used when you need to use a string to find the value of a variable.

It's like if you go:

$MyVariable = 'Hello daddy'
$VariableTest1 = '$MyVariable'
$VariableTest2 = Eval('MyVariable')

MsgBox(0, '', $VariableTest1)
MsgBox(0, '', $VariableTest2)

The first message box will actually say "$MyVariable", the secondone will say "Hello daddy"

Understand now?

*Edit: Damnit cameronsdad, now my opening statement of "since nobody else pointed it out" isn't valid. :whistle:

Edited by Saunders
Link to comment
Share on other sites

Okay, since nobody else pointed it out... (*Edit: Until after I started writing this, making me look like a nerd)

$btINT = "$btn" & $i

That's going to make $btINT equal to a STRING "$btn1"

$btn1 (the VARIABLE) is going to equal a control ID.

That's why if you use

Case @GUI_CtrlId = $btn1 it works fine.

Just try it, do MsgBox(0, 'button 1', $btn1)

What you're probably looking for is something like this:

$btINT = Eval('btn' & $i)

Eval is used when you need to use a string to find the value of a variable.

It's like if you go:

$MyVariable = 'Hello daddy'
$VariableTest1 = '$MyVariable'
$VariableTest2 = Eval('MyVariable')

MsgBox(0, '', $VariableTest1)
MsgBox(0, '', $VariableTest2)

The first message box will actually say "$MyVariable", the secondone will say "Hello daddy"

Understand now?

*Edit: Damnit cameronsdad, now my opening statement of "since nobody else pointed it out" isn't valid. :whistle:

<{POST_SNAPBACK}>

technically, i pointed out that he was using a string in my first reply to him, but i disregarded it (until my last post) when he said that's what he wanted.
Link to comment
Share on other sites

CameronsDad, nifty workaround.

I had noticed that the id was a number, but had not thought to try and use that in the equation. Mainly because I was unsure of where it would start at always. I can only assume the 4103 is only going to be applicable to this GUI script? Or is there a standard number at which buttons start or something?

But that does work.

Saunders, that pretty much explains it. Not only that, it explains why it has failed to work in other functions too. In what little vb I have done, I could declare something like that as a variant I think it was, and it would work. So in autoit, there are only two datatypes, numbers and strings. Is that why you have tools such as Eval? because of there not being a variant type?

Makes much sense now.

Thank you all for your time and knowledge.

sul

EDIT -props to Cameronsdad I guess for pointing that out straight away. heh. I did not realize what you meant by that. Sry.

Edited by sulfurious
Link to comment
Share on other sites

CameronsDad, nifty workaround.

I had noticed that the id was a number, but had not thought to try and use that in the equation. Mainly because I was unsure of where it would start at always. I can only assume the 4103 is only going to be applicable to this GUI script? Or is there a standard number at which buttons start or something?

But that does work.

Saunders, that pretty much explains it. Not only that, it explains why it has failed to work in other functions too. In what little vb I have done, I could declare something like that as a variant I think it was, and it would work. So in autoit, there are only two datatypes, numbers and strings. Is that why you have tools such as Eval? because of there not being a variant type?

Makes much sense now.

Thank you all for your time and knowledge.

sul

EDIT -props to Cameronsdad I guess for pointing that out straight away. heh. I did not realize what you meant by that. Sry.

<{POST_SNAPBACK}>

np glad i could help, and i think saunders gets credit for easiest, most consistent work around, his:

if @GUI_CtrlId = $btn1
can be used for other scripts (since the reference to $btn1 returns the same numeric control id as the @GUI_CtrlId) my work around works for THIS scenario, but his is the 'right' way to do it...
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...