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

Use Option Group Or Switch To Buttons? 1

Status
Not open for further replies.

wrgt9740

Programmer
Dec 5, 2020
35
PH
Hi. I am currently having problems with my option group.

My form has a grid, a start date textbox, an end date textbox, a run button, and an option group.

The grid that displays a table with date, invoice, and item as fields.
The textboxes are where a user enters the date range of records to be displayed.
The button is to display the records within the date entered.
The option group has 3 choices: date, invoice, and item. The purpose is to sort the table by among the 3 choices, with a change in background color. For example, if a user clicks the option date, then the table will be sorted by date, and the background color of the date field will change to gray. If the user clicks the option invoice, then the sort will change by invoice, and the background color of the date field will turn back to white while the invoice field will turn to gray. Note: table is initially sorted by date after clicking run button.

At first run, the table is sorted by date with date field color at gray. If I click on the option invoice to change the sort and color, then run again, the table is sorted by date, but the colors of both date and invoice are gray, and the option invoice is still selected. Is there a way to reset the option group, or perhaps I should switch to buttons?
 
Not quite clear what the problem is. You said:

If I click on the option invoice to change the sort and color, then run again, the table is sorted by date,

Is that really what you meant? If you really select the Invoice option, but that results in the table being sorted on the Date column, that indicates that there is something wrong with the sorting procedure rather than with the option group.

the option invoice is still selected

Do you mean that, when you click on the Invoice option and then sort the grid, the Invoice option is still selected? If so, that's exactly what you would expect.

Perhaps you could review the last paragraph in your post, and explain the problem a bit more clearly.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
At first run, the table is sorted by date with date field color at gray. If I click on the option invoice to change the sort and color, then run again, the table is sorted by date, but the colors of both date and invoice are gray, and the option invoice is still selected. Is there a way to reset the option group, or perhaps I should switch to buttons?

1. When you click on the option group, irrespective of which one is selected, reset the background color of all three columns Date, Invoice & Item to normal. Then, according to the selected option, change the background of respective column to Gray. This will prevent the previously selected column's background also being shown as gray.

2. When you click on Invoice but the sort is on Date, it means that the sorting routine you run when you click the option group has some problem. It appears, the sorting routine of Invoice has the same code that of Date, maybe a copy-paste problem. Please check that.

Rajesh
 
Rajesh,

not sure if i have understood correctly but....

are you are testing the option group's value within the interactivechange event and then changing the background colour accordingly?

If so, move your code to the .valid event. I've had this before where the new value isn't being seen by .interactivechange but is seen within .valid.

n
 
Or, another option ... move the code to the Click event of the column header. From the user's point of view, this will be more intuitive: you click on a column header to sort the grid on the relevant column. It's also more in line with the way many other windows applications and utilities work (such as Windows Explorer in Detail view). And by removing the option group, you reduce clutter on your form.

Going further, to give the user the required feedback, rather than changing the colour of the relevant column, you could place a small icon in the column's header (in Grid1.Column1.Header1.Picture, for example). Again, this is consistent with other Windows applications.

And going further still, let them click once to sort into ascending order, and to click on the same column header again to sort into descending order. And, when you do, you can change the icon from (say) an upward arrow to a downward arrow.

These are all fairly standard procedures.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Hi everyone. I'm very sorry for not being clear. This is what should happen:

When the form opens, the user will enter start date and end date in their respective textboxes, then click run button. The grid will display records within date entered, sorted by date, and the date field in gray color. If the user clicks on the option invoice, a black dot will appear beside the option invoice, the records will be sorted by invoice, the date field color turn back to white, and invoice field change to gray color. If the user changes the date in the textboxes and clicks the run button, then the grid will display a new set of records, sorted by date, invoice field color change back to white, and date field color to gray.

The problem is when the user clicks on the option invoice, then changes the date in textboxes and clicks run button, a new set of records appear, sorted by date, both the date field and invoice field color as gray, and the black dot beside the option invoice still present. The gray color in invoice field won't disappear even if I entered at the start of run button
column.backcolor = RGB(255,255,255)

I've already found the solution: include at the start of run button
thisform.optiongroup.value = 0
to reset the option group and remove the gray color of any fields.

But Mike Lewis' suggestion of entering code to the headers instead seems to be a better solution, and I have to admit the form currently has a lot of clutter. I'll have to read more on how to change the sort order between ascending and descending.

Thank you everyone, for your invaluable help.

Update: I forgot to put in the run button the code to change the colors back to white before doing the sorting thing. I'm terribly sorry for creating confusion and inconvenience to everyone because of my simple mistake. I'm still pushing through with Mike Lewis' suggestion, though.
 
That's much clearer now. However, since you are now considering using the column's headers, I won't try to work out what was going wrong with the option group.

Regarding the switch between ascending and descending, this is quite easy. You just add the keyword ASCENDING or DESCENDING to the ORDER BY command, as appropriate.

