Jump to content
Sign in to follow this  
ontherocks

File Browser & other help

Recommended Posts

ontherocks

I am trying to create a simple GUI.

1) There will be drop down list which lists all the drives in the computer. The user selects one and that is read.

2) There is a text box beside a file browser button, where the user searches for a file. Once selected, the path is read and it is displayed in the text box.

3) Run a DOS command on clicking OK button using the read variables from step #1 & step #2.

I tried my best starting playing with autoit since yesterday for the first time. Pls help.

Also please let me know if any of the lines/words/numbers(options) are redundant so that I can get rid of it and make the file smaller.

#include <GUIConstantsEx.au3>

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

GUICreate("Syslinux GUI", 370, 160) 
GUICtrlCreateLabel("This will Run your code", 40, 10) 
GUICtrlCreateLabel("Select the drive", 140, 42) 

GUISetState ()

GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")

GuiCtrlCreateInput("Put the exe here", 40, 80, 220, 20) 

Func BROWSEButton()
    FileOpenDialog("Browse the exe", "C:\" , "(*.exe)")
EndFunc

$browsebutton = GUICtrlCreateButton("Browse", 270, 80, 70, 20)
GUICtrlSetOnEvent($browsebutton, "BROWSEButton")

$drives = DriveGetDrive ( "all" )


$n2 = GUICtrlCreateCombo("", 60, 40) 

For $i = 1 to $drives[0]
        GUICtrlSetData(-1, $drives[$i])
    Next
    
$okbutton = GUICtrlCreateButton("OK", 100,  120, 70, 20) 
GUICtrlSetOnEvent($okbutton, "OKButton")

$exitbutton = GUICtrlCreateButton("EXIT", 200, 120, 70, 20) 
GUICtrlSetOnEvent($exitbutton, "EXITButton")


While 1
 Sleep(1000)
WEnd

Func OKButton()
  Run ("cmd.exe")
EndFunc

Func EXITButton()
    Exit
EndFunc

Func CLOSEClicked()
    Exit
EndFunc

Share this post


Link to post
Share on other sites
martin

I am trying to create a simple GUI.

1) There will be drop down list which lists all the drives in the computer. The user selects one and that is read.

2) There is a text box beside a file browser button, where the user searches for a file. Once selected, the path is read and it is displayed in the text box.

3) Run a DOS command on clicking OK button using the read variables from step #1 & step #2.

I tried my best starting playing with autoit since yesterday for the first time. Pls help.

Also please let me know if any of the lines/words/numbers(options) are redundant so that I can get rid of it and make the file smaller.

#include <GUIConstantsEx.au3>

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

GUICreate("Syslinux GUI", 370, 160) 
GUICtrlCreateLabel("This will Run your code", 40, 10) 
GUICtrlCreateLabel("Select the drive", 140, 42) 

GUISetState ()

GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")

GuiCtrlCreateInput("Put the exe here", 40, 80, 220, 20) 

