Jump to content
Sign in to follow this  
AlexChernavsky

Problem with selecting a file through FileOpenDialog

Recommended Posts

AlexChernavsky

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
JLogan3o13

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
  • Like 1

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

Share this post


Link to post
Share on other sites
AlexChernavsky

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

Share this post


Link to post
Share on other sites
JLogan3o13

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  

  • Similar Content

    • Blois
      By Blois
      Hi Guis,
       
      I need to create keyboard shortcut to navigate the tabs and activate them, but I could not use the code below:
      #include <TabConstants.au3> #include <WindowsConstants.au3> #include <GuiTab.au3> #include <GUIConstantsEx.au3> HotKeySet("^{TAB}", "_TabRight") HotKeySet("^+{TAB}", "_TabLeft") $gui = GUICreate("test",450, 300) $Tab1 = GUICtrlCreateTab(20, 24, 425, 201) $TabSheet1 = GUICtrlCreateTabItem("Tabsheet 1") $lbContagemGrupos2 = GUICtrlCreateLabel("aaaa", 50, 50) GUICtrlSetColor(-1, 0x0000FF) GUICtrlCreateTabItem("") $TabSheet2 = GUICtrlCreateTabItem(" ") $TabSheet3 = GUICtrlCreateTabItem(" ") GUISetState() While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd Func _TabRight() $CurPos = _GUICtrlTab_GetCurSel($Tab1) _GUICtrlTab_SetCurSel($Tab1, $CurPos + 1) $tab = "$TabSheet" & $CurPos GUICtrlSetState($TabSheet1 + $CurPos, $GUI_SHOW) Return EndFunc Func _TabLeft() $CurPos = _GUICtrlTab_GetCurSel($Tab1) _GUICtrlTab_SetCurSel($Tab1, $CurPos - 1) $tab = "$TabSheet" & $CurPos GUICtrlSetState($TabSheet1 + $CurPos, $GUI_SHOW) Return EndFunc Exit  
      can you help me?
    • Pricehacker
      By Pricehacker
      Hello again!
      I have tried searching for quite some time now but couldn´t find a single working way to change the class of your GUI that doesn´t include changing it manually after its compilation.
      First or "AutoIt v3 GUI" is a pretty boring name tbh and i would like to spice it up a bit, also, and more importantly, I would like to identify my windows by something other than its title as it will change depending on some factors and im using two different programs independently as multi threading isn't supported.
      Any help is appreciated! Thank you!
    • xtcislove
      By xtcislove
      Hello, 
      i have a Problem with  _WinAPI_SetLayeredWindowAttributes, everything is working as excepted if u are on Windows 7.

      But if u are on Windows 10 the transparent background of a control is not clickable anymore. 

      Could someone help?

      Please see example below, put cmd.jpg into the same folder as Example.au3

      Please see trigger.jpg to see which sections are not clickable in Windows 10 (the red dots, the green dots are clickable) on Windows 7 the red ones and green ones are clickable.
       
      The same problem occurs if i use 
       
       
      Example.au3


    • WilliamasKumeliukas
      By WilliamasKumeliukas
      Hello everyones reading this post,
      I'm struggling at resizing properly my radio buttons to fit long text inside without affecting others radio buttons and also adjust position of them relative to the string.
      I tried with _StringSize UDF but I guess im too tired to figure out how to use it correctly..
      I also tried: _Control_SetWidth2Text I found on this forum, it work aslong im not maximizing GUI (radio text is gone when I maximize and maximize is a must)
       
      here is the code example, I hope someone can figure out (sorry if guictrlsetpos is not included in the example, I know thats probably the way to go but I wanted to make sure there is nothing better before use it)
      Thank you
       
      Br,
      ~WilliamasKumeliukas
       
      #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <Array.au3> #include <StringConstants.au3> #include <FileConstants.au3> #include <WindowsConstants.au3> #include <File.au3> #include <EditConstants.au3> #include <GUIListBox.au3> #include <ButtonConstants.au3> #include <ProgressConstants.au3> #include <ColorConstants.au3> #include <AutoitConstants.au3> #include <Misc.au3> #include <WinAPIReg.au3> #include <WinAPI.au3> #Region Opt's Opt("GUIResizeMode", 802) #EndRegion Opt's Global $q = "INTRO LINE" & @CRLF & @CRLF & "description of everything until user press start." Global $file = @TempDir & "\test.p" destroy() Global $msg, $score = 0, $rate, $com, $total = 0, $cfile, $cq, $i = 0, $time = 0, $min = 0, $sec = 0, $hour = 0, $qf, $ii = 0 Main() Func Main() Global $iGUIInitSize = 550 #Region GUI Opt("GUICloseOnEsc", 1) Global $gui = GUICreate("example", 1124, 550,-1 ,-1, BitOR($WS_MAXIMIZEBOX , $WS_MINIMIZEBOX)) Global $gui_size = WinGetClientSize($gui) Global $iFontSize = Int(2 * (.25 + (8 * $gui_size[0] / $iGUIInitSize))) / 2 Global $options = GUICtrlCreateMenu("&Options") Global $guide = GUICtrlCreateMenuItem("Guide", $options) Global $restart = GUICtrlCreateMenuItem("Restart", $options) Global $about = GUICtrlCreateMenuItem("About", $options) Global $exit = GUICtrlCreateMenuItem("Exit", $options) Global $extra = GUICtrlCreateMenu("Extras") Global $creator = GUICtrlCreateMenu("CQB", $extra ) Global $new = GUICtrlCreateMenuItem("custom QP", $creator) Global $load = GUICtrlCreateMenuItem("CQ", $creator ) Global $timer = GUICtrlCreateLabel("", 752, 472, 294, 30, $WS_BORDER) GUICtrlSetResizing(-1, BitOR($GUI_DOCKHCENTER, $GUI_DOCKVCENTER) ) GUICtrlSetFont(-1, $iFontSize) GUICtrlSetState($timer, $GUI_HIDE) Global $info = GUICtrlCreateLabel( $q, 200, 20, 700, 200, $SS_CENTER ) GUICtrlSetResizing(-1, BitOR($GUI_DOCKHCENTER, $GUI_DOCKVCENTER) ) $fi = GUICtrlSetFont($info, $iFontSize) Global $a = GUICtrlCreateRadio("A)", 100, 210, $WS_EX_CLIENTEDGE) $fa = GUICtrlSetFont($a, 15) GUICtrlSetResizing(-1, BitOR($GUI_DOCKHCENTER, $GUI_DOCKVCENTER) ) GUICtrlSetState($a, 16) Global $b = GUICtrlCreateRadio("B)", 100, 310 , $WS_EX_CLIENTEDGE) $fb = GUICtrlSetFont($b, 15) GUICtrlSetResizing(-1, BitOR($GUI_DOCKHCENTER, $GUI_DOCKVCENTER) ) GUICtrlSetState($b, 16) Global $c = GUICtrlCreateRadio("C)", 750, 210 , $WS_EX_CLIENTEDGE) $fc = GUICtrlSetFont($c, 15) GUICtrlSetResizing(-1, BitOR($GUI_DOCKHCENTER, $GUI_DOCKVCENTER) ) GUICtrlSetState($c, 16) Global $d = GUICtrlCreateRadio( "D) ", 750, 310, $WS_EX_CLIENTEDGE) $fd = GUICtrlSetFont($d, 15) GUICtrlSetResizing (-1, BitOR($GUI_DOCKHCENTER, $GUI_DOCKVCENTER) ) GUICtrlSetState($d, 16) Global $sign = GUICtrlCreateLabel( $file, 420, 480, -1, -1, BitOR ( $SS_CENTER, $WS_BORDER ) ) GUICtrlSetResizing ( -1, BitOR ( $GUI_DOCKHCENTER, $GUI_DOCKVCENTER ) ) GUICtrlSetFont($sign, 12) Global $yb = GUICtrlCreateRadio("True", 200, 420, 200, 50, $BS_PUSHLIKE) GUICtrlSetResizing(-1, BitOR($GUI_DOCKHCENTER, $GUI_DOCKVCENTER,$GUI_DOCKSIZE)) GUICtrlSetFont($yb, 15) GUICtrlSetState($yb, 32) Global $nb = GUICtrlCreateRadio("False", 700, 420, 200, 50, $BS_PUSHLIKE) GUICtrlSetResizing(-1, BitOR($GUI_DOCKHCENTER, $GUI_DOCKVCENTER, $GUI_DOCKSIZE)) GUICtrlSetFont($nb, 15) GUICtrlSetState($nb, 32) Global $start = GUICtrlCreateButton("Start", 20, 20, 100, 50, $SS_CENTER, $WS_EX_CLIENTEDGE) GUICtrlSetResizing(-1, BitOR($GUI_DOCKHCENTER, $GUI_DOCKVCENTER)) GUICtrlSetFont($start, 12) Global $valid = GUICtrlCreateButton("Confirm", 20, 20, 100, 50, $SS_CENTER, $WS_EX_CLIENTEDGE) GUICtrlSetResizing(-1, BitOR($GUI_DOCKHCENTER, $GUI_DOCKVCENTER)) GUICtrlSetFont($valid, 12) GUICtrlSetState($valid, 32) GUISetState(@SW_SHOW) #EndRegion GUI While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Switch MsgBox(4096 + 4, "Warning", "Are you sure to exit?") Case 6 AdlibUnRegister("timer") If Not $qf = "" Then FileClose($file) EndIf Exit Case 7 ContinueLoop EndSwitch Case $start ;AdlibRegister("timer") ;Global $qf = StringSplit(StringStripWS(FileRead($file), 6), @CRLF, 0) ;qloop($file) GUICtrlSetData($a, "A) This long string is an example as string that I want to fit in 4 radio buttons because sometimes it can be that long.") GUICtrlSetData($b, "B) This long string is an example as string that I want to fit in 4 radio buttons because sometimes it can be that long.") GUICtrlSetData($c, "C) This long string is an example as string that I want to fit in 4 radio buttons because sometimes it can be that long.") GUICtrlSetData($d, "D) This long string is an example as string that I want to fit in 4 radio buttons because sometimes it can be that long.") Case $restart MsgBox(4096, "Warning", 'Normally, your suposed to start something before restarting it.') ContinueLoop Case $new ;GUISetState(@SW_HIDE, $gui) ;_CustomNew() ContinueLoop Case $load ;_CustomLoad() Case $about ;_About() EndSwitch WEnd EndFunc ;Main() Func timer() If $time = 0 Then Global $time = TimerInit() Global $diff = TimerDiff($time) Global $sec = Int(Mod($diff / 1000, 60)) Global $min = Int(Mod($diff / 60000, 60)) Global $hour = Int($diff / 3600000) If $sec < 10 Then $sec = "0" & $sec If $min < 10 Then $min = "0" & $min If $hour < 10 Then $hour = "0" & $hour GUICtrlSetData($timer, "Temps: " & $hour & ":" & $min & ":" & $sec) EndFunc ;==>timer Func destroy() If FileExists($file) Then FileDelete($file) EndIf DirCreate(@TempDir & "\About") FileInstall("C:\test.txt", $file) EndFunc ;==>destroy  
    • xtcislove
      By xtcislove
      Hello,

      im searching like 6 hours and i didnt found the right solution.

      Im trying to build a treeview from a directory and its subfolders etc.

      This function give me right treeview, but i cant color each item seperated.

       
      #include <GuiTreeView.au3> $hGui = GUICreate("Demo1", 600, 400) $hTreeView = _GUICtrlTreeView_Create($hGui, 10, 10, 580, 380) GUISetState() _GUICtrlTreeView_BeginUpdate($hTreeView) ListFiles_ToTreeView(@ScriptDir, 0) _GUICtrlTreeView_EndUpdate($hTreeView) Do Until GUIGetMsg() = -3 Func ListFiles_ToTreeView($sSourceFolder, $hItem) Local $sFile ; Force a trailing \ If StringRight($sSourceFolder, 1) <> "\" Then $sSourceFolder &= "\" ; Start the search Local $hSearch = FileFindFirstFile($sSourceFolder & "*.*") ; If no files found then return If $hSearch = -1 Then Return ; This is where we break the recursive loop <<<<<<<<<<<<<<<<<<<<<<<<<< ; Now run through the contents of the folder While 1 ; Get next match $sFile = FileFindNextFile($hSearch) ; If no more files then close search handle and return If @error Then ExitLoop ; This is where we break the recursive loop <<<<<<<<<<<<<<<<<<<<<<<<<< ; Check if a folder If @extended Then ; If so then call the function recursively ListFiles_ToTreeView($sSourceFolder & $sFile, _GUICtrlTreeView_AddChild($hTreeView, $hItem, $sFile)) Else ; If a file than write path and name _GUICtrlTreeView_AddChild($hTreeView, $hItem, $sFile) EndIf WEnd ; Close search handle FileClose($hSearch) EndFunc ;==>ListFiles_ToTreeView
      So i am searching for the exact same function just with
      GUICtrlCreateTreeViewItem
      instead of 
      _GUICtrlTreeView_AddChild
       
      Ps: i have a folder structure where i like to color each item green if a file only exist once and red if it exist more than once.

      Could someone help?
       
      Solution:

       
       
×