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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkbox in a grid

Status
Not open for further replies.

Bryan - Gendev

Programmer
Jan 9, 2011
408
AU
I have a form with a programmed grid on it based on a cursor with a logical field DoDel.
I want to have a checkbox based on this field in my grid. My added chkbox control displays correctly but when I click in it a previous form opens. My code segments are
Code:
With This.Column5
		.Header1.Caption = 'Del'
		.Header1.BackColor= Rgb(255,255,255)
		.Header1.ForeColor = Rgb(255,0,0)
		.Header1.FontBold = .T.
		.ControlSource = 'mycursor.DODEL'	
		.BackColor= Rgb(255,255,255)
		.ForeColor = Rgb(255,0,0)
		.AddObject('Check1','checkbox')	
		.currentcontrol = 'Check1'
		.Visible  = .T.
		.Sparse = .F.
		.Width = 18
 endwith

and my control Check1
Code:
DEFINE CLASS Check1 AS CHECKBOX
HEIGHT = 17
WIDTH = 18
AUTOSIZE = .T.
CAPTION = ""
NAME = "Check1"

PROCEDURE CLICK
SET STEP ON
IF DODEFAULT()
   KEYBOARD '{DNARROW}'
ENDIF
ENDPROC
ENDDEF

If I can get this working correctly I will strikeout the row and ultimately delete all rows that are .T.
Any suggestions please?

GenDev
 
You say this opens another form. But there is nothing in the code that you showed that opens any form.

Also, you've have DODEFAULT() for the Click of the checkbox, but that doesn't make sense because, in this case, the parent of your checkbox is the native Checkbox class.

Also, rather than KEYBOARD '{DNARROW}', it might be better simply to SKIP IN <underlying cursor>. Then again, I'm not clear exactly what you are trying to achieve here, so maybe you have a good reason for doing it this way.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Hi,

Please have a look at the code snippet below.

Code:
goForm = CREATEOBJECT("frmForm")
goForm.Visible = .T.
goForm.Show()

READ EVENTS

CLOSE ALL
CLEAR ALL 

RETURN

**********

DEFINE CLASS frmForm as Form

AutoCenter = .T.
BorderStyle = 2
Width = 540
MinWidth = 540
MinButton = .F.
MaxButton = .F.
ShowTips = .T.
Themes = .F.

*!*	Add the visual elements

	ADD OBJECT lblCHKAll as Label WITH Left = 12, Top = 12, Caption = "All"
	ADD OBJECT lblCHKGA as Label WITH Left = 84, Top = 12, Caption = "Grade A"
	ADD OBJECT lblCHKGB as Label WITH Left = 156, Top = 12, Caption = "Grade B"
	ADD OBJECT lblCHKGC as Label WITH Left = 228, Top = 12, Caption = "Grade C"

	ADD OBJECT chkAll as CheckBox WITH Top = 36, Left = 12, AutoSize = .T., Value = .T., Caption = "All"
	ADD OBJECT chkGrA as CheckBox WITH Top = 36, Left = 84, AutoSize = .T., Value = .F., Caption = "Grade A"
	ADD OBJECT chkGrB as Checkbox WITH Top = 36, Left = 156, AutoSize = .T., Value = .F., Caption = "Grade B"
	ADD OBJECT chkGrC as Checkbox WITH Top = 36, Left = 228, AutoSize = .T., Value = .F., Caption = "Grade C"

	ADD OBJECT cmdFilter as CommandButton WITH ;
		Left = 426, ;
		Top = 12, ;
		Height = 42, ;
		BackColor = RGB(0, 255, 255), ;
		Caption = "Show Selection"

	ADD OBJECT grdSchool as Grid WITH ;
		Left = 12, ;
		Top = 60, ;
		Width = ThisForm.Width - 24, ;
		Height = ThisForm.Height - 84, ;
		DeleteMark = .F., ;
		Visible = .F.
		
*!*	Now the procedures:
*!*	Uncheck all other checkboxes if ALL is checked

		PROCEDURE chkAll.Click() 
			FOR i = 1 TO ThisForm.ControlCount
				IF AT(UPPER("chkGr"), ThisForm.Controls(i).Name) != 0
					
					ThisForm.Controls(i).Value = .F.
					
				ENDIF
			ENDFOR
		ENDPROC 
		
*!*	Uncheck the ALL checkbox if you check any other checkbox

		PROCEDURE chkGrA.Click()
			ThisForm.CheckBoxClick()
		ENDPROC
		
		PROCEDURE chkGrB.Click()
			ThisForm.CheckBoxClick()
		ENDPROC

		PROCEDURE chkGrC.Click()
			ThisForm.CheckBoxClick()
		ENDPROC
		