Func BROWSEButton()
    FileOpenDialog("Browse the exe", "C:\" , "(*.exe)")
EndFunc

$browsebutton = GUICtrlCreateButton("Browse", 270, 80, 70, 20)
GUICtrlSetOnEvent($browsebutton, "BROWSEButton")

$drives = DriveGetDrive ( "all" )


$n2 = GUICtrlCreateCombo("", 60, 40) 

For $i = 1 to $drives[0]
        GUICtrlSetData(-1, $drives[$i])
    Next
    
$okbutton = GUICtrlCreateButton("OK", 100,  120, 70, 20) 
GUICtrlSetOnEvent($okbutton, "OKButton")

$exitbutton = GUICtrlCreateButton("EXIT", 200, 120, 70, 20) 
GUICtrlSetOnEvent($exitbutton, "EXITButton")


While 1
 Sleep(1000)
WEnd

Func OKButton()
  Run ("cmd.exe")
EndFunc

Func EXITButton()
    Exit
EndFunc

Func CLOSEClicked()
    Exit
EndFunc
Welcome to the AutoIt forums. That's a good first attempt :) If you only started yesterday you'll soon overtake me.

Just a few small changes below, but I'm not sure what you want to happen when the OK button is pressed.

#include <GUIConstantsEx.au3>

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

GUICreate("Syslinux GUI", 370, 160)
GUICtrlCreateLabel("This will Run your code", 40, 10)
GUICtrlCreateLabel("Select the drive", 140, 42)

GUISetState()

GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")

$IpFileName = GUICtrlCreateInput("Put the exe here", 40, 80, 220, 20)



$browsebutton = GUICtrlCreateButton("Browse", 270, 80, 70, 20)
GUICtrlSetOnEvent($browsebutton, "BROWSEButton")

$drives = DriveGetDrive("all")


$n2 = GUICtrlCreateCombo("", 60, 40)
$Alldrives = ''
For $i = 1 To $drives[0]
    $Alldrives &= $drives[$i] & '|'
Next
GUICtrlSetData(-1, $Alldrives, $drives[1])
$okbutton = GUICtrlCreateButton("OK", 100, 120, 70, 20)
GUICtrlSetOnEvent($okbutton, "OKButton")

$exitbutton = GUICtrlCreateButton("EXIT", 200, 120, 70, 20)
GUICtrlSetOnEvent($exitbutton, "EXITButton")


While 1
    Sleep(1000)
WEnd

Func BROWSEButton()
    Local $selFile
    $selFile = FileOpenDialog("Browse the exe", "C:\", "(*.exe)")
    If Not @error Then
        GUICtrlSetData($IpFileName, $selFile)
    EndIf
    
    
EndFunc  ;==>BROWSEButton

Func OKButton()
    Run("cmd.exe")
EndFunc  ;==>OKButton

Func EXITButton()
    Exit
EndFunc  ;==>EXITButton

Func CLOSEClicked()
    Exit
EndFunc  ;==>CLOSEClicked
Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
ontherocks

Thats great buddy :) Thanks a lot.

I cannot figure out the syntax of the following:-

1)

$Alldrives = ''
For $i = 1 To $drives[0]
    $Alldrives &= $drives[$i] & '|'
Next

What is $Alldrives = ''? Is it a string variable initialized to zero/null?

What does &= mean?

What does & '|' mean?

2)

GUICtrlSetData(-1, $Alldrives, $drives[1])

How do you put two variables as parameters? How does it read two variables?

How do you print out a read variable? Say, I want to see/print out what is stored in $Alldrives, $drives[1], how do I do that? Through message box?

In other words I want to know the equivalent of "printf" statement in C language.

Share this post


Link to post
Share on other sites
nobbe

What is $Alldrives = ''? Is it a string variable initialized to zero/null?

yes, something like that

What does &= mean?

adds string to previously defined string

What does & '|' mean?

adds the "|" character (pipe symbol of often used for separation of strings)

GUICtrlSetData(-1, $Alldrives, $drives[1])

see help file on "GUICtrlSetData"

How do you print out a read variable? Say, I want to see/print out what is stored in $Alldrives, $drives[1], how do I do that? Through message box?

you can use edit field , or label, or message boxes , or something like that (there is a viriety of gui controls)

In other words I want to know the equivalent of "printf" statement in C language.

this cant be compared, a printf would be something like a "consolewrite" ,but then it wouldnt show in the compiled exe because there is no console (only in scite window)

so you need to set your data with "GUICtrlSetData" to a ontrol of your choice e.g. an edit field

$edit_log = GUICtrlCreateEdit("", 8, 524, 485, 117)

    _DebugPrint($edit_log, "hello world");

