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!

date textbox

Status
Not open for further replies.

newtofoxpro

Programmer
Sep 16, 2007
301
0
16
IN
oForm=CREATEOBJECT("myForm")
oForm.show()
READ EVENTS
RETURN

DEFINE CLASS myForm as Form
visible=.T.
ADD OBJECT mDate as textbox WITH top=10, Left=10
ADD OBJECT mCode as textbox WITH top=50, Left=10
PROCEDURE Init
this.mDate.StrictDateEntry=0
this.mDate.Value=DATE()
this.mDate.InputMask="99-99-9999"
PROCEDURE mDate.GotFocus
KEYBOARD '{CTRL+A}'
PROCEDURE mDate.interactivechange
WAIT WINDOW NOWAIT this.value
ENDDEFINE

With Above Code example.
1. If I enter [1] , wait wind shows empty mdate.

2. If I enter [1-8] , wait wind shows 01-08-2015

year = 2015 how vfp auto complete it ? why 2015 and why not 2014

And if year auto-feed by vfp, how to auto-feed month & year.

If I enter [2] vfp should auto-feed 02-08-2015 Is it possible ?

Thanks




 
It is unclear what you are trying to achieve.

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Please run example code

In first textbox, enter/type 1
wait wind shows empty mdate textbox
and if you enter/type 1-8
wait wind shows 01-08-2015

I want to know how vfp auto-feed year 2015 ?
and if year can be auto-feed, is it possible to auto-feed month & year both ?

Thank you for reply post.
 
Hi,

see StrictDateEntry=0
MSDN said:
If the year is omitted from a date, the current year is used for the date
There is no such automatism for the month. You could do something of your own by adding MONTH(DATE()) if the displaytext has no month yet.

Another thing: Don't do KEYBOARD {CTRL+A}, there is Format="K" for that behaviour.

See Format Property
MSDN said:
K | Selects all the text when the control gets the focus

Bye, Olaf.
 
A simple thing you could do for speed entering of dates:
SET CONFIRM OFF
Three Textboxes
Two with InputFormat "99"
One with InputFormat "9999"

Input will automatically jump to the next textbox because of SET CONFIRM OFF.
Accept only valid overall values by checking whether DATE(text3.value,text2.value,text1.value) (pseudocode, need to convert each parameter to number) converts to a valid date, eg in TRY..CATCH or whether you get no empty date via CTOD() (which is depending on date settings, though) or you get a non empty date via EVAL("{^YYYY-MM-DD}") (which is date setting independent = good).

Bye, Olaf.
 
What you are seeing is entirely consistent with what you would expect to see, as documented in the Help topic for StrictDateEntry. Spend a moment reading that topic, and it should all be clear.

And be aware that, when StrictDateEntry is 0, the behaviour depends on your SET DATE setting, and several other factors.

Personally, when I want the user to enter a valid date, I simply bind the textbox to a date field, or initialise it to a date variable, and then trap any "invalid date" error that is generated (usually Error 2034). Alternatively, use a date picker control (but that raises different problems of its own).

