[EMAIL PROTECTED] wrote:
This creates a horizontal scrollbar even if the strings inserted do not need scrolling. Can this be set to an
auto-detection? I tried  "-autohscroll", but it didn't work.

Please keep the discussion on the list so that others can benefit from it, and suggest their solutions.

There is (surprisingly) no way to get the underlying listbox control to set the correct horizontal scroll size automatically for you. You need to calculate the size to set the width to. Here's some code to get you going - depending on how dynamic and how large your lists are there may be better ways to do this.

Regards,
Rob.

#!perl -w
use strict;
use warnings;
use Win32::GUI();

# Maximum length of string to add to listbox:
# 100 characters, or set as first argument on
# command line
my $maxlen = $ARGV[0] || 100;

my $main_window = new Win32::GUI::DialogBox(
    -text     => "Listbox scrollbars",
    -pos      => [100,50],
    -size     => [300,150],
);

my $listbox = $main_window -> AddListbox(
    -pos     => [15,20],
    -size    => [200,100],
    -hscroll => 1,
    -vscroll => 1,
);

# Generate some random strings: each string
# consists of set of random printable ASCII
# characters (ASCII code points 32..126) between
# 0 and $maxlen characters long
foreach (1 .. 10) {
    my $len = int(rand($maxlen));
    my $listbox_string = '';
    $listbox_string .= chr(int(rand(94))+32) for (1 .. $len);
    $listbox->InsertItem("$_: $listbox_string");
}

# Set the hscroll width dependent on the strings
listbox_sethscrollwidth($listbox);

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

sub listbox_sethscrollwidth {
    my $lb = shift;
    my $maxwidth = 0;
    my $hFont = $lb->GetFont();
    my $items = $lb->Count();

    for(0 .. $items-1) {
        my ($width, undef) = $lb->GetTextExtentPoint32(
                                      $lb->GetString($_), $hFont);
        $maxwidth = $width if $width > $maxwidth;
    }

    # Need to add a fudge factor to prevent the last charater
    # being partially obscured
    $listbox->SetHorizontalExtent($maxwidth+5);
}

__END__

Reply via email to