;
; print log into edit control
;
;
;
Func _DebugPrint($id, $text)
    GUICtrlSetData($id, GUICtrlRead($id) & @HOUR & ":" & @MIN & ":" & @SEC & " " & $text & @CRLF)
    _GUICtrlEdit_LineScroll($id, 0, _GUICtrlEdit_GetLineCount($id))
EndFunc   ;==>_DebugPrint
Edited by nobbe

Share this post


Link to post
Share on other sites
ontherocks

Thanks a lot nobbe.

From the following I understand that $n2 gives the read value/string of the drive thats selected by the user.

$n2 = GUICtrlCreateCombo("", 60, 40)
$Alldrives = ''
For $i = 1 To $drives[0]
    $Alldrives &= $drives[$i] & '|'
Next
GUICtrlSetData(-1, $Alldrives, $drives[1])

But when I create a messagebox or GUICtrlCreateLabel($n2, 300, 42), it gives a value 6 instead of strings like c: or d:

Why is this so?

Can I add a text to a variable output? Like for example I want to add the text "Disk" before the hard disk labels in the combo box so that they appear as "Disk c:", or "Disk d:" instead of "c:" or "d:".

Edited by ontherocks

Share this post


Link to post
Share on other sites
rasim

ontherocks

I understand that $n2 gives the read value/string of the drive thats selected by the user

No. It`s returns the identifier (controlID) of the new control.

Can I add a text to a variable output?

"Disk: " & $var

Share this post


Link to post
Share on other sites
ontherocks

Thanks rasim.

The controlID fundamentals are still not clear to me.

GUICtrlSetData needs a controlID as returned by GUICtrlCreateCombo. In the code below it is mentioned as "-1". But the controlID returned by GUICtrlCreateCombo is "6" Where did "-1" come from?

GUICtrlSetData(-1, $drives[$i])

In the following, "$selFile" stores/returns the value of the path of selected file. It is mentioned as "local", which means that it has a value only inside the Function (not outside it). I want the value outside the function, so that I can use it in like "Run ("$selFile")". I tried removing "Local $selFile" and declaring "$selFile" as global at the beginning of the program, but there was no change. What should I do?

Func BROWSEButton()
    Local $selFile
    $selFile = FileOpenDialog("Browse the exe", "C:\", "(*.exe)")
    If Not @error Then
        GUICtrlSetData($IpFileName, $selFile)
    EndIf

I also want a variable to return the value of the drive the user chooses. How do I do that?

Edited by ontherocks

Share this post


Link to post
Share on other sites
nobbe

i changed it

#include <GUIConstantsEx.au3>

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

GUICreate("Syslinux GUI", 370, 160)
GUICtrlCreateLabel("This will Run your code", 40, 10)
GUICtrlCreateLabel("Select the drive", 140, 42)

GUISetState()

GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")

$IpFileName = GUICtrlCreateInput("Put the exe here", 40, 80, 220, 20)

$browsebutton = GUICtrlCreateButton("Browse", 270, 80, 70, 20)
GUICtrlSetOnEvent($browsebutton, "BROWSEButton")


global $global_exe =""; 

$drives = DriveGetDrive("all")


$n2 = GUICtrlCreateCombo("", 60, 40)
$Alldrives = ''
For $i = 1 To $drives[0]
    $Alldrives &= $drives[$i] & '|'
Next
GUICtrlSetData(-1, $Alldrives, $drives[1])
$okbutton = GUICtrlCreateButton("OK", 100, 120, 70, 20)
GUICtrlSetOnEvent($okbutton, "OKButton")

$exitbutton = GUICtrlCreateButton("EXIT", 200, 120, 70, 20)
GUICtrlSetOnEvent($exitbutton, "EXITButton")


While 1
    Sleep(1000)
WEnd


Func BROWSEButton()
    local $my_drive = GUICtrlRead($n2)
    MsgBox(0,"",$my_drive);

    
    Local $selFile
    $selFile = FileOpenDialog("Browse the exe",  $my_drive, "(*.exe)")
    If Not @error Then
        GUICtrlSetData($IpFileName, $selFile)
        $global_exe = $selFile
    EndIf
  

EndFunc  ;==>BROWSEButton



Func OKButton()
    
    MsgBox(0,"",$global_exe);
    
    ;Run("cmd.exe")
EndFunc  ;==>OKButton

Func EXITButton()
    Exit
EndFunc  ;==>EXITButton

Func CLOSEClicked()
    Exit
EndFunc  ;==>CLOSEClicked

Share this post


Link to post
Share on other sites
rasim

ontherocks

GUICtrlSetData needs a controlID as returned by GUICtrlCreateCombo. In the code below it is mentioned as "-1". But the controlID returned by GUICtrlCreateCombo is "6" Where did "-1" come from?

-1 it`s the last control identifier.

