Tac0s

User input into Variable.

3 posts in this topic

Hi!

New to the forums, hopefully you can help.

I am trying to use a gui input as a variable for the rest of the script, but cannot seem to make it work.
We need to get the client to indicate his username, and using that input and declaring it as a variable we can then indicate the path to his userprofile (in order to install some files on their profile - We are running a software installation with admin rights and unfortunately some of the necessary files end up in the Admin account used to run the setup.exe).

So looking at my code below, could you perhaps help to clarify what could be wrong?
Thanks!

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>

Const $Username = GUICtrlRead ("")
Const $Profile = "C:\users"

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("title", 347, 317, 379, 128)
$Label1 = GUICtrlCreateLabel("Please Write your  Username:", 24, 136, 300, 65)
$Input1 = GUICtrlCreateInput("", 32, 216, 297, 21)
$Button1 = GUICtrlCreateButton("OK", 128, 248, 81, 33)
$Pic1 = GUICtrlCreatePic("", 8, 0, 332, 108)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
$nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
        Exit
        Case $Button1
        GUICtrlRead($Input1)
        If FileExists ($Profile & $Input1) Then
            MsgBox (0, "Info", "Username Exists" , 0, "")
        Else
            MsgBox (0, "Info", "Wrong Username", 0, "")
        EndIf
        If FileExists ($Profile & $Input1) Then
            ExitLoop
        EndIf
    EndSwitch
WEnd

 getlocalusernameAUTOIT.au3

Share this post


Link to post
Share on other sites



1/ You have to retrieve the control value with GUICtrlRead

$username = GUICtrlRead($Input1)

2/ The FileExists needs a "\" (or add a "\" to the $Profile value)

If FileExists ($Profile & "\" & $username) ...

3/ You can put the ExitLoop just after the MsgBox and then remove the second FileExits test

Here is a corrected version :

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>

Local $Username
Const $Profile = "C:\users"

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("title", 347, 317, 379, 128)
$Label1 = GUICtrlCreateLabel("Please Write your  Username:", 24, 136, 300, 65)
$Input1 = GUICtrlCreateInput("", 32, 216, 297, 21)
$Button1 = GUICtrlCreateButton("OK", 128, 248, 81, 33)
$Pic1 = GUICtrlCreatePic("", 8, 0, 332, 108)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
$nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
        Exit
        Case $Button1
            $username = GUICtrlRead($Input1)
            If FileExists ($Profile & "\" & $username) Then
                MsgBox (0, "Info", "Username Exists" , 0, "")
                ExitLoop
            Else
                MsgBox (0, "Info", "Wrong Username", 0, "")
            EndIf
    EndSwitch
WEnd

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Tac0s,

Welcome to the AutoIt forums.

Look at the changes I made:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>

Const $Username = GUICtrlRead ("")
Const $Profile = "C:\users\"                                ; Add a "\" here

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("title", 347, 317, 379, 128)
$Label1 = GUICtrlCreateLabel("Please Write your  Username:", 24, 136, 300, 65)
$Input1 = GUICtrlCreateInput("", 32, 216, 297, 21)
$Button1 = GUICtrlCreateButton("OK", 128, 248, 81, 33)
$Pic1 = GUICtrlCreatePic("", 8, 0, 332, 108)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
$nMsg = GUIGetMsg()
    Switch $nMsg

        Case $GUI_EVENT_CLOSE
        Exit
        Case $Button1
        $sUsername = GUICtrlRead($Input1)                   ; You need to put the content into a variable...
        If FileExists ($Profile & $sUsername) Then          ; ...which you then use here
            MsgBox (0, "Info", "Username Exists" , 0, "")
            ExitLoop                                        ; And you can put this here rather then ask the same queston again
        Else
            MsgBox (0, "Info", "Wrong Username", 0, "")
        EndIf



    EndSwitch

WEnd

Please ask if you have any questions.

M23

Edit: Getting slow in my old age!

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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