Center a checkbox in a DataGrid Table Column

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
aetaylorBUSBnWt
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 118
Joined: Thu Sep 20, 2012 5:11 pm

Center a checkbox in a DataGrid Table Column

Post by aetaylorBUSBnWt » Mon Oct 17, 2022 7:41 pm

Want a checkbox in a column in a DataGrid Table

- do NOT want a title on it - that is what the column Header is for.
So the size of the checkbox icon is no bigger than is necessary to show the checkbox itself.

The checkbox shows up on the left edge of the column.

So I put the following statement :

set the dgColumnAlignment["clnResident"] of group "fCondoList" to "center"

in the code that I use to load the data into the DataGrid sets the property and it shows up when I look at the Align property for that column in the Property Inspector.
Checkbox is still left aligned.

When editing the Row Template, I selected the checkbox on the Editing Template form and tried to drag out the left side so that the checkbox would get moved right. That does not work.

neither does the following in the button column behavior

Code: Select all

on LayoutControl pControlRect    
    set the margins of me to 8,2,0,2
end LayoutControl
So what is the magic to center a checkbox in a DataGrid Table Column?

Oh, and how do I get rid of the DataGrid trying to name the Checkbox?
The only way I found so far is to make the size of the checkbox too small for any text to be printed to the right side of the graphic.

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Center a checkbox in a DataGrid Table Column

Post by stam » Tue Oct 18, 2022 12:12 am

aetaylorBUSBnWt wrote:
Mon Oct 17, 2022 7:41 pm
So what is the magic to center a checkbox in a DataGrid Table Column?
Assuming you've created the behaviour script for the column (and hence your column template):
1. As you don't want a title on the checkbox, set the 'show name' property to false and resize it so the 'tick' graphic is in the middle of the rect (assuming you edit the row template group to access that column's row templet group and then edit that group as well to add and edit a tickbox button)
2. In the behaviour script's layoutControl:

Code: Select all

on LayoutControl pControlRect    
     set the loc of btn "check" of me to the loc of me
end LayoutControl
HTH
Stam

aetaylorBUSBnWt
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 118
Joined: Thu Sep 20, 2012 5:11 pm

Re: Center a checkbox in a DataGrid Table Column

Post by aetaylorBUSBnWt » Tue Oct 18, 2022 12:48 am

Serious magic!, but it works!

As far as resizing goes I did not find that changing the size for the checkbox from what I had that was only showing the checkbox.
But turning off show name (I never got down that far in the template editing often enough) was huge.

What I don't understand is that statement for setting the location of the button in LayoutControl.

Why does that center the checkbox button?
Thx!

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Center a checkbox in a DataGrid Table Column

Post by stam » Tue Oct 18, 2022 1:32 am

'loc' is the coordinate x,y pair for the centre of an object. Anything that has a rect has a loc.
By setting the loc of one object to the loc of another object you basically centre them.
This is fairly unusual in programming languages and LC is the only language i know of that implements this (most other languages use topLeft or some such, which LC also has) - for me it's one of the many conveniences of the language that makes things surprisingly easy with much less code than other languages.

Normally in other languages, to centre the smaller object 2 over the larger object one, you'd need to individually calculate the top/left based on height/widths, eg

Code: Select all

top = (height2 - height1)/2
left = (width2 - width1)/2
whereas in LC you just write

Code: Select all

set the loc of object2 to the loc of object1 
loc is useful in other ways too - say you want to position an object in the middle of the top edge of a larger object:

Code: Select all

set the loc of obj2 to item 1 of the loc of obj1, the top of obj1
-- magic, courtesy of LiveCode ;)

aetaylorBUSBnWt
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 118
Joined: Thu Sep 20, 2012 5:11 pm

Re: Center a checkbox in a DataGrid Table Column

Post by aetaylorBUSBnWt » Tue Oct 18, 2022 3:15 pm

How to put in a Checkbox into a Data Grid Table

and then center it

and it has no name.

First:

Do the documented process for creating a Data Grid and create your columns, including the one that is going to be a checkbox.

Once you have completed the Data Grid creation:

Go back to the Property Inspector and select the column you want to put a checkbox into.

1. Click on the '+' sign next to Column Behavior to create a column Behavior.
(do NOT subsequently click on the column behavior that shows up in the window)

2. Now click on the Data Grid tab in the Property Inspector.

3. Down at the bottom, click on Row Template.

4. This presents a window named Editing Template.
You will see the name of the column that you want to be a checkbox.
Make sure that "Select Grouped" item in the Toolbar is off.
Click on the column that you want to be a checkbox.
The entire area that is the "Row Template" will be "selected".

5. Now click on the Edit Group item in the Toolbar
(it will change its name to "Stop Editing")