Share this post


Link to post
Share on other sites
ontherocks

Fantastic nobbe :)

I have a question

If I use something like the following it doesn't work. I have to put local $my_drive = GUICtrlRead($n2) inside the Okbutton function to make it work. What is wrong with the code below?

$drives = DriveGetDrive("all")

$n2 = GUICtrlCreateCombo("", 60, 40)
$Alldrives = ''
For $i = 1 To $drives[0]
    $Alldrives &= $drives[$i] & '|'
Next
GUICtrlSetData(-1, $Alldrives, $drives[1])
$my_drive = GUICtrlRead($n2)
Func OKButton()
    MsgBox(0,"",$my_drive);
EndFunc;==>OKButton

I also noticed that the following works, though I am not sure if its the proper way to do

Func BROWSEButton()
    global $selFile
    $selFile = FileOpenDialog("Browse the exe",  $my_drive, "(*.exe)")
    If Not @error Then
        GUICtrlSetData($IpFileName, $selFile)
    EndIf
EndFunc;==>BROWSEButton

Func OKButton()
      MsgBox(0,"",$selFile);
EndFunc;==>OKButton

@rasim

I still don't get the controlID funda. Is there somewhere I can read more about it. The help file doesn't say much.

Edited by ontherocks

Share this post


Link to post
Share on other sites
nobbe

i am not really a friend of the "eventmode" approach for beginners - i consider it for more advanced programmers only.

the message looping is more logical and easier to understand for beginners

anyhow you should consider using "consolewrite" to write all variables out to scite console for debugging at runtime - this will get you more information about local / global variables

also a good idea is to prefix "glob_" or "local_" to variables for easier understanding

"local" are only valid within a function so "$myvar" in the following are 2 different ones !!

func 1()
begin 

local $myvar =1 

endfunc

; ---- 
func 2()
begin 

local $myvar = "test"

endfunc

I have a question

If I use something like the following it doesn't work. I have to put local $my_drive = GUICtrlRead($n2) inside the Okbutton function to make it work. What is wrong with the code below?

ok i try to explain:

on SETUP you fill the combo box with drive letter, then read the "letter" from combobox right after that -- so the user had no time to click and select it! i assume now the $my_drive = GUICtrlRead($n2) is set to the last value or to void since no selection !

in the function

Func OKButton()

MsgBox(0,"",$my_drive);

EndFunc;==>OKButton

the user has NOT selected the value !! ergo you need to put

$my_drive = GUICtrlRead($n2) inside the function so that the user had time to select it - then ok -> then value is readable

Edited by nobbe

Share this post


Link to post
Share on other sites
ontherocks

I think I get a little what you are saying. Will need some more time to understand that :)

I tried to create an error messagebox if the user doesn't select a file from the browse button and presses OK button. Upon pressing OK in messagebox, it should go away and give back the main GUI so that user can select the file.

In the code below, after I click OK in messagebox the next statement runs, i.e. pops up the $my_drive message box. I am used to goto statements and so was thinking of putting a "If $n4 =0 Then..............else goto Func BROWSEButton()" kind of thing. What to do?

