Sign in to follow this  
Followers 0
AlexChernavsky

Problem with selecting a file through FileOpenDialog

4 posts in this topic

I'm having a strange issue with using FileOpenDialog to select a file. Everything works fine the first time you select a file. But if you change your mind (while the program is still running) and want to select a different file, nothing changes. The program is "stuck" with the first selection you made, and nothing you do will change that selection in the input box.

I pulled the function below out of my program, and I had to modify it a bit to make it run on its own. But it's basically the same as the original.

To see the problem in action, click on either of the two buttons that say, "Choose other file...". Select any file from the list. Click on the "Open" button. So far, so good. Now, repeat the process again. See? The original file you selected is still listed in the input box.

I'm stumped by this one.

Thanks in advance.

-- Alex Chernavsky in Rochester, NY

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

_ConfirmParametersWithUser("test.txt", "test2.txt", 16, 1000, 3000, "InputFile", "WriteToFile", "Testing")

Func _ConfirmParametersWithUser($sInputFileName, $sOutputFileName, $iNumberOfProblems, $iMinTime, $iMaxTime, $sSourceOfProblems, $sOutputType, $sCondition)
Local $hSetParametersForm ; Handle to GUI
Local $hRadio1
Local $hRadio2
Local $hRadio3
Local $hRadio4
Local $hInputFileName
Local $hChooseInputFile
Local $hOutputFileName
Local $hChooseOutputFile
Local $hTrialsPerSession
Local $hTime1
Local $hTime2
Local $hCondition
Local $hExitButton
Local $hHelpButton
Local $hContinueButton
Local $nMsg
Local $sTemp

$sInputFileName = @ScriptDir & "\" & $sInputFileName
$sOutputFileName = @ScriptDir & "\" & $sOutputFileName
$hSetParametersForm = GUICreate("Reaction Time Program: Set Parameters", 580, 578, -1, -1)
GUICtrlCreateLabel("Reaction Time Program", 175, 3, 205, 23)
GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif")

GUICtrlCreateLabel("Based on work by Seth Roberts", 200, 29, 155, 15)
GUICtrlSetFont(-1, 8, 400, 0, "MS Sans Serif")
;GUICtrlSetBkColor(-1, 0xCCFFCC) ; Give it a green background to make it easier to position it (it's a temporary thing to help design the GUI)

GUICtrlCreateGroup("Source of arithmetic problems: ", 40, 50, 528, 105)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
GUIStartGroup()
$hRadio1 = GUICtrlCreateRadio("Use problems included with source code", 64, 80, 257, 17)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
If $sSourceOfProblems = "SourceCode" Then
GUICtrlSetState(-1, $GUI_CHECKED)
EndIf
$hRadio2 = GUICtrlCreateRadio("Read problems from input file: ", 64, 104, 280, 17)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
If $sSourceOfProblems = "InputFile" Then
GUICtrlSetState(-1, $GUI_CHECKED)
EndIf
$hInputFileName = GUICtrlCreateInput($sInputFileName & " ", 81, 124, 380, 20)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
GUICtrlSetState($hInputFileName, $GUI_FOCUS)
Send("{end}") ; This line and the one before it are necessary to right-align the text (useful in case of long path names, so that the left part gets cut off, not the right part)
$hChooseInputFile = GUICtrlCreateButton("Choose other file...", 465, 121)
If $sSourceOfProblems = "InputFile" Then
GUICtrlSetState($hChooseInputFile, $GUI_ENABLE)
Else
GUICtrlSetState($hChooseInputFile, $GUI_DISABLE)
EndIf

GUICtrlCreateLabel("Number of trials (problems) presented per testing session:", 48, 167, 345, 20)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$hTrialsPerSession = GUICtrlCreateInput($iNumberOfProblems, 397, 165, 30, 22, BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER))
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")

GUICtrlCreateGroup("Random delay between trials (in milliseconds): ", 40, 198, 349, 89)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")

GUICtrlCreateLabel("Minimum delay:", 64, 224, 97, 20)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
GUICtrlCreateLabel("Maximum delay :", 64, 256, 104, 20)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$hTime1 = GUICtrlCreateInput($iMinTime, 176, 224, 41, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER))
$hTime2 = GUICtrlCreateInput($iMaxTime, 176, 256, 41, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER))

