Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FAQ on centering a checkbox in a grid column?

Status
Not open for further replies.

BPeisch

Programmer
Oct 19, 2003
464
US
Am I just being dense or blind (wouldn't be the first time) or is there no FAQ on centering a checkbox in a grid column? If there really isn't one, I can go ahead and write one up.



-BP
 
Barbara

Am I just being dense or blind (wouldn't be the first time) or is there no FAQ on centering a checkbox in a grid column? If there really isn't one, I can go ahead and write one up.

I cannot see one either, go for it. :)

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Barbara,

Please do make that FAQ. Will this be for VFP 8 only or have you got a trick for earlier versions of VFP as well?

Slighthaze = NULL
craig1442@mchsi.com
"Whom computers would destroy, they must first drive mad." - Anon​
 
The only way I've done it is to create a borderless container class c/w checkbox, reposition the checkbox within the container, and then drop the container into the column.

You can also add text to the left or right of the checkbox within the container if it suits to 'wrap' the checkbox, and centre it if required..

FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
The technique I've used covers all version of VFP (well, I haven't tested in 3 or 5). It's for situations where you have a checkbox with no caption, and a column header that's wider than the checkbox. It the same as Chris's technique where you use a container and calculate the Left property of the checkbox at runtime, based on the width of the column.

I know others who recommend using graphical style checkboxes instead in this situation, but I've found that unsatisfactory because a. the height of the button is cut off with the default RowHeight and b. it's not as obvious to users that the button is something that you click to select that row.

As a bonus in VFP 8 only, I also use Bindevent to have the checkbox re-center itself when you resize the column.

I've been spending a lot of time with client this week, and I'm not sure how soon I can get to it, but I will get it done eventually.



-BP
 
Barbara

My post was not meant in any way to be a 'spoiler' for your FAQ, and if it seemed that way, please accept my apologies.

Your FAQ, and any others you contribute, will be more than welcome as you can gather from this thread. [smile]

FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
My post was not meant in any way to be a 'spoiler' for your FAQ, and if it seemed that way, please accept my apologies.

I didn't view your message as a spoiler at all. No apologies necessary. In fact, maybe you've found a better solution to one thing I'm doing that bugs me.

If you check the width of a checkbox with no caption, it's something like 60, even though the actual width of the box itself is more like 18. In part of my calculation, I need to subtract the checkbox width/2, but because the width is not returning an accurate number, I'm hardcoding the width.

Have you found a way around this that doesn't require hardcoding the width?



-BP
 
Barbara

First set .AutoSize = .F., remove the caption and make the width to be 14, (should be 13, but tick mark is too small).

There seems to be a pixel difference between left and right alignment at width of 14, so you may need to adjust again if it's critical.

To centre it in the container, width to be determined by column width, code would be, in .Init(), .Resize() or whatever of container :-

WITH THIS
[tab].Check1.Left = (.Width /2) - (.Check1.Width /2)
ENDW


FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
HI

This has always worked out correctly for me. May be 1 pixel this way or that, but that is negligible for viewing.

WITH Grid1.....Container1.Check1
.AutoSize = .f.
.Width = .Parent.Width
.Caption = cText
.AutoSize = .t.
.Left = MAX(((.Parent.Width-.Width)/2),1)
ENDWITH

By setting the width to the containers width, which in turn is the width of the column object.., word wrap taking place is avoided.

:)


____________________________________________
ramani - (Subramanian.G) :)
When you ask VFP questions, please add VFP version.
 
Thanks guys. The Autosize = .F. was the key part I was missing for getting the correct width. You would think that with Autosize = .T. and no caption it would return the correct width...but Noooooo! ;-)



-BP
 
Hey guys,

I'm not going to write up an FAQ on this. I've found that in a plain vanilla grid, setting the columns Alignment property to Middle Centered is adequate for centering the checkbox, with or without a caption. I just need to make sure the checkbox's AutoSize property is .F.

Now I just need to figure out why this still isn't working in the particular app where I need it. (Although my workaround with the container and a lot of code does work.)



-BP
 
Sorry, Barbara, I don't see that.

This is something I've been trying to do for years (literally), and have always assumed it's not possible. I just did what you suggested, and still can't see it.

Here is my code (without all irrelevant stuff eliminated):

