Recent

Author Topic: Out of bounds index error  (Read 1464 times)

Al3

  • New Member
  • *
  • Posts: 19
Out of bounds index error
« on: June 23, 2022, 12:48:24 am »
Hey!

I am using
Code: Pascal  [Select][+][-]
  1. TStringGrid
and wrote this function to create a new column when the user ctrl+clicks on a column:

Code: Pascal  [Select][+][-]
  1. { Signals }
  2. procedure TForm1.listviewHeaderClick(Sender: TObject; IsColumn: Boolean; Index: Integer);
  3. var
  4.     Column   : TGridColumn;
  5. begin
  6.      if GetKeyPressed(VK_CONTROL) = FALSE then exit;
  7.      if isColumn                  = FALSE then exit;
  8.  
  9.      Column := listview.Columns.Add;
  10.      Column.Title.Caption := 'Unnamed_' + listview.colCount.toString();
  11.      Column.SizePriority := 1;
  12.      Column.Index := Index;
  13.      Column.Alignment := taCenter;
  14. end;

However, when I set the TStringGrid's
Code: Pascal  [Select][+][-]
  1. AutoFillCoumns
property to TRUE, so that columns expand and fill up the available space, when I attempt to create a column the program blows with an exception:

Quote
Project tables raised exception class 'EGridException' with message:
Index Out of range Cell[Col=2 Row=4]

I really don't know what this has to do with out of range cells and error messages with the likes of that.
I am a bit incompetent as I am primarily a C developer that mostly only used GTK+
I came back to Lazarus after 6 years and it feels good!  :)

Zvoni

  • Hero Member
  • *****
  • Posts: 2300
Re: Out of bounds index error
« Reply #1 on: June 23, 2022, 09:22:39 am »
and you actually have 2 columns with 4 rows (incl. Fixed Rows/Columns)?

btw: Is it now a TStringGrid or a ListView?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 14166
  • Probably until I exterminate Putin.
Re: Out of bounds index error
« Reply #2 on: June 23, 2022, 10:02:01 am »
Yes, I was wondering about that too... We need more code, proper code.
Specialize a type, not a var.

Al3

  • New Member
  • *
  • Posts: 19
Re: Out of bounds index error
« Reply #3 on: June 23, 2022, 07:58:20 pm »
It is a TStringGrid

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Menus, ExtCtrls,
  9.   LCLIntf, LCLType,
  10.   ValEdit, Grids;
  11.  
  12. type
  13.  
  14.   { TForm1 }
  15.  
  16.   TForm1 = class(TForm)
  17.     mm: TMainMenu;
  18.     MenuItemFile: TMenuItem;
  19.     MenuItemEdit: TMenuItem;
  20.     MenuItemSearch: TMenuItem;
  21.     MenuItemHelp: TMenuItem;
  22.     MenuItemNew: TMenuItem;
  23.     MenuItemOpen: TMenuItem;
  24.     MenuItemSave: TMenuItem;
  25.     MenuItemSaveAs: TMenuItem;
  26.     listview: TStringGrid;
  27.  
  28.     procedure listviewHeaderClick(Sender: TObject; IsColumn: Boolean; Index: Integer);
  29.  
  30.   private
  31.  
  32.   public
  33.  
  34.   end;
  35.  
  36. var
  37.   Form1: TForm1;
  38.  
  39. implementation
  40.  
  41. function GetKeyPressed(const VKeyCode: Integer): Boolean;
  42. begin
  43.   Result := GetKeyState(VKeyCode) and $80 <> 0;
  44. end;
  45.  
  46.  
  47.  
  48. {$R *.lfm}
  49.  
  50. { TForm1 }
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. { Signals }
  63. procedure TForm1.listviewHeaderClick(Sender: TObject; IsColumn: Boolean; Index: Integer);
  64. var
  65.     Column   : TGridColumn;
  66. begin
  67.      if GetKeyPressed(VK_CONTROL) = FALSE then exit;
  68.      if isColumn                  = FALSE then exit;
  69.  
  70.      Column := listview.Columns.Add;//TGridColumn.Create(listview.Columns);
  71.      Column.Title.Caption := 'Unnamed_' + listview.colCount.toString();
  72.      Column.SizePriority := 1;
  73.      Column.Index := Index;
  74.      Column.Alignment := taCenter;
  75.      //listview.AutoFillColumns := TRUE;
  76. end;
  77.  
  78. end.

Picture of how the form looks when I run the application: https://game-editor2.com/table.png
Or the entire project: https://game-editor2.com/tables.rar

There are several new concepts for me and I haven't had the time to get used to that.
« Last Edit: June 23, 2022, 08:00:09 pm by Al3 »

Zvoni

  • Hero Member
  • *****
  • Posts: 2300
Re: Out of bounds index error
« Reply #4 on: June 23, 2022, 08:02:58 pm »
Ahh….. „ListView“ is the name of the TStringGrid.

Well, the only thing coming to mind has to do with the (re-) painting of the Form/Grid
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Al3

  • New Member
  • *
  • Posts: 19
Re: Out of bounds index error
« Reply #5 on: June 23, 2022, 08:17:33 pm »
Ahh….. „ListView“ is the name of the TStringGrid.

Well, the only thing coming to mind has to do with the (re-) painting of the Form/Grid

Yep, in GTK+ that's called a listview so I am used to name it that way :D

repainting of the form should not cause such an error either way. Maybe I did something wrong in the code?
Also most of the properties in Options are enabled.

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: Out of bounds index error
« Reply #6 on: June 23, 2022, 08:38:58 pm »
Game Editor? Wow, glad to know someone is developing it. I was a fan of it and I even made a private class taught my friends how to use it. Game Editor was the first game builder I learned. I still remember, I was so excited after I understood the actor concept and managed to build a simple animation using it.