GUICtrlCreateGroup("Upon completion of the testing session: ", 40, 296, 528, 110)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$hRadio3 = GUICtrlCreateRadio("Display summary and append data to output file: ", 64, 325, 422, 17)
If $sOutputType = "WriteToFile" Then GUICtrlSetState(-1, $GUI_CHECKED)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$hOutputFileName = GUICtrlCreateInput($sOutputFileName & " ", 81, 350, 380, 20)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
GUICtrlSetState($hOutputFileName, $GUI_FOCUS)
Send("{end}") ; This line and the one before it are necessary to right-align the text (useful in case of long path names, so that the left part gets cut off, not the right part)
$hChooseOutputFile = GUICtrlCreateButton("Choose other file...", 465, 347)
If $sOutputType = "WriteToFile" Then
GUICtrlSetState($hChooseOutputFile, $GUI_ENABLE)
Else
GUICtrlSetState($hChooseOutputFile, $GUI_DISABLE)
EndIf
$hRadio4 = GUICtrlCreateRadio("Display summary but do not write data to output file", 64, 379, 320, 17)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
If $sOutputType = "DisplayOnly" Then GUICtrlSetState(-1, $GUI_CHECKED)
GUICtrlCreateGroup("Please describe the condition for this test:", 40, 417, 520, 90)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$hCondition = GUICtrlCreateInput("", 54, 447, 500, 20)
GUICtrlCreateLabel("Example: ""45 mins. after taking 3 tablespoons of flaxseed oil""", 56, 477, 550, 28)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")

