Recent

Author Topic: Stringgrid checkbox with select-only-one functionality  (Read 4457 times)

mijen67

  • Full Member
  • ***
  • Posts: 130
  • It's hard to beat the bandwidth of a flying DVD
Stringgrid checkbox with select-only-one functionality
« on: October 07, 2017, 11:38:33 pm »
I'm using a stringgrid with a column defined as a checkbox (by setting ButtonStyle=cbsCheckboxColumn, ValueChecked and ValueUnchecked are default i.e. 1 and 0; and finally option GoEditing is true so that user can toggle each checkbox-cell).

In the application I sometimes need to allow multiple rows to be selected (by checking checkbox) and in other cases I need to request user to only select one row. In the latter case when user clicks on one rows checkbox all other rows should be set to unchecked. Is there an elegant way to accomplish this? Or should the functionality be programmed "manually"?

Kind regards,
Michael

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Stringgrid checkbox with select-only-one functionality
« Reply #1 on: October 08, 2017, 01:07:51 am »
1) doesn't stringgrid support multiselect? (haven't used for so long I can't remember)
2)to code a multi select ability you create a list of selected rows when the user checks the box you add the row to the list when the boxgets uncheckd you remove the row from the list. you do not keep the state of the selection in the grid / data you simple paint a checked box if the row is in the checked list, otherwise you paint an unchecked box. That is in my experience the less prone to errors method.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Stringgrid checkbox with select-only-one functionality
« Reply #2 on: October 08, 2017, 01:12:29 am »
The OnCheckBoxToggled event is the best place to handle that. Then manually
select the items you want.
The only true wisdom is knowing you know nothing

mijen67

  • Full Member
  • ***
  • Posts: 130
  • It's hard to beat the bandwidth of a flying DVD
Re: Stringgrid checkbox with select-only-one functionality
« Reply #3 on: October 14, 2017, 07:58:41 pm »
1) doesn't stringgrid support multiselect? (haven't used for so long I can't remember)

It does, but this doesn't mean it supports "select one only".

May I ask why you don't use stringgrid? Is there something better for GUIs?

2)to code a multi select ability you create a list of selected rows when the user checks the box you add the row to the list when the boxgets uncheckd you remove the row from the list. you do not keep the state of the selection in the grid / data you simple paint a checked box if the row is in the checked list, otherwise you paint an unchecked box. That is in my experience the less prone to errors method.

So basically the answer is: there is no automatic settings that handles this, do it by programming.

Thanks!

mijen67

  • Full Member
  • ***
  • Posts: 130
  • It's hard to beat the bandwidth of a flying DVD
Re: Stringgrid checkbox with select-only-one functionality
« Reply #4 on: October 14, 2017, 07:59:27 pm »
The OnCheckBoxToggled event is the best place to handle that. Then manually
select the items you want.

So basically the answer is: there is no automatic settings that handles this, do it by programming.

Thanks!

munair

  • Hero Member
  • *****
  • Posts: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: Stringgrid checkbox with select-only-one functionality
« Reply #5 on: October 16, 2017, 09:32:24 pm »
The OnCheckBoxToggled event is the best place to handle that. Then manually
select the items you want.

So basically the answer is: there is no automatic settings that handles this, do it by programming.

Thanks!
How would you imagine this could be done automatically? The programmer must tell what happens when a user event occurs and code the appropriate event handler to make it happen. See it as a challenge to make it work just the way you imagine. Be creative! I promise you, it is fun and quite rewarding. Some programmers come up with ideas other would never have thought of. RAD already makes it very easy to quickly write great software. There must be something left for the programmer to do.  8)
keep it simple

 

TinyPortal © 2005-2018