Recent

Author Topic: TStringGrid and Checkbox(Solved)  (Read 201 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1259
TStringGrid and Checkbox(Solved)
« on: November 24, 2022, 10:52:11 pm »
I have a form with 3 colms ( 3 to 2) no Fixed columns or Rows.

Column 0 is data
Column 1 is data
Column 2 is Checkbox

I would like to determine which column the user clicks and  if column 2 the state of the checkbox. I programed the CheckboxToggled,OnClicked and OnSelction but I can't make them work.

Missing Something.


 
« Last Edit: November 25, 2022, 12:48:04 am by JLWest »
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

wp

  • Hero Member
  • *****
  • Posts: 10454
Re: TStringGrid and Checkbox
« Reply #1 on: November 24, 2022, 11:56:34 pm »
The column and row indices of the active cell always can be queried from the grid's Col and Row properties, respectively. In the grid's OnClick event the active cell already has moved to the clicked cell.

Be careful when you have added columns to the grid by means of the Columns property editor. Because these column indices are offset from the grid column indices by the count of fixed columns. So, when there is one fixed colum, the first Column that you add by the property editor has index 0 although it is the second column in the grid (with grid index 1). Confusing? Yes...

In your case, there are no fixed columns and thus the index of a Column object is the same as the grid column index.

In order to evaluate whether a checkbox in some row of your column 2 is checked you first read the cell text: cellText := StringGrid1.Cells[2, row]. Among the properties of the grid's column objects there are a "ValueChecked" and a "ValueUnchecked" property, defaulting to 1 and 0, respectively. This means that when the cell text is 1 the checkbox is considered to be ON, if the cell text is 0 the checkbox is OFF, and in all other cases the checkbox is grayed.

Another trap is that the OnClick event fires before the checkbox state changes. So, when you read the checkbox state in the above-described manner in the OnClick event you always get the previous state. There is also a OnCheckboxToggled event which comes after OnClick, and here the checkbox state is correct.

Hoping this information helps you to understand the attached mini project.

JLWest

  • Hero Member
  • *****
  • Posts: 1259
Re: TStringGrid and Checkbox
« Reply #2 on: November 25, 2022, 12:47:34 am »
@wp
The Good and The Bad.
It's not good to know that I missed it by a mile in my feeble attempt.
It is good to know that there is always a pro who has the answer on the form.

Thanks,
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

 

TinyPortal © 2005-2018