So, assuming you already have indexes on the date and the invoice number, your code will look something like the following:

First, you will need to add a custom property to the form or (preferably) the grid, to indicate the current direction (ascending or descending) of the date column; and another to serve the same purpose for the invoice column. Let's call those lDateAscending and lInvoiceAscending respectively. Set them initially to .T., to indicate ascending order.

Now, in the Click event of the header of the date column, do something like this:

Code:
SELECT TheTable   && change this to the relevant alias
IF thisform.Grid1.lDateAscending 
  SET ORDER TO DateField ASCENDING
  this.Picture = ascending.ico   && name of image or icon to indicate ascending sequence
ELSE
  SET ORDER TO DateField DESCENDING
  this.Picture = descending.ico   && ditto for descending sequence
ENDIF
thisform.Grid1.lDateAscending = NOT thisform.Grid1.lDateAscending
thisform.Grid1.SetFocus   && this is needed to make the change visible

And similar code in the Click event of the invoice column, but change the name of the field as appropriate, and change lDateAscending to lInvoiceAscending.

I haven't tested the above, but it should give you the general idea.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Hi,

Below the code of a fully working demo to sort the data by clicking the column header with the header specific ToolTipText

Code:
PUBLIC goForm

goForm = CREATEOBJECT("MyForm")
goForm.Show()

READ EVENTS

CLOSE ALL
CLEAR ALL 

**********

DEFINE CLASS MyForm as Form
	Width = 540
	Height = 360
	MinWidth = This.Width
	MinHeight = This.Height
	MaxWidth = This.Width
	Caption = "Sorting data in grid"
	AutoCenter = .T.
	ShowTips = .T.
	Themes = .F.


	ADD OBJECT grdNames as grdBase WITH ;
		RecordSource = "csrNames"
		
		PROCEDURE grdNames.Init()
			WITH This
				.Column1.Header1.Caption = "N°"
				.Column2.Header1.Caption = "Name"
				.Column3.Header1.Caption = "Age"
				.Column4.Header1.Caption = "G°"
				.Column5.Header1.Caption = "City"
				
			ENDWITH 
			
		ENDPROC 
		
		PROCEDURE grdNames.Refresh()
			LOCAL lcDBClick as String, lcClick as String
			
			lcDBClick = "DoubleClick header to reset order"
			lcClick = "Click header to sort by "
			
			DODEFAULT()

			WITH This
				.Column1.Header1.ToolTipText = IIF(ORDER() = "TNUMBER", lcDBClick, lcClick + "Number")
				.Column2.Header1.ToolTipText = IIF(ORDER() = "TNAME", lcDBClick, lcClick + "Name")
				.Column3.Header1.ToolTipText = IIF(ORDER() = "TAGE", lcDBClick, lcClick + "Age")
				.Column4.Header1.ToolTipText = "Sorting by Gender not available"
				.Column5.Header1.ToolTipText = IIF(ORDER() = "TCITY", lcDBClick, lcClick + "City")
				
			ENDWITH 
		
		ENDPROC 
	
	PROCEDURE Load()
		CREATE CURSOR csrNames (iRNumber I,cName C(20), iAge I, cGender C(1), cCity C(20))
		
			INSERT INTO csrNames VALUES (3,'Sahra', 31, "F", "Bruxelles")
			INSERT INTO csrNames VALUES (4,'Jeoffrey', 20, "M", "Paris")
			INSERT INTO csrNames VALUES (6,'Jenny', 53, "F", "Den Haag")
			INSERT INTO csrNames VALUES (1,'Toni', 44, "M", "Rome")
			INSERT INTO csrNames VALUES (2,'Sophie', 76, "F", "Berlin")
			INSERT INTO csrNames VALUES (7,'Jeremy', 67, "M", "New York")
			INSERT INTO csrNames VALUES (8,'John', 19, "M", "Chicago")
			INSERT INTO csrNames VALUES (9,'Marie-Josée', 28, "F", "Quebec")
			INSERT INTO csrNames VALUES (10,'Karen', 62, "F", "Toronto")
			INSERT INTO csrNames VALUES (11,'Abi', 56, "M", "Zurich")
			INSERT INTO csrNames VALUES (12,'Bernhard', 42, "M", "Basel")
			INSERT INTO csrNames VALUES (13,'Christiane', 26, "F", "Marseille")
			INSERT INTO csrNames VALUES (14,'Doris', 29, "F", "Munich")
			INSERT INTO csrNames VALUES (15,'Fred', 58, "M", "Wien")
			INSERT INTO csrNames VALUES (16,'Georges', 40, "M", "Budapest")
			INSERT INTO csrNames VALUES (17,'Juanita', 36, "F", "Mexico")
			INSERT INTO csrNames VALUES (18,'Jane', 52, "F", "Los Angeles")
			INSERT INTO csrNames VALUES (19,'Jan', 57, "M", "Amsterdam")
			INSERT INTO csrNames VALUES (20,'Henrietta', 41, "F", "Baton Rouge")
			INSERT INTO csrNames VALUES (5,'Mark', 54, "M", "Bern")
			
		INDEX on iRNumber TAG TNumber
		INDEX on cName TAG TName
		INDEX on iAge TAG TAge
		INDEX on cCity TAG TCity
		
		SET ORDER TO
		
		LOCATE
		 
	ENDPROC

	PROCEDURE Init()
		DODEFAULT() 
		This.DoBinds()
		
	ENDPROC
	
	PROCEDURE DoBinds()
		LOCAL loCol, loObj

		FOR EACH loCol IN This.grdNames.Columns
			IF loCol.BaseClass = "Column"
				UNBINDEVENTS(loCol)
				BINDEVENT(loCol,"MouseMove", This,"GetColumnToolTipText")
			ENDIF 
						
			FOR EACH loObj IN loCol.Objects
				IF loObj.Baseclass == "Header"
					UNBINDEVENTS(loObj)
					BINDEVENT(loObj,"Click",This,"HeaderClick")
					BINDEVENT(loObj,"DblClick",This,"HeaderTwiceClick")
					BINDEVENT(loObj,"MouseMove", This,"GetHeaderToolTipText")

				ENDIF
			NEXT 
		NEXT
		
	ENDPROC

	PROCEDURE HeaderClick()
		LOCAL ARRAY laEvents[1]
		LOCAL lcOrderBy, loObject
		
		AEVENTS(laEvents, 0)