..............
Func BROWSEButton()

$selFile = FileOpenDialog("Browse the exe", "C:\", "(*.exe)")
$n4 = GUICtrlSetData($IpFileName, $selFile)

EndFunc

$okbutton = GUICtrlCreateButton("OK", 100, 120, 70, 20)
GUICtrlSetOnEvent($okbutton, "OKButton")

$exitbutton = GUICtrlCreateButton("EXIT", 200, 120, 70, 20)
GUICtrlSetOnEvent($exitbutton, "EXITButton")

While 1
    Sleep(1000)
WEnd

Func OKButton()
If $n4 =0 Then
    MsgBox(53,"Error", "Select Syslinux.exe")
EndIf
    
    Local $my_drive = GUICtrlRead($n2)
MsgBox(0,"",$my_drive);
EndFunc
..............
Edited by ontherocks

Share this post


Link to post
Share on other sites
nobbe

i absolutely dont know what you are trying to do ?? :)

Share this post


Link to post
Share on other sites
ontherocks

Let me explain again.

In the GUI there is a FileOpenDialog. The user has to select a file and this file path gets entered in GUICtrlCreateInput box. This path is read by GUICtrlSetData.

There is the also the OKButton. When the user clicks the OKButton, the path which was read by GUICtrlSetData is run in DOS.

Case 1) What if the user doesn't click the Browsebutton in FileOpenDialog and clicks the OKButton (i.e. he doesn't select the exe file)?

In this case I want to output a messagebox giving the user a warning to select the exe file first.

I tried to do it this way but got stuck. Its not giving the user a chance to select the file if he clicks OK without selecting the exe file in FileOpenDialog.

#include <GUIConstantsEx.au3>
Global $n4, $select_file, $my_drive
Opt("GUIOnEventMode", 1)

GUICreate("Syslinux GUI", 370, 160)
GUICtrlCreateLabel("This will install Syslinux to MBR of drive, proceed very carefully!!", 40, 10)
GUICtrlCreateLabel("Select the drive, be very careful!!", 140, 42)

GUISetState()

GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")

$browsebutton = GUICtrlCreateButton("Browse", 270, 80, 70, 20)
GUICtrlSetOnEvent($browsebutton, "BROWSEButton")

$drives = DriveGetDrive("all")

$select_drive = GUICtrlCreateCombo("", 60, 40)

$all_drives = ""
For $i = 1 To $drives[0]
    $all_drives &= $drives[$i] & '|'
Next
GUICtrlSetData(-1, $all_drives, $drives[1])


$input_file = GUICtrlCreateInput("Put Syslinux.exe here", 40, 80, 220, 20)

Func BROWSEButton()
 $select_file = FileOpenDialog("Browse Syslinux.exe", "C:\", "(*.exe)")

$n4 = GUICtrlSetData($input_file, $select_file)

EndFunc

$okbutton = GUICtrlCreateButton("OK", 100, 120, 70, 20)
GUICtrlSetOnEvent($okbutton, "OKButton")

$exitbutton = GUICtrlCreateButton("EXIT", 200, 120, 70, 20)
GUICtrlSetOnEvent($exitbutton, "EXITButton")

While 1
    Sleep(1000)
WEnd

Func OKButton()
If $n4 =0 Then
            MsgBox(48,"Error", "Select Syslinux.exe first")
        EndIf
    
    $my_drive = GUICtrlRead($select_drive)
        $msgbox_button_pressed = MsgBox(36,"Warning!!", "Are you sure you want to install Syslinux to the MBR of " & $my_drive & " drive?")
    
    If $msgbox_button_pressed = 6 Then
      Run($select_file)
    EndIf
    
EndFunc ;==>OKButton

Func EXITButton()
    Exit
EndFunc ;==>EXITButton

Func CLOSEClicked()
    Exit
EndFunc ;==>CLOSEClicked

Case 2) The user clicks the browse button in FileOpenDialog but doesn't select any exe file and clicks the cancel button. In this case too I plan to give a warning message box to select the exe file first.

