Sign in to follow this  
Followers 0
PhilipH

Help needed to find error in my code

7 posts in this topic

I'll try and be as succinct as possible.

I’m trying to create a GUI to assist in building PCs. My code includes a GUICtrlCreateInput field for the computer name - allowing the user to enter a new name - and a button that will open the System Properties window, navigate to the correct place to change the computer name and enter the new name from the GUICtrlCreateInput field.

The msgbox to confirm the action shows the correct content of my variable (the new name), but when the System Properties dialog is opened the number 4 is inputted not the name in the variable.

I hope the above makes sense!

Can anyone point out where I’m going wrong?

Thanks

#include <GuiConstantsEx.au3>
#include <process.au3>

;VARIABLES

Global Const $OrigComputerName = @ComputerName
Global $MachineName

Opt("GUIOnEventMode", 1) ; Change to OnEvent mode

; Window
$MasterWindow = GUICreate("Computer build utility", 340, 600, @DesktopWidth - 345, @DesktopHeight / 2 - 350); Create the Main GUI.
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")

; Window controls
GUICtrlCreateGroup("Computer Name", 10, 10, 320, 60)
$MachineName = GUICtrlCreateInput($OrigComputerName, 35, 35, 175, 20, 0x1001); Print computer name to the GUI.
GUICtrlSetBkColor(-1, 0xffffff) ;
GUICtrlSetColor(-1, 0x000099) ; Blue

; Reset computer name
$btnResetPCName = GUICtrlCreateButton("Reset", 240, 32, 70)
GUICtrlSetOnEvent($btnResetPCName, "btnResetPCName")

; Change computer name
$btnNameChange = GUICtrlCreateButton("Change Name", 230, 90, 90)
GUICtrlSetOnEvent($btnNameChange, "btnNameChange")

$btnExit = GUICtrlCreateButton("Exit", 125, 560, 90)
GUICtrlSetOnEvent($btnExit, "doExit")
GUISetState(@SW_SHOW)


While 1
    Sleep(1000) ; Idle around
WEnd

; Reset computer name
Func btnResetPCName()
    GUICtrlSetData($MachineName, $OrigComputerName)
EndFunc   ;==>btnResetPCName

; Rename computer
Func btnNameChange()
    #Region --- CodeWizard generated code Start ---
    ;MsgBox features: Title=Yes, Text=Yes, Buttons=Yes and No, Default Button=Second, Icon=None
    If Not IsDeclared("iMsgBoxAnswer") Then Local $iMsgBoxAnswer
    $iMsgBoxAnswer = MsgBox(260, "Name change", "Are you sure you want to change the name of this computer to...   " & @CRLF & @CRLF & GUICtrlRead($MachineName))
    Select
        Case $iMsgBoxAnswer = 6 ;Yes
            _RunDOS('start ' & @SystemDir & '\sysdm.cpl') ; opens My Computer properties
            WinWaitActive("System Properties", "Registered to:") ; wait for the window to open
            ControlClick("System Properties", "Registered to:", 12320, "left", 1, 150, 30) ; clicks to switch to the Computer Name tab
            ControlClick("System Properties", "Change...", 115, "left", 1, 35, 10) ; clicks the Change button
            ControlClick("Computer Name Changes", "Full computer name:", 1002, "left", 2) ; highlights text in the name field
            Send($MachineName)
        Case $iMsgBoxAnswer = 7 ;No
    EndSelect
    #EndRegion --- CodeWizard generated code Start ---

EndFunc   ;==>btnNameChangeGo


Func CLOSEClicked()
    Exit
EndFunc   ;==>CLOSEClicked

Func doExit()
    Exit
EndFunc   ;==>btnExit

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Line 56

Send($MachineName)
Send(GuiCtrlRead($MachineName))
Edited by dwerf

Share this post


Link to post
Share on other sites

Thanks dwerf.

So simple when it's pointed out to you.

As you can probably guess I am very new to AutoIT...

I found by searching the forum a snippet of code that had GUICtrlRead in the message box line allowing the variable to be used. Am I right in saying then, that the Select Case structure is classed as part of the MsgBox construct (in this situation), which is why I need the GUICtrlRead in the Send line?

I did spend a while reading the help files, searching the forum and the web but found nothing that explained this. I'm still not sure why! Can you (or anyone else) recommend a website or book maybe that would help a newcomer understand this type of stuff?

Thanks again for quickly pointing out my error.

Philip

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Am I right in saying then, that the Select Case structure is classed as part of the MsgBox construct (in this situation), which is why I need the GUICtrlRead in the Send line?

Select Case structure checks wich Button you have pressed.

All functions in AutoIt can return some values. As example GuiCtrlCreateButton returns the id of the created button. FileCopy returns 1 if a file could be copied or 0 if it was unable.

MsgBox returns the id of the button in the box, that was pressed. If you look after MsgBox in the help-file, you will find a table with ids.

Now to Select Case.

It does the same as If ... Then.

In your script the value, that MsgBox returns, is saved in the variable $iMsgBoxAnswer.

Select case checks if the value of $iMsgBoxAnswer is 6 (id of "Yes" button), then it does something, or the value is 7 (id of "No" button), then it could do something.

You can also write

$iMsgBoxAnswer = MsgBox(260, "Name change", "Are you sure you want to change the name of this computer to...   " & @CRLF & @CRLF & GUICtrlRead($MachineName))
If $iMsgBoxAnswer = 6 Then
    _RunDOS('start ' & @SystemDir & '\sysdm.cpl') ; opens My Computer properties
    WinWaitActive("System Properties", "Registered to:") ; wait for the window to open
    ControlClick("System Properties", "Registered to:", 12320, "left", 1, 150, 30) ; clicks to switch to the Computer Name tab
    ControlClick("System Properties", "Change...", 115, "left", 1, 35, 10) ; clicks the Change button
    ControlClick("Computer Name Changes", "Full computer name:", 1002, "left", 2) ; highlights text in the name field
    Send($MachineName)
ElseIf $iMsgBoxAnswer = 7 Then
EndIf

Or

If MsgBox(260, "Name change", "Are you sure you want to change the name of this computer to...   " & @CRLF & @CRLF & GUICtrlRead($MachineName)) = 6 Then
    _RunDOS('start ' & @SystemDir & '\sysdm.cpl') ; opens My Computer properties
    WinWaitActive("System Properties", "Registered to:") ; wait for the window to open
    ControlClick("System Properties", "Registered to:", 12320, "left", 1, 150, 30) ; clicks to switch to the Computer Name tab
    ControlClick("System Properties", "Change...", 115, "left", 1, 35, 10) ; clicks the Change button
    ControlClick("Computer Name Changes", "Full computer name:", 1002, "left", 2) ; highlights text in the name field
    Send($MachineName)
EndIf

I hope you can understand my english.

Edited by dwerf

Share this post


Link to post
Share on other sites

I get the fact that all functions return a value, but (in this case) can't work out where the 4 comes from. The Yes & No buttons are 6 & 7. Retry is 4 - not used here. I cannot find another item with a value of 4, so where is that value coming from?

And your English is great. What is your native language?

Philip

Share this post


Link to post
Share on other sites

The 4 come from variable $MachineName. It's the id of the input control, that was returned by the function GUICtrlCreateInput at begin of the script.

If you write Send($MachineName), this id will be send. If you write Send(GuiCtrlRead($MachineName)), GuiCtrlRead will first return the data, that it will read from the control with id from $MachineName. And function Send will "send" the returned value.

My native language is russian.

Share this post


Link to post
Share on other sites

Thanks for the clear explanation. I fully understand now.

Your English is amazing.

Regards

Philip

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