Dan Dascalescu wrote:

Hello everyone,

I've been using Win32::GUI for a while, but this is my first post on
the mailing list.
Welcome.

What I'm wondering is if there is a way to iterate through all the
controls in a window, by name. After creating a window, I examined the
object in Komodo and I saw that its children had numerical names.
What you should see is that every window/control has a -name member in its hash. The value of this -name key is the name of the window/control and is set by the -name option to the constructor (new). If a -name option is not given to the constructor, then Win32::GUI generates internal names of the form '#123456'.

This name should also appear as a the key in the control's parent hash, with it's value being a reference to the child's object. So yes, it is possible to iterate through a windows children. This scheme also allows access to child controls without storing each object:

my $win = Win32::GUI::Window->new( ... );
$win->AddButton( -name => 'myButton', ... );

$win->myButton->Text("Press Me");

So if you set the names of your controls, you can access them directly.

For example, suppose you have N radio buttons in a group and you need
to determine which one was checked. One way is to store the radio
button objects in an array, iterate and use $rb_array[$i]->Checked.
However, since the window must know its children, is there a way to
iterate through these radio buttons using no extra array?

Something like foreach (grep(/^rb/, $window->ChildrenNames)) {
do_someting if $window{$_}->Checked }
If you were to use a consistent naming scheme for your radio buttons with the 
-name option to the constructor, for example RB1 .. RBN, then you can to do 
something like this:

#!perl -w

use strict;
use warnings;

use Win32::GUI ();

my $win = Win32::GUI::Window->new(
        -title => "Radio Buttons",
        -pos   => [100,100],
        -size  => [400,300],
);

for my $i (1..3) {
        $win->AddRadioButton(
                -name => "RB$i",
                -text => "Radio button $i",
                -top  => 20 * $i,
        );
}

$win->AddButton(
        -text    => "Check",
        -pos     => [200,20],
        -onClick => \&Check,
);

$win->Show();
Win32:GUI::Dialog();
exit(0);

sub Check
{
        for my $key (%{$win}) {
                next unless $key =~ /RB(\d+)/;
                if ($win->{$key}->Checked()) {
                        print "Radio button $1 checked\n";
                        last;
                }
        }


        return 1;
}
__END__

Regards,
Rob.


Reply via email to