If you want to auto-generate the month or day, you will need to write your own logic to do that. VFP will default the year to the current year, but that's as far as it is prepared to go.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
I suggest you to follow the previous advices, because this is the best way to handle data type fields or variables.
But if you are stubborn (please forgive my expression, it's only for fun), you can use the Text property of the textbox in the LostFocus event of the textbox

Something like that :
Code:
	Procedure mDate.lostfocus
		IF EMPTY(This.Value)
			IF EMPTY(CHRTRAN(This.Text,'0123456756','')) AND !EMPTY(This.Text) && only one number
				IF BETWEEN(VAL(This.text),1,DAY(GOMONTH(DATE(YEAR(DATE()),MONTH(DATE()),1),1) - 1))
					This.Value = DATE(YEAR(DATE()),MONTH(DATE()),VAL(This.text))
				ENDIF
			ENDIF
		ENDIF
	ENDPROC

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
Another suggestion: As you init the textbox with today via this.mDate.Value=DATE(), you can simply skip KEYBOARD '{CTRL+A}' and Format="K" to NOT select the date.
Then users will overwrite the day portion and can tab to the next control. So simply NOT selecting the content of the control you can enter just the day to have a full date of current month and year.

I just tried that and it reveals the next issue: Since you init with a date type value and use inputmask - which only works for string values - you have a type conflict problem. You also better not set StrictDateEntry=0.

This version of your code works much easier:
Code:
oForm=CREATEOBJECT("myForm")
 oForm.show()
 READ EVENTS
 RETURN

 DEFINE CLASS myForm as Form
 visible=.T.
 ADD OBJECT mDate as textbox WITH top=10, Left=10
 ADD OBJECT mCode as textbox WITH top=50, Left=10
 PROCEDURE Init
 this.mDate.StrictDateEntry=1
 this.mDate.DateFormat=11 && DMY
 this.mDate.DateMark="-"
 this.mDate.Value=Date()
 PROCEDURE mDate.GotFocus

 PROCEDURE mDate.interactivechange
 WAIT WINDOW NOWAIT this.value
 ENDDEFINE

Now when you overwrite the day portion the value will be a correct date, since you don't clear the month and year until you overwrite them.

Bye, Olaf.
 
vgulielmus, Sorry, I was not aware of "this.text"

Code:
oForm=CREATEOBJECT("myForm")
oForm.show()
READ EVENTS
RETURN

DEFINE CLASS myForm as Form
visible=.T.
   ADD OBJECT mDate as textbox WITH top=10, Left=10
   ADD OBJECT mCode as textbox WITH top=50, Left=10
   PROCEDURE Init
      this.mDate.StrictDateEntry=0
      this.mDate.Value=DATE()
      this.mDate.InputMask="99-99-9999"
      this.mDate.Format="K"
   PROCEDURE mDate.LostFocus
      IF LEN(ALLTRIM(this.text))<=2
         this.value=CTOD(ALLTRIM(this.text)+SUBSTR(DTOC(DATE()),3))
      ENDIF
      IF !EMPTY(PADR(this.text,2)) AND EMPTY(this.Value)
         this.value={}
         NODEFAULT
      ENDIF
ENDDEFINE

Thank you all.
 
I would rather opt for my last solution, as StrictDateEntry=1 (default) actually insures you get a valid date and makes your own intervention unnecessary.

Bye, Olaf.
 
Olaf, Does your code works only for day for you ? Because its not working for me.
For Example
With your code : Type only day = 05 and press enter
With my code : Type only day = 05 and press enter

Anyway, My last solution works for me as I desired.
Thank you.
 
Yes, it works for me just typing the day. Have you tried it at all?

Today: 05-08-2015 is default value
type 1: textbox is 15-08-2015, wait window displays 08/15/2015
type 1: textbox is 11-08-2015, wait window displays 08/11/2015

What's not working for you?
Simply copy the full code 1:1 and run it.
Notice, that I don't set the inputmask and don't set the format nor need specific code for GotFocus, InteractiveChange or Valid.

Bye, Olaf.
 
Today: 05-08-2015 is default value

I want 03-08-2015

type 3 and press <Enter>

Try with yours & mine code.
 
That was your concern? "its not working for me." sounded more like it doesn't work at all.
Typing 3 into your form you only see 3 until leaving. I think of that as less good UX, in the end it's your choice.

If you want easy keyboard oriented change from the current date to some days earlier or later you could use a spinner control or use the textbox.keypress:
Code:
   Procedure mDate.KeyPress
      Lparameters nKeyCode, nShiftAltCtrl
      Do Case
            * up arrow +1d
         Case nKeyCode=5  And nShiftAltCtrl=0
            This.Value = This.Value+1
            Nodefault
            * down arrow -1d
         Case nKeyCode=24 And nShiftAltCtrl=0
            This.Value = This.Value-1
            Nodefault
            * shift+up +1m
         Case nKeyCode=56 And nShiftAltCtrl=1
            This.Value = Gomonth(This.Value,1)
            Nodefault
            * shift+down -1m
         Case nKeyCode=50 And nShiftAltCtrl=1
            This.Value = Gomonth(This.Value,-1)
            Nodefault
            * ctrl+shift+up +1y
         Case nKeyCode=145 And nShiftAltCtrl=3
            This.Value = Gomonth(This.Value,12)
            Nodefault
            * ctrl+shift+down -1y
         Case nKeyCode=141 And nShiftAltCtrl=3
            This.Value = Gomonth(This.Value,-12)
            Nodefault
      Endcase

Bye, Olaf.
 

There is <+> and <-> keys already there. Pls try.

Thanks for your help.

vgulielmus, thanks I visited posted link.
 
Good, something I didn't know yet.

I think this is done now, isn't it?
I would just like you to be more specific about your needs, if you can't live with the need to type 03 instead of 3, then that's that. I can't stand it.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top