$hHelpButton = GUICtrlCreateButton("Help", 272, 534, 75, 25)
$hContinueButton = GUICtrlCreateButton("Continue", 382, 534, 75, 25, $BS_DEFPUSHBUTTON)
$hExitButton = GUICtrlCreateButton("Exit Program", 488, 534, 75, 25)
GUICtrlSetState($hCondition, $GUI_FOCUS) ; Set the focus to the "Condition" input box
GUISetState(@SW_SHOW)

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $hRadio1
GUICtrlSetState($hChooseInputFile, $GUI_DISABLE)
Case $hRadio2
GUICtrlSetState($hChooseInputFile, $GUI_ENABLE)
Case $hRadio3
GUICtrlSetState($hChooseOutputFile, $GUI_ENABLE)
Case $hRadio4
GUICtrlSetState($hChooseOutputFile, $GUI_DISABLE)
Case $hChooseInputFile
$sTemp = FileOpenDialog("Choose input file", @ScriptDir & "\", "All files (*.*)", 1 + 2)
If $sTemp Then
$sInputFileName = $sTemp
$hInputFileName = GUICtrlSetData($hInputFileName, $sInputFileName & " ")
EndIf
GUICtrlSetState($hCondition, $GUI_FOCUS) ; Return focus to the "Continue" button
Case $hChooseOutputFile
$sTemp = FileOpenDialog("Choose output file", @ScriptDir & "\", "All files (*.*)", 2 + 8)
If $sTemp Then
$sOutputFileName = $sTemp
$hOutputFileName = GUICtrlSetData($hOutputFileName, $sOutputFileName & " ")
EndIf
GUICtrlSetState($hCondition, $GUI_FOCUS) ; Return focus to the "Continue" button
Case $hHelpButton
ShellExecute("http://www.astrocyte-design.com/reaction-time/index.html")
Case $hContinueButton
ExitLoop
Case $hExitButton
Exit
EndSwitch
WEnd
If (BitAND(GUICtrlRead($hRadio1), $GUI_CHECKED) = $GUI_CHECKED) Then
$sSourceOfProblems = "SourceCode"
Else
$sSourceOfProblems = "InputFile"
EndIf
$iNumberOfProblems = GUICtrlRead($hTrialsPerSession)
$iMinTime = GUICtrlRead($hTime1)
$iMaxTime = GUICtrlRead($hTime2)
If (BitAND(GUICtrlRead($hRadio3), $GUI_CHECKED) = $GUI_CHECKED) Then
$sOutputType = "WriteToFile"
Else
$sOutputType = "DisplayOnly"
EndIf
$sCondition = GUICtrlRead($hCondition)
$sCondition = StringReplace($sCondition, ",", "") ; Remove any commas that the user happens to enter (they interfere with the CSV file format)
GUIDelete($hSetParametersForm)
EndFunc ;==>_ConfirmParametersWithUser

Share this post


Link to post
Share on other sites

The problem is due to you redefining your variable. In this section:

Case $hChooseInputFile
$sTemp = FileOpenDialog("Choose input file", @ScriptDir & "", "All files (*.*)", 1 + 2)
If $sTemp Then
$sInputFileName = $sTemp
$hInputFileName = GUICtrlSetData($hInputFileName, $sInputFileName & " ")
EndIf
GUICtrlSetState($hCondition, $GUI_FOCUS) ; Return focus to the "Continue" button

you don't need to say $hInputFileName = GUICtrlSetData($hInputFileName, $sInputFileName & " "). Instead, just set the data:

Case $hChooseInputFile
$sTemp = FileOpenDialog("Choose input file", @ScriptDir & "", "All files (*.*)", 1 + 2)
 If $sTemp Then
  $sInputFileName = $sTemp
  GUICtrlSetData($hInputFileName, $sInputFileName & " ")
 EndIf
GUICtrlSetState($hCondition, $GUI_FOCUS) ; Return focus to the "Continue" button
1 person likes this

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

Thank you, JLogan3o13. I think that did the trick. I appreciate your help.

Share this post


Link to post
Share on other sites

Glad to be of service :)


√-1 2^3 ∑ π, and it was delicious!

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

  • Similar Content

    • Gringo
      By Gringo
      Hi,
      I'm trying to:
      -Select a file in a folder (to store it to an ini file)
      -Write the file on an ini
      -Copy files to the folder selected by the user
      instead of using FileOpenDialog then FileSelectFolder, I was wondering if it was possible to do the whole thing only with FileOpenDialog spliting the value returned in 2 variables. I got something like that for the first part (select a file and store it to an ini file)
      Local $message = "Select your executable" Local $pathk = FileOpenDialog($message, "C:" & "", "Select the executable you want to terminate (*.exe)", 1 + 4) Local $path = "None" ;ici je dois copier les fichiers $split = StringSplit($pathk, "\") $tokill = $split[$split[0]] If @error Then MsgBox(4096, "", "No Executable chosen") Else MsgBox(4096, "", $pathk & " Will be terminated " & @LF & @LF & "Press OK to EXIT ") IniWrite(@ScriptDir & "\path.ini", "Torun", "path", $path) IniWrite(@ScriptDir & "\path.ini", "Tokill", "pathk", $tokill) EndIf As you can see I manage to split the value returned by FileOpenDialog to have only the exe but as a noob I can't manage to get the path to copy the files I need to the same path.
      Any idea? ^^
    • computergroove
      By computergroove
      I've made a form in Koda where I make a list of 3 user definable elements that I want to be able to replicate with a button press so a user can add data that can be used as part of the program. 
      #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #Region ### START Koda GUI section ### Form=D:\Carbonite Important\0 - Programing\Scite and Autoit\Koda 1.7.3.0\Forms\Mileage Estimator.kxf $Form1 = GUICreate("Save Project", 614, 1126, 298, 141) $Location = GUICtrlCreateInput("Location", 40, 112, 345, 21) $Distance = GUICtrlCreateInput("Distance", 400, 112, 89, 21) $AddLocation = GUICtrlCreateButton("AddLocation", 288, 80, 97, 25) $Label1 = GUICtrlCreateLabel("Location", 40, 88, 45, 17) $Label2 = GUICtrlCreateLabel("Distance", 400, 88, 46, 17) $HomeBase = GUICtrlCreateInput("HomeBase", 312, 32, 121, 21) $LeapYear = GUICtrlCreateCheckbox("LeapYear", 464, 32, 17, 17) $Label3 = GUICtrlCreateLabel("Leap Year?", 488, 32, 59, 17) $Variance = GUICtrlCreateInput("Variance", 504, 112, 81, 21) $Label4 = GUICtrlCreateLabel("Variance", 504, 88, 46, 17) $Label5 = GUICtrlCreateLabel("Home Base", 312, 8, 59, 17) $Button1 = GUICtrlCreateButton("Button1", 144, 56, 1, 57) $Save = GUICtrlCreateButton("Save Project", 40, 16, 121, 25) $Load = GUICtrlCreateButton("Load Project", 176, 16, 121, 25) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Form1 Case $AddLocation EndSwitch WEnd I want to press the "add location" button and have the fields {Location, Distance and variance} create a new blank record located under the existing 3 input boxes. Since the fields can number over 30 locations in some examples I am thinking about having these items in a scrolling window. What is the best way to do this and can auto it add a variable while running by pressing a button?
    • XaelloNegative
      By XaelloNegative
      Hi,
      I made a small blunder with my code. I created the GUI under 1920x1080 resolution however, my user is configured to ONLY 1080x720 and cannot go any further than that.
      What do you guys think is the best way to correct this one? Resolution mathematics is something that came to mind however I do not have access to his PC to check  from time to time if  my estimation is correct and the resolution on my PC doesn't go down to 1080x720.
      Please help.
      ~XN~
    • davidacrozier
      By davidacrozier
      Hello all ~
      I am running an autoit script on Windows 10 inside VMware Workstation 12 Pro version 12.5.2.  Technically I am remoting into ESXi which has a Domain Controller (DC), WebServer, FilServer, Windows 10, etc.  Using the GUI (i.e. running explorer.exe) I am able to open several different folders successfully.  The desktop, documents, USB external all open without issue.  The network share opening gives me issues.  Whenever I attempt to open \\filserver\users\user\sharedfolder I get the documents folder instead.  I understand that the documents folder is the default for explorer.  I have also attempted to use the letter drive mapped to the network share (Z:) and receive the same result.  When I run this script on Windows 10 alone without  the VM or the ESXi I am able to open the network share without problems.  I have tried to use the net use command to designate a letter M: to the network share folder prior to running the script.  This did not work for me.  
      One additional avenue I think might work is to use the systreeview321 and _GUICtrlTreeView_FindItem to step through the tree looking for the network share.  Once found,  double click on it and see if that opens the shared network folder.  I can click inside the VM with my mouse on the network share and it opens just fine.  Not sure if running up against GUI issues, or permission issues, or what?
      Thanks in advance,
      Davida Crozier
      TestNetworkShare.au3
      This script is a subset of a much larger program, but it illustrates what I am dealing with.

    • superflq
      By superflq
      Hi everyone,
      GUICtrlSetState Function example ,i found $GUI_DROPACCEPTED isn't working in windows10 x64(windows_10_enterprise_2016_ltsb_x64).
      if the example put to windows10 x86 or windows xp, the $GUI_DROPACCEPTED is effective,
      the AutoIt Edition is v3.3.14.2.
      any ideas? thanks you.
       
      #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> Example() Func Example() ; Create a GUI with various controls. Local $hGUI = GUICreate("Example", 420, 200, -1, -1, -1, $WS_EX_ACCEPTFILES) ; Create a label and set the state as drop accepted. Local $idLabel = GUICtrlCreateLabel("Drop a file on this label.", 10, 10, 400, 40, $WS_BORDER) GUICtrlSetState($idLabel, $GUI_DROPACCEPTED) ; Create an input and set the state as drop accepted. Local $idInput = GUICtrlCreateInput("", 10, 60, 400, 22) GUICtrlSetState($idInput, $GUI_DROPACCEPTED) Local $idOK = GUICtrlCreateButton("OK", 310, 170, 85, 25) ; Display the GUI. GUISetState(@SW_SHOW, $hGUI) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE, $idOK ExitLoop Case $GUI_EVENT_DROPPED ; If the value of @GUI_DropId is $idLabel, then set the label of the dragged file. If @GUI_DropId = $idLabel Then GUICtrlSetData($idLabel, @GUI_DragFile) EndSwitch WEnd ; Delete the previous GUI and all controls. GUIDelete($hGUI) EndFunc ;==>Example