6. Now click on the column item in the Editing Template window that you want to convert to a checkbox.
This time, that column item will actually be selected.
Delete it.

7. Drag a Checkbox in from the UI Palette or pick Checkbox from the Object Menu / New Control,
Placing it on the Editing Template window.

8. Click on the new Checkbox control.
You should see in the Property Inspector window that the title is now "Button Check".

IF the title on the Property Inspector window is NOT correct, make sure that the new CheckBox is selected, you might have to click on "Edit Group" again. But you need the correct title on the property Inspector before moving forward.

Change your focus to the Property Inspector.
In the Basic tab, change the Name back to the column name for the Checkbox.
If you want a Label, put it in - I did not.
If you do NOT want a label, click on "Show Name" to uncheck it.
Put in whatever Tooltip you want.
(there may be other things you want to change in the property inspector, that affect the button, do them)
(you also may be able to change the width of the Checkbox here to properly encompass what you want shown.)
(do NOT do SCRIPT things at this point!!!)

9. Change your focus back to the Editing Template window.
Change the size of the Checkbox so that it is properly sized. So for no title, just make it big enough for the Check box.

10. Click "Stop Editing" in the Toolbar.
(this stops editing of the templates)

11. Now you need that "Editing Template" window to go away.
Either close it, in which case you will need to save your stack.
Or save the stack and then close the window.

12. NOW we get into setting the Script for the Checkbox.
You will need a "Column Behavior" Script for this Checkbox button and getting such is not obvious (at least to me). If you don't put your script code into the proper place, it is never executed.

Open the Project Browser.
Expand the display of the stack you are working on (click on "+" sign)
You should see a "Data Grid Template" entry.
Expand it.
There are a number of cards displayed now - one for each Data Grid - and one more)
You will need to investigate each of the cards until you find one with the Column with the name of the CheckBox you just added.

Once you find the correct card, select the entry that is titled "checkbox name Behavior".

13. Control click on that Behavior entry and "Edit Script"
You will need to modify several of the commands / messages in this script.
FillInData, LayoutControl, ResetData.
Delete EditValue and mouseDoubleUp
Add mouseUp

Below is a possible set of methods for a checkbox:

In the following code, you only need to change the name of the column for the checkbox.

This code "centers" the location of the Checkbox in its column.

(Thanks to stam for that code and how to shut off the name of a Checkbox)

You might just completely replace all the methods in the "default" code with the following.
Double check that there has not been new features since I wrote this.

Code: Select all

-- This script defines the behavior of a table column's custom template.

on FillInData pData
    -- This message is sent when the Data Grid needs to populate
    -- this template with the column data. pData is the value to be displayed.
    
    -- Example:
    set the hilited of button 1 of me to (pData = "true")
end FillInData


on LayoutControl pControlRect    
    -- This message is sent when you should layout your template's controls if your template is a group.
    -- If your template is not a group then there is no work to do here.
    -- You can use items 1 through 4 of pControlRect as boundaries for laying out your controls.
    
    -- The following code CENTERS THE CHECKBOX IN THE COLUMN
    set the loc of btn 1 of me to the loc of me
end LayoutControl


on ResetData
    -- Sent when column data is being emptied because the control is no longer being used to display data
    set the hilite of button 1 of me to false
end ResetData


on PreFillInData
    -- Sent right before new data is going to replace existing data in the column
end PreFillInData


setprop dgHilite pBoolean
    -- This custom property is set when the highlight of your column template has
    -- changed. You only add script here if you want to customize the highlight.
    
    if pBoolean then
        set the foregroundColor of me to the dgProp["hilited text color"] of the dgControl of me
    else
        set the foregroundColor of me to empty
    end if
end dgHilite


getprop dgDataControl
    -- Required by library so that it can locate your control.
    return the long ID of me
end dgDataControl


-- Sent when the mouse is released after clicking
-- pMouseButton specifies which mouse button was pressed
on mouseUp pMouseButton
    SetDataOfLine the dgLine of me, "NAME OF CHECKBOX COLUMN", the hilite of the target
end mouseUp


aetaylorBUSBnWt
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 118
Joined: Thu Sep 20, 2012 5:11 pm

Post by aetaylorBUSBnWt » Tue Oct 18, 2022 3:27 pm

Duplicate!
Last edited by aetaylorBUSBnWt on Tue Oct 18, 2022 7:01 pm, edited 1 time in total.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9842
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Center a checkbox in a DataGrid Table Column

Post by FourthWorld » Tue Oct 18, 2022 4:23 pm

Duplicate thread merged. Please create only one thread for a given topic, so current users know where to post replies, and future users can find more complete info on one place.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”