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

Entering a date into a Textbox 1

Status
Not open for further replies.

AndrewMozley

Programmer
Oct 15, 2005
621
1
18
GB
I have a data-entry text box. When it has focus it shows the date in dd-mm-yy format
date1_hr4j0i.png

So the user can perhaps key in new values

And when it does not have focus it shows as :
date2_pb8xrr.png


To that end it has a PUBLIC variable - zValueD - of type DATE, (which is not itself the value of the control). So the application can perhaps assign a value to this variable; this causes the textbox to be refreshed with the new date.

The code for the class looks more or less like this.
Code:
DEFINE CLASS amdate AS amtxt   && a subclass of Textbox
	Alignment = 3
	Value = {}
	zvalued = .F.
	Name = "amdate"

	PROCEDURE zvalued_assign
		LPARAMETERS vNewVal
		THIS.zvalued = m.vNewVal
		This.Value = FormatDate(This.zValueD)
	ENDPROC

	PROCEDURE Valid
		LOCAL lValue
		WITH This
		   lValue = This.Value
		   .zvalueD = CTOD(lValue)
		   This.Value = lValue
		   IF EMPTY(.zValueD)
		      MESSAGEBOX("Invalid date")
		      RETURN .F.
		      ENDIF
		   ENDWITH
		RETURN DODEFAULT()
	ENDPROC

	PROCEDURE LostFocus
		WITH This
		   .InputMask = "XXXXXXXXX"
		   .zvalueD = CTOD(.Value)
		   .Value = FormatDate(.zValueD)  && so : 17-Jul-22
		   ENDWITH
	ENDPROC

	PROCEDURE GotFocus
		WITH This
		   .Value = DTOC(.zValueD)
		   .InputMask = "99-99-99"
		   ENDWITH
	ENDPROC

	PROCEDURE Init
		IF EMPTY(This.zValueD)
		   This.zValueD = DATE()
		   ENDIF
		RETURN DODEFAULT()
	ENDPROC
ENDDEFINE

Any suggestions for improvement? Thanks - Andrew
 
Hi Andrew,

Is this of any help?

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
Height = 300
MinHeight = 300
MinButton = .F.
MaxButton = .F.
Themes = .F.

	ADD OBJECT lblSDate as Label WITH Left = 84, Top = 12, Caption = "Startdate"
	ADD OBJECT lblEDate as Label WITH Left = 186, Top = 12, Caption = "Today"

	ADD OBJECT txtSDate as textbox WITH Top = 36, Left = 84, Width = 90
	ADD OBJECT txtEDate as textbox WITH Top = 36, Left = 186 , Width = 90
	
	PROCEDURE txtSDate.LostFocus()
		LOCAL ldDate
		
		ldDate = This.Value

		This.Value = ""
		This.Value = STR(DAY(ldDate),2) +" "+ SUBSTR(CMONTH(ldDate),1,3) +" "+ STR(YEAR(ldDate),4)

		WAIT WINDOW + VARTYPE(This.Value) NOWAIT 

	ENDPROC   
	
	PROCEDURE txtSDate.GotFocus()
		LOCAL lcMonth, lcThisDate
		 
		IF VARTYPE(This.Value) = "C"
		
			lcMonth = SUBSTR(This.Value, 4, 3)
			lcMonth = ICASE(lcMonth = "Jan", "01", lcMonth = "Feb", "02", lcMonth = "Mar", "03", lcMonth = "Apr", "04", ;
							lcMonth = "Jul", "07", lcMonth = "Aug", "08", "12") && aso
							
			lcThisDate = SUBSTR(This.Value, 1, 2) + "/" + lcMonth + "/" + RIGHT(This.Value, 4)

			This.Value = {}
			This.Value = CTOD(lcThisDate)

		ENDIF 
		
		WAIT WINDOW + VARTYPE(This.Value) NOWAIT 
		
	ENDPROC   

	PROCEDURE Init()
		This.txtSDate.Value = DATE()
		This.txtEDate.Value = DATE()
	ENDPROC
	

	PROCEDURE Destroy()
		CLEAR EVENTS
	
	ENDPROC 
	
   	
ENDDEFINE 
**********
 
Hello--

I too have a date-entry class. In my version, I allow the user to enter input with the up/down/left/right arrow keys. The left/right arrow keys moves the selection to the day, month, or year. The up/down arrow keys increase or decrease the selected value. If the user is selected on the day and increases the day value beyond the current month end, then the month is automatically increased and the day value reset; same for month (if past 12, then month is set to 1 and year increased. The opposite is true for decreasing the day or month value.

Greg
 
Vernspace's post is worth considering.

Nowadays, users expect always to be able to use a date picker when entering dates. These are now so common on websites that users would find it strange to have to type a date manually. Like many VFP developers, I created my own date picker some years ago and have often used it in my applications. It is probably not as good as Craig Boyd's, but definitely better than the ActiveX control that comes with VFP.

That said, I think the use a date picker should always be optional. Some users (myself included) prefer to simply type the date.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Yes, it's nice to have options - users like options. That's why the date picker class allows either manual date entry OR the calendar dropdown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top