Now, back to the topic.

I copied, pasted and run the code you provided. Not surprisingly, I didn't get any error as you mentioned. So, can you please provide the source code that can show the issue so we can download, run, inspect and help you?

Al3

  • New Member
  • *
  • Posts: 19
Re: Out of bounds index error
« Reply #7 on: June 23, 2022, 10:29:53 pm »
Game Editor? Wow, glad to know someone is developing it. I was a fan of it and I even made a private class taught my friends how to use it. Game Editor was the first game builder I learned. I still remember, I was so excited after I understood the actor concept and managed to build a simple animation using it.

Now, back to the topic.

I copied, pasted and run the code you provided. Not surprisingly, I didn't get any error as you mentioned. So, can you please provide the source code that can show the issue so we can download, run, inspect and help you?

Glad to hear about past members!
I initiated this project not too long ago actually and came out public just this year :)

Back to the topic, what do you mean?
I already posted a link to the entire project folder: https://game-editor2.com/tables.rar   :-[
That shall suffice, right?

Make sure to enable the option to observe this error and I really hope that it can be reproduced.
I am running a fresh Lazarus installation on Win11 and I only installed anchor docking.
But you know.. considering that I consider myself very new to Lazarus and Pascal it is probably something stupid and definitely my fault

Josh

  • Hero Member
  • *****
  • Posts: 1270
Re: Out of bounds index error
« Reply #8 on: June 24, 2022, 01:45:53 am »

Try this moded routine, i dont think your given the grid time to update;

Code: Pascal  [Select][+][-]
  1. procedure TForm1.listviewHeaderClick(Sender: TObject; IsColumn: Boolean; Index: Integer);
  2. var
  3.     Column   : TGridColumn;
  4. begin
  5.   If Sender is TStringGrid then
  6.   begin
  7.     if GetKeyPressed(VK_CONTROL) = FALSE then exit;
  8.     if isColumn                  = FALSE then exit;
  9.     TStringGrid(Sender).BeginUpdate;
  10.     Column := listview.Columns.Add;//TGridColumn.Create(listview.Columns);
  11.     Column.Title.Caption := 'Unnamed_' + listview.colCount.toString();
  12.     Column.SizePriority := 1;
  13.     Column.Index := Index;
  14.     Column.Alignment := taCenter;
  15.     TStringGrid(Sender).EndUpdate(true);
  16.   end;
  17. end;              
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

Al3

  • New Member
  • *
  • Posts: 19
Re: Out of bounds index error
« Reply #9 on: June 24, 2022, 02:59:24 am »

Try this moded routine, i dont think your given the grid time to update;

Code: Pascal  [Select][+][-]
  1. procedure TForm1.listviewHeaderClick(Sender: TObject; IsColumn: Boolean; Index: Integer);
  2. var
  3.     Column   : TGridColumn;
  4. begin
  5.   If Sender is TStringGrid then
  6.   begin
  7.     if GetKeyPressed(VK_CONTROL) = FALSE then exit;
  8.     if isColumn                  = FALSE then exit;
  9.     TStringGrid(Sender).BeginUpdate;
  10.     Column := listview.Columns.Add;//TGridColumn.Create(listview.Columns);
  11.     Column.Title.Caption := 'Unnamed_' + listview.colCount.toString();
  12.     Column.SizePriority := 1;
  13.     Column.Index := Index;
  14.     Column.Alignment := taCenter;
  15.     TStringGrid(Sender).EndUpdate(true);
  16.   end;
  17. end;              

Yep, that indeed worked out here.

If I only succeeded to find a source where I could learn about that..
I would've had to deal with that for very long time if I didn't ask here, probably end up ditching this approach entirely.

Also that's a somewhat of a weird pipeline to see (with the code inside BeginUpdate and EndUpdate) and is also still puzzling to me why an error about indexing bounds occurs when I don't give time for the grid to update.

And thanks a lot everyone!

« Last Edit: June 24, 2022, 03:01:09 am by Al3 »

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: Out of bounds index error
« Reply #10 on: June 24, 2022, 04:56:50 am »
I already posted a link to the entire project folder: https://game-editor2.com/tables.rar   :-[
That shall suffice, right?

Oh I'm sorry, I thought that was in C/C++, I hadn't tried it.


... I consider myself very new to Lazarus and Pascal ...

I think you might be interested to try:
https://wiki.freepascal.org/Portal:HowTo_Demos

You can find many short demos for doing certain tasks using Lazarus. For StringGrid demos, you can find them in the User Interface category.

And don't forget to bookmark this page:
https://wiki.freepascal.org/Lazarus_Documentation

Al3

  • New Member
  • *
  • Posts: 19
Re: Out of bounds index error
« Reply #11 on: June 24, 2022, 04:59:22 pm »
I already posted a link to the entire project folder: https://game-editor2.com/tables.rar   :-[
That shall suffice, right?

Oh I'm sorry, I thought that was in C/C++, I hadn't tried it.


... I consider myself very new to Lazarus and Pascal ...

I think you might be interested to try:
https://wiki.freepascal.org/Portal:HowTo_Demos

You can find many short demos for doing certain tasks using Lazarus. For StringGrid demos, you can find them in the User Interface category.

And don't forget to bookmark this page:
https://wiki.freepascal.org/Lazarus_Documentation

Yes, that is useful thank you :)
By the way, you can check up what the project is about. I like hearing other's opinion, ideas and preferences!

 

TinyPortal © 2005-2018