*!*			= MESSAGEBOX("Clicked: " + SYS(1272, laEvents[1]), 64, "Clicked Object", 1500)
			
		loObject = laEvents[1]

		lcOrderBy = SUBSTR(RIGHT(SYS(1272, loObject),9),1,1)

		IF INLIST(lcOrderBy, "1", "2", "3", "5")
			WITH This.grdNames
				.SetAll("FontBold", .F., "Header")
				.SetAll("FontItalic", .F., "Header")
				.SetAll("BackColor", RGB(228, 228, 228), "Header")
			ENDWITH 

			WITH loObject
				.FontBold = .T.
				.FontItalic = .T.
				.Backcolor = RGB(0, 201, 201)
			ENDWITH 
		ENDIF 

		DO CASE 
			CASE lcOrderBy = "1"
				SET ORDER TO TNumber
					
			CASE lcOrderBy = "2"
				SET ORDER TO TName
					
			CASE lcOrderBy = "3"
				SET ORDER TO TAge
				
			CASE lcOrderBy = "4"
				= MESSAGEBOX("Sorting by Gender is not available", 64, "Setting Order", 2000)

			CASE lcOrderBy = "5"
				SET ORDER TO TCity

			OTHERWISE
				= MESSAGEBOX("Call developper", 16, "Setting Order", 5000)

		ENDCASE

		LOCATE  
		This.Refresh()
		
	ENDPROC

	PROCEDURE HeaderTwiceClick()
			WITH This.grdNames
				.SetAll("FontBold", .F., "Header")
				.SetAll("FontItalic", .F., "Header")
				.SetAll("BackColor", RGB(228, 228, 228), "Header")
			ENDWITH 
			
			SET ORDER TO
			LOCATE
			This.Refresh()

	ENDPROC 

	PROCEDURE GetHeaderToolTipText(nButton, nShift, nXCoord, nYCoord)
		LOCAL laEvents[1]
		LOCAL loObject
		
		AEVENTS(laEvents,0)
		
		loObject = laEvents[1]
		
		This.grdNames.cToolTipText = loObject.ToolTipText
		
	ENDPROC
	
*!*	The procedure below AVOIDS the TTT to show when the mouse is hovering over columns	
	
	PROCEDURE GetColumnToolTipText(nButton, nShift, nXCoord, nYCoord)
*!*			LOCAL laEvents[1]
*!*			LOCAL loObject
*!*			
*!*			AEVENTS(laEvents,0)
*!*			
*!*			loObject = laEvents[1]
*!*			
*!*			This.grdNames.cToolTipText = loObject.Name

		This.grdNames.cToolTipText = ""
		
	ENDPROC
	
	PROCEDURE Destroy()
		ThisForm.Release()
		CLEAR EVENTS

	ENDPROC
ENDDEFINE 

DEFINE CLASS grdBase as Grid 
	cToolTipText = ""

	Top = 6
	Left = 6
	Width = 540 - 12
	Height = 360 - 12
	Anchor = 15
	BackColor = RGB(0, 246, 246)
	ColumnCount = -1
	
	PROCEDURE ToolTipText_Access()
		RETURN This.cToolTipText
	ENDPROC

ENDDEFINE 

**********

Enjoy

marK
 
Mike Lewis, thank you very much for your help. I'll try to implement your ascending-descending suggestion.

Mjcmkrsr, your code looks very complicated, but i'll try it out. I'm curious to see how it will run.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top