jtsteffen Posted October 28, 2008 Posted October 28, 2008 Hello I've wasted a couple of days trying to figure out what the heck I'm doing wrong. I have a much larger script that I run that checks for files. I count the number of files and assign that number to a variable $NumLocalFiles. If this variable is 0, the program is suppose to create a label and tell the user that "No Files To Return". If that variable is greater than 0 it is suppose to Delete that control and take it off my window. For some reason the text will not go away sometimes. I wrote a simple code below to demonstrate the problem. You can push the Button 1, then Button 2, then Button 1, back and forth and the text will come and go as it should. However if you hit the Button 1 twice in a row, the text will never go away. What am I doing wrong here. Thanks CODE#include <GUIConstantsEx.au3> #include <ButtonConstants.au3> Global $NumLocalFiles = 1, $NoLocalToReturn, $Files ; Create GUI $MainWin = GUICreate("AutoFarm Document Management Utility", 600, 600) $But1 = GUICtrlCreateButton("Turn on", 505, 150, 80, 80, $BS_MULTILINE) $But2 = GUICtrlCreateButton("Turn off", 505, 450, 80, 80, $BS_MULTILINE) $Files = GUICtrlCreateLabel($NumLocalFiles, 10,10, 20,20) GUISetState(@SW_SHOW) While 1 $msg = GUIGetMsg() Select Case $msg = $GUI_EVENT_CLOSE ExitLoop Case $msg = $But1 $NumLocalFiles = 0 _ButtonPush() Case $msg = $But2 $NumLocalFiles = 1 _ButtonPush() EndSelect WEnd Func _ButtonPush() GUICtrlSetData($Files, $NumLocalFiles) If $NumLocalFiles = 0 Then ; There are no files on the local drive $NoLocalToReturn = GUICtrlCreateLabel("No Files to Return", 30, 164, 200, 15) GUICtrlSetFont($NoLocalToReturn, 12, 400) Else GUICtrlDelete($NoLocalToReturn) ; Remove the No Files to Return text EndIf EndFunc
Monamo Posted October 28, 2008 Posted October 28, 2008 HelloI've wasted a couple of days trying to figure out what the heck I'm doing wrong. I have a much larger script that I run that checks for files. I count the number of files and assign that number to a variable $NumLocalFiles. If this variable is 0, the program is suppose to create a label and tell the user that "No Files To Return". If that variable is greater than 0 it is suppose to Delete that control and take it off my window. For some reason the text will not go away sometimes. I wrote a simple code below to demonstrate the problem. You can push the Button 1, then Button 2, then Button 1, back and forth and the text will come and go as it should. However if you hit the Button 1 twice in a row, the text will never go away. What am I doing wrong here.ThanksCODE#include <GUIConstantsEx.au3>#include <ButtonConstants.au3>Global $NumLocalFiles = 1, $NoLocalToReturn, $Files; Create GUI $MainWin = GUICreate("AutoFarm Document Management Utility", 600, 600) $But1 = GUICtrlCreateButton("Turn on", 505, 150, 80, 80, $BS_MULTILINE) $But2 = GUICtrlCreateButton("Turn off", 505, 450, 80, 80, $BS_MULTILINE) $Files = GUICtrlCreateLabel($NumLocalFiles, 10,10, 20,20) GUISetState(@SW_SHOW)While 1 $msg = GUIGetMsg() Select Case $msg = $GUI_EVENT_CLOSE ExitLoop Case $msg = $But1 $NumLocalFiles = 0 _ButtonPush() Case $msg = $But2 $NumLocalFiles = 1 _ButtonPush() EndSelectWEndFunc _ButtonPush() GUICtrlSetData($Files, $NumLocalFiles) If $NumLocalFiles = 0 Then ; There are no files on the local drive $NoLocalToReturn = GUICtrlCreateLabel("No Files to Return", 30, 164, 200, 15) GUICtrlSetFont($NoLocalToReturn, 12, 400) Else GUICtrlDelete($NoLocalToReturn) ; Remove the No Files to Return text EndIfEndFuncThe problem is that when you click Button 1 the first time, you're creating a label control with a specific "control id" - But your second click (while the first control exists still) is creating a new label control exactly on top of the existing one and assigning that id to $NoLocalReturn, essentially leaving the first one in limbo. When you click Button 2, it's "working" - it's deleting the new control id referenced by the $NoLocalReturn variable. What you're left with is the first one created.Example:Click Button1 - $NoLocalReturn label created with control id = "5"Click Button1 - (new) $NoLocalReturn label created with control id = "6" <== This is where you lose a variable pointer to control id "5"Button 2 - Deletes Control id "6"What you could do is simply create the label when you create the rest of your GUI elements, and just hide/show the label as needed. Either that or create it with no text and just toggle using GUICtrlSetData() to set the "no files" message or an empty string "".There are a handful of ways to handle it, but hopefully the explanation above will help you understand the "why" of the issue. - MoChr(77)& Chr(97)& Chr(100)& Chr(101)& Chr(32)& Chr(121)& Chr(97)& Chr(32)& Chr(108)& Chr(111)& Chr(111)& Chr(107)-------I've told you 100,000 times not to exaggerate!-------Don't make me hit you with my cigarette hand...-------My scripts:Random Episode Selector, Keyboard MouseMover, CopyPath v2.1, SmartRename for XP,Window Tracer[sup]New![/sup]
jtsteffen Posted October 28, 2008 Author Posted October 28, 2008 (edited) Thanks for your timely response. All I can say now is duh. It makes complete sense. Your explaination also explains a bunch of other "odd" things I saw earlier that I fixed by creating all the controls in the first section and then used the HIDE and SHOW commands to fix. I knew it worked I just didn't put together why it did.Thanks again.Here is the code that fixes the problem. For those of you that careCODE#include <GUIConstantsEx.au3>#include <ButtonConstants.au3>Global $NumLocalFiles = 1, $NoLocalToReturn, $Files; Create GUI $MainWin = GUICreate("AutoFarm Document Management Utility", 600, 600) $But1 = GUICtrlCreateButton("Turn on", 505, 150, 80, 80, $BS_MULTILINE) $But2 = GUICtrlCreateButton("Turn off", 505, 450, 80, 80, $BS_MULTILINE) $Files = GUICtrlCreateLabel($NumLocalFiles, 10,10, 20,20) $NoLocalToReturn = GUICtrlCreateLabel("No Files to Return", 30, 164, 200, 15) GUICtrlSetFont($NoLocalToReturn, 12, 400) GUISetState(@SW_SHOW) _ButtonPush() While 1 $msg = GUIGetMsg() Select Case $msg = $GUI_EVENT_CLOSE ExitLoop Case $msg = $But1 $NumLocalFiles = 0 _ButtonPush() Case $msg = $But2 $NumLocalFiles = 1 _ButtonPush() EndSelectWEndFunc _ButtonPush() GUICtrlSetData($Files, $NumLocalFiles) If $NumLocalFiles = 0 Then ; There are no files on the local drive GUICtrlSetState ( $NoLocalToReturn, $GUI_SHOW) ElseIf $NumLocalFiles > 0 Then GUICtrlSetState ( $NoLocalToReturn, $GUI_HIDE) EndIfEndFunc Edited October 28, 2008 by jtsteffen
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now