marco hofmann wrote:
Hello,
I want to control the Scrollbar in a grid. The aim is to set the focus
on a defined cell and to move the scrollbar so thats visible. I tried
the following skript (modified script from the Win32::Gui::Grid) but
the sequence
my $Scroll = $Grid->ScaleHeight;
$Grid->ScrollPos(0,$Scroll/2);
doesn't work.
Someone who knows why?
The ScrollPos() method doesn't do what you seem to think it should.
Win32::GUI::Grid has an EnsureCellVisible() method that ensures the cell
passed is in the visible bit of the window - I think that is what you need.
Some comments on your original code below, and a complete script at the
end.
Regards,
Rob.
use strict;
use Win32::GUI();
use Win32::GUI::Grid;
sub paintgrid
{
my $Grid =${$_[0]};
for my $row (0..$Grid->GetRows())
{
for my $col (0..$Grid->GetColumns())
{
if ($row == 0)
{
$Grid->SetCellText($row, $col,"Column : $col");
}
elsif ($col == 0)
{
$Grid->SetCellText($row, $col, "Row : $row");
}
else
{
$Grid->SetCellText($row, $col, "Cell : ($row,$col)");
}
}
}
}
# main Window
my $Window = new Win32::GUI::Window (
Best not to use indirect method invocation:
my $Window = Win32::GUI::Window->new(
-title => "Win32::GUI::Grid",
-pos => [0, 0],
-size => [500, 500],
-name => "Window",
) or die "new Window";
# Grid Window
my $Grid = $Window->AddGrid(
-name => "Grid",
-pos => [0, 40],
-rows => 50,
-columns => 10,
-fixedrows => 1,
-fixedcolumns => 1,
-editable => 1,
-vscroll => 1,
-hscroll =>1,
) or die "new Grid";
# Fill Grid
Nothing broken, but I don't understand why you're using a reference to
$Grid here, as $Grid is already a reference to a Win32::GUI::Grid object.
my $ref_Grid = \$Grid;
paintgrid($ref_Grid);
Your constructor creates a grid with 50 rows and 10 columns. row 3 and
column 30 is outside this range, so the SetFocus() call will do nothing.
$Grid->SetFocusCell(3,30); #Set Cell Focus on Row 3, Column 30
The available scroll range depends entirely on what the class has set up
for it's scrolling parameters, so unless you know the class's innards
you can't scroll this way.
# try to scroll to the middle
my $Scroll = $Grid->ScaleHeight;
$Grid->ScrollPos(0,$Scroll/2);
# Event loop
$Window->Show();
Win32::GUI::Dialog();
# Main window event handler
If all your _Terminate handler is doing is to return -1; then it's not
necessary (unless you need backwards compatibility with Win32::GUI
earlier than v1.0)
sub Window_Terminate {
return -1;
}
sub Window_Resize {
my ($width, $height) = ($Window->GetClientRect)[2..3];
$Grid->Resize ($width, $height);
}
# Grid event handler
sub Grid_Click {
my ($col, $row) = @_;
print "Click on Cell ($col, $row)\n";
}
Here's how I'd do it:
#!perl -w
use strict;
use warnings;
use Win32::GUI();
use Win32::GUI::Grid();
# main Window
my $Window = Win32::GUI::Window->new(
-title => "Win32::GUI::Grid",
-size => [500, 500],
-name => "Window",
) or die "new Window";
# Grid Window
my $Grid = $Window->AddGrid(
-name => "Grid",
-pos => [0, 0],
-rows => 50,
-columns => 10,
-fixedrows => 1,
-fixedcolumns => 1,
-editable => 1,
-vscroll => 1,
-hscroll => 1,
) or die "new Grid";
# Fill Grid
fillgrid($Grid);
$Grid->SetFocusCell(30,3); # Set Cell Focus on Row 30, Column 3
# Must show grid before calling EnsureCellVisible(), else crashes
$Window->Show();
# Ensure the cell with focus is in visible area
$Grid->EnsureCellVisible($Grid->GetFocusCell());
Win32::GUI::Dialog();
$Window->Hide();
exit(0);
# Main window event handler
sub Window_Resize {
$Grid->Resize (($Window->GetClientRect)[2..3]);
return 0;
}
# Grid event handler
sub Grid_Click {
my ($col, $row) = @_;
print "Click on Cell ($col, $row)\n";
return 0;
}
sub fillgrid {
my ($self) = @_;
for my $row (0 .. $self->GetRows()) {
for my $col (0 .. $self->GetColumns()) {
if ($row == 0) {
$self->SetCellText($row, $col,"Column : $col");
}
elsif ($col == 0) {
$self->SetCellText($row, $col, "Row : $row");
}
else {
$self->SetCellText($row, $col, "Cell : ($row,$col)");
}
}
}
return;
}
__END__