*!*	Filtering the data and showing the results
			
		PROCEDURE cmdFilter.Click()
			LOCAL lcFilter as Character
			lcFilter = ""
				
			IF ThisForm.chkAll.Value = .T.
				SELECT *, CAST(.T. as L) as lCheck FROM csrSchool ORDER BY 2, 1 INTO CURSOR csrFilter READWRITE 
					
			ELSE 

				IF ThisForm.chkGrA.Value = .T.
					lcFilter = "csrSchool.cGrade = [A] OR "
				ENDIF

				IF ThisForm.chkGrB.Value = .T.
					lcFilter = lcFilter + "csrSchool.cGrade = [B] OR "
				ENDIF

				IF ThisForm.chkGrC.Value = .T.
					lcFilter = lcFilter + "csrSchool.cGrade = [C]"
				ENDIF

				IF RIGHT(lcFilter, 3) = "OR "
					lcFilter = Substr(lcFilter, 1, Len(lcFilter) - 4)
				ENDIF 

				IF EMPTY(lcFilter)
					= MESSAGEBOX("Please check at least one option", 64, "Show selected data", 3000)
					
				ELSE 
					SELECT *, CAST(.T. as L) as lCheck FROM csrSchool WHERE &lcFilter ORDER BY 2, 1 INTO CURSOR csrFilter READWRITE 

				ENDIF 
			ENDIF 
			
			IF ALIAS() = UPPER("csrFilter")
				WITH ThisForm.grdSchool
*!*						.BackColor = RGB(0, 255, 255)
					.ColumnCount = -1
					.RecordSource = "csrFilter"
					.Visible = .T.

					WITH .Column1
						.Header1.Caption = "Name"
						.Width = 90
						.ReadOnly = .T.
					ENDWITH

					WITH .Column2
						.Header1.Caption = "Grade"
						.Width = 60
						.ReadOnly = .T.
					ENDWITH

					WITH .Column3
						.Header1.Caption = "Click"
						.AddObject('chkBox','chkMyBox')	
						.currentcontrol = 'chkBox'
						.chkBox.Visible  = .T.
						.Sparse = .F.
						.Width = 30
					ENDWITH

				ENDWITH 			
			ENDIF
		ENDPROC 
				
	PROCEDURE Load()
	
		CREATE CURSOR csrSchool (cName C(10), cGrade C(1))
		
		INSERT INTO csrSchool VALUES ("Mandy", "A")
		INSERT INTO csrSchool VALUES ("Jeff", "B")
		INSERT INTO csrSchool VALUES ("Chris", "C")
		INSERT INTO csrSchool VALUES ("Sandy", "B")
		INSERT INTO csrSchool VALUES ("Mickey", "C")
		INSERT INTO csrSchool VALUES ("Paolo", "B")
		INSERT INTO csrSchool VALUES ("Paula", "A")
		
		LOCATE 
		
	ENDPROC 
	
	PROCEDURE Destroy()
		ThisForm.Release()
		CLEAR EVENTS
	
	ENDPROC 

	PROCEDURE CheckBoxClick()
		ThisForm.chkAll.Value = .F.
	ENDPROC 
	
ENDDEFINE 
**********

DEFINE CLASS chkMyBox AS CHECKBOX
	CAPTION = ""
	ReadOnly = .F.
	Enabled = .T.

	PROCEDURE CLICK
		DODEFAULT()
		KEYBOARD '{DNARROW}'
	ENDPROC
ENDDEFINE

hth

MarK
 
mjcmkrsr,

Thanks for that - the chkbox operates exactly as I want and would expect.
Now to find out why mine does not.
I have added your code for the checkbox but the first clck into one on my grid causes the form to display - then after closing no other clicks cause anything to happen.
Control does not pass to the checkbox control when clicked and I see nothing in the debugger except the line that the old form is raised on.

GenDev
 
Mike,

My form with the grid on it is run from another form with a cmd button to get it going.
When I 'Click' in the chkbox in my grid the debugger does not reach the set step on and the previous form is displayed over the top of the grid.
This is my problem.
I am asking is something wrong with the code segments I show for that column in the grid and the ctrl define?
I feel that vfp is not recognising the checkbox control thus just jumping out of the prg holding the grid.
So I have changed the placement of the control define within the prg but that has not helped.

Thanks
GenDev
 
Hi GenDev,

Please check the code in your column5

With This.Column5
.Header1.Caption = 'Del'
.Header1.BackColor= Rgb(255,255,255)
.Header1.ForeColor = Rgb(255,0,0)
.Header1.FontBold = .T.
.ControlSource = 'mycursor.DODEL'
.BackColor= Rgb(255,255,255)
.ForeColor = Rgb(255,0,0)
[highlight #73D216] .AddObject('Check1','checkbox') && you need to add a checkbox based on YOUR class - not the base class e.g. .AddObject("chkBox", "Check1")
.currentcontrol = 'Check1'&& .CurrentControl = "chkBox" [/highlight]
.Visible = .T.
.Sparse = .F.
.Width = 18
endwith

hth

MarK

 
Thanks Mark,
I cut your code in and now it's working perfectly.

GenDev
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top