Jump to content

Check if GUICTRL exists.


 Share

Recommended Posts

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
EndFunc

Note 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

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 by KaFu
Link to comment
Share on other sites

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. :graduated:

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

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 Gude
How 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

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

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...