MrBeatnik Posted November 18, 2010 Share Posted November 18, 2010 Hi folks.I create some controls on a form dynamically, depending on what is returned from a sepcific function.I want to delete all existing controls first, and then recreate (there could be different number of controls each time).This is generally fine - a For...Next loop can loop through the control array and do a GUICTRLDelete.However, there is no control created until the data is passed for the first time.This means that the GUICTRLDelete errors out and kills the program.Is there a way to check if a GUIcontrol exists?An example code may help demonstrate what I am trying to achieve:Dim $controlArray[1] Func _displayData($myData) ;First clean out data For $myCount = 0 to UBound($controlArray)-1 Step +1 GUICtrlDelete ( $controlArray[$myCount] ) ;FAILS HERE THE FIRST TIME AS CONTROL HAS NOT BEEN CREATED YET Next ;Now set data ReDim $controlArray[UBound($myData)] $topInc = 0 For $myCount = 0 to UBound($myData)-1 Step +1 $controlArray[$myCount] = GUICtrlCreateLabel($myData[$myCount], 56, 136 + $topInc, 81, 22, -1, 0) $topInc += 31 Next EndFuncNote this example code of course doesn't work on its own.It requires the GUI to be created etc, and the function to be called with data passed.Thanks! Please correct me if I am wrong in any of my posts. I like learning from my mistakes too. Link to comment Share on other sites More sharing options...
wakillon Posted November 18, 2010 Share Posted November 18, 2010 Try a GUICtrlGetState before delete ctrl ... AutoIt 3.3.14.2 X86 - SciTE 3.6.0 - WIN 8.1 X64 - Other Example Scripts Link to comment Share on other sites More sharing options...
KaFu Posted November 18, 2010 Share Posted November 18, 2010 (edited) The code shouldn't fail because of GUICtrlDelete(), as it returns 0 if the function fails and does not crash. Maybe they arrays do not exist? Global $controlArray Func _displayData($myData) ;First clean out data If IsArray($controlArray) Then For $myCount = 0 To UBound($controlArray) - 1 GUICtrlDelete($controlArray[$myCount]) ;FAILS HERE THE FIRST TIME AS CONTROL HAS NOT BEEN CREATED YET Next EndIf ;Now set data If IsArray($myData) Then Dim $controlArray[UBound($myData)] $topInc = 0 For $myCount = 0 To UBound($myData) - 1 $controlArray[$myCount] = GUICtrlCreateLabel($myData[$myCount], 56, 136 + $topInc, 81, 22, -1, 0) $topInc += 31 Next EndIf EndFunc ;==>_displayData Edited November 18, 2010 by KaFu OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2022-Nov-26) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Feb-16) HMW - Hide my Windows (2018-Sep-16) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2023-Jun-03) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
MrBeatnik Posted November 18, 2010 Author Share Posted November 18, 2010 The code shouldn't fail because of GUICtrlDelete(), as it returns 0 if the function fails and does not crash. Maybe they arrays do not exist? Have it sorted, although slightly strange behaviour I think. The array does exist, but I actually have a 2d array. I should have mentioned this the first time. Although I expected GUICtrlDelete() to return 0 (as per the help), it was actually crashing because, when trying to check the control, it found an array. Doesn't work: GUICtrlDelete($controlArray[$myCount]) Does work: GUICtrlDelete($controlArray[$myCount])[0] So GUICtrlDelete() fails and crashes if the object is an array. I feel that GUICtrlDelete() should have error control to return -1 or something if an expect object wasn't found, rather than crashing out. Of course, had this not have crashed out, the control would not have been deleted if it did exist as I wasn't referencing the control directly, but the first array in which the second array of controls is held. It was invalid code. Thanks for the pointer KaFu! Please correct me if I am wrong in any of my posts. I like learning from my mistakes too. Link to comment Share on other sites More sharing options...
BrewManNH Posted November 18, 2010 Share Posted November 18, 2010 What EXACTLY is the error message you receive when it fails? Also, please post an example script that actually "works" to demonstrate the error so that we can see what is happening and where. Trying to trouble shoot this problem with a small snippet of code isn't going to help much and not knowing what error message you receive makes any attempts at helping a guess and nothing more. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
MrBeatnik Posted November 18, 2010 Author Share Posted November 18, 2010 What EXACTLY is the error message you receive when it fails? Also, please post an example script that actually "works" to demonstrate the error so that we can see what is happening and where. Trying to trouble shoot this problem with a small snippet of code isn't going to help much and not knowing what error message you receive makes any attempts at helping a guess and nothing more.As per my last post, it is fixed. Please correct me if I am wrong in any of my posts. I like learning from my mistakes too. Link to comment Share on other sites More sharing options...
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