Share this post


Link to post
Share on other sites
rasim

ontherocks

Example:

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

Global $n4, $select_file, $my_drive
Dim $all_drives

$hGUI = GUICreate("Syslinux GUI", 370, 160)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")

GUICtrlCreateLabel("This will install Syslinux to MBR of drive, proceed very carefully!!", 40, 10)
GUICtrlCreateLabel("Select the drive, be very careful!!", 140, 42)

$browsebutton = GUICtrlCreateButton("Browse", 270, 80, 70, 20)
GUICtrlSetOnEvent($browsebutton, "BROWSEButton")

$select_drive = GUICtrlCreateCombo("", 60, 40)

$input_file = GUICtrlCreateInput("Put Syslinux.exe here", 40, 80, 220, 20)

$okbutton = GUICtrlCreateButton("OK", 100, 120, 70, 20)
GUICtrlSetOnEvent($okbutton, "OKButton")

$exitbutton = GUICtrlCreateButton("EXIT", 200, 120, 70, 20)
GUICtrlSetOnEvent($exitbutton, "CLOSEClicked")

GUISetState()

$drives = DriveGetDrive("all")

For $i = 1 To $drives[0]
    $all_drives &= $drives[$i] & '|'
Next

GUICtrlSetData($select_drive, $all_drives, $drives[1])

While 1
    Sleep(1000)
WEnd

Func BROWSEButton()
    GUICtrlSetData($input_file, "")
    $select_file = FileOpenDialog("Browse Syslinux.exe", "C:\", "(*.exe)")
    If $select_file <> "Syslinux.exe" Then Return False
    GUICtrlSetData($input_file, $select_file)
EndFunc

Func OKButton()
    $ReadInput = GUICtrlRead($input_file)
    If ($ReadInput = "") Or (FileExists($select_file) = 0) Then
        MsgBox(48, "Error", "Select Syslinux.exe first", 0, $hGUI)
        Return False
    EndIf
    
    $my_drive = GUICtrlRead($select_drive)
    $msgbox_button_pressed = MsgBox(36,"Warning!!", "Are you sure you want to install Syslinux to the MBR of " & $my_drive & " drive?")
    
    If $msgbox_button_pressed = 6 Then
        Run($select_file)
    EndIf
EndFunc ;==>OKButton

Func CLOSEClicked()
    Exit
EndFunc ;==>CLOSEClicked

Share this post


Link to post
Share on other sites
ontherocks

Almost done. Thanks rasim :)

Questions:

1) You have written "If $select_file <> "Syslinux.exe" Then Return False".

But, $select_file is the complete path to Syslinux.exe for example "C:\Syslinux\Syslinux.exe". Since the path may vary, how do I put that in $select_file. Something like If $select_file <> "*" & "Syslinux.exe" Then Return False

2) What does "Return False" do in msgbox function?

3) I was thinking of popping up a message box giving a warning "are you sure you want to use this drive" as soon as the user clicks and selects the drive from the GUICtrlCreateCombo box. How do I do that?

Share this post


Link to post
Share on other sites
rasim

ontherocks

1)

If StringRegExpReplace($select_file, "^.*\\", "") <> "Syslinux.exe" Then Return False

2)

Use the Return keyword to exit the function. Unlike built-in functions, user-defined functions return 0 unless another return value is specified.

3)

$select_drive = GUICtrlCreateCombo("", 60, 40)
GUICtrlSetOnEvent(-1, "_DriveCheck")

Func _DriveCheck()
    Local $iDrive = GUICtrlRead($select_drive)
    If MsgBox(4 + 32, "Select Drive", 'Are you sure you want to use a "' & $iDrive & '" drive', 0, $hGUI) = 6 Then $my_drive = $iDrive
EndFunc
Edited by rasim

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  

×