Code:
DEFINE CLASS form1 AS form


	ADD OBJECT grdproducts AS grid WITH ;
		ColumnCount = 3, ;
           Column3.Alignment = 2, ;  && "middle centered"

		Column3.ControlSource = "products.discontinu", ;
		Column3.Sparse = .F., ;

	ADD OBJECT form1.grdproducts.column3.header1 AS header WITH ;
		Caption = "discontinu", ;
		Name = "Header1"


	ADD OBJECT form1.grdproducts.column3.check1 AS checkbox WITH ;
		Top = 76, ;
		Left = 15, ;
		Height = 17, ;
		Width = 60, ;
		AutoSize = .T., ;
		Alignment = 0, ;
		Centered = .T., ;
		Caption = "", ;
		Name = "Check1"


ENDDEFINE

In VFP 7.0, when I try to set the column's alignment to "middle centered", I get an "illegal value" message (at design time). In VFP 8.0, I don't get that message, but the check box is still left-aligned.

Any ideas on what's happening?

Mike


Mike Lewis
Edinburgh, Scotland
 
Mike,

Have you tried a new form with a plain vanilla grid and checkbox? It worked for me in that situation. But here's the technique I'm using in my app where that doesn't work:

Create a container class, and place a checkbox in it. Make sure the checkbox's AutoSize property is .F. Add a method to the container class called RepositionCheckbox with this code:
Code:
* Center the checkbox within the height and width of the column
this.chkbase1.Left = (this.Parent.Width/2) - (this.chkbase1.width/2) + 2
IF this.chkbase1.Left < 0
   this.chkbase1.Left = 0
ENDIF 
this.chkbase1.Top = (this.Parent.Parent.RowHeight/2) - (this.chkbase1.Height/2)
IF this.chkbase1.Top < 0
   this.chkbase1.Top = 0
ENDIF
In the Init and Resize methods of the container, add a call to RepositionCheckbox(). In the Init of the Checkbox, put:
Code:
this.ControlSource = this.Parent.Parent.ControlSource
If you're in VFP 8, you can bind the column's resize to code that calls the container's resize. In my grid's Init, I have this code:
Code:
FOR EACH oColumn IN This.Columns
   BINDEVENT(oColumn,&quot;Resize&quot;,Thisform.oEventHandler,&quot;colResize&quot;)
ENDFOR
The eventhandler class looks like this:
Code:
DEFINE CLASS SystemEventHandler as Session
   PROCEDURE colResize
      LOCAL loCalledby

      AEVENTS(aCurEvent,0)
      loCalledBy = aCurEvent[1]
		
      IF PEMSTATUS(loCalledBy,&quot;cntChkGrid1&quot;,5)
         loCalledBy.cntChkGrid1.Resize()
      ENDIF 
      Return .T.
   ENDPROC 
ENDDEFINE




-BP
 
Barbara,

Very curious. I followed your instructions exactly. I got the same result as you: no centering. (I also set the column's Sparse to .F., but that presumably is not an issue.)

When I created the class, I just dropped the checkbox in the container without regard to its position. It turned out to be at the left side, with just a little space to the right. So I then tried manually centering the checkbox within the container (at the class level). After that, the centering worked fine.

But it shouldn't have been necessary for me to do that, because I am calling the RepositionCheckbox from the Init of the container. The event tracking tool showed me that the Init was correctly being called (of course), and the trace window showed the RepositionCheckbox code being executed.

So ... it's got me baffled. It's late now ... maybe I'll take another look in the morning.

Mike


Mike Lewis
Edinburgh, Scotland
 
HI

Sorry, I had not followed this thread earlier.

The way to go is..

Assuming, you have added a container and a checkbox within the conatainer.. say in Column2..

WITH ThisForm.grdAcmas.Column2.Container1
WITH .Check1
.Width = .Parent.Parent.Width
.AutoSize = .f.
.Caption = &quot;Yes/No&quot;
.AutoSize = .t.
.Visible = .t.
** Visible is the key to get the correct size
** returned before the calculation is done.
.Left = (.Parent.Parent.Width - .Width)/2
ENDWITH
ENDWITH

This works correctly for me. If it is not working, let me know, I will post a full sample form code. :)

____________________________________________
ramani - (Subramanian.G) :)
When you ask VFP questions, please add VFP version.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top