Page 1 of 3

Check for valid

Posted: Wed Sep 13, 2017 6:33 pm
by ROBROS
Hello to all,

once again I am asking for your help. The user should key in via 2 Textbox controls month and year. With inputmask and ON LostFocus and ON ENTER
the valid input is checked, works. See the following lines of code:

BTW: How can I put these lines of code into a kind of frame in which you can scroll up and down, as I have seen so often?

Code: Select all

#include <hmg.ch>

Function Main

local cMon, cYear

set navigation extended

DEFINE WINDOW Form_1 ; 
AT 90,90 ; 
WIDTH 400 ; 
HEIGHT 250 ; 
TITLE "Year/Month" ; 
MAIN 

@ 20,20 LABEL lbl1 ; 
PARENT Form_1 ; 
VALUE "Monat 2-stellig" //month 2 digits



@ 20,150 LABEL lbl2 ; 
PARENT Form_1 ; 
VALUE "Jahr 4-stellig" //year 4 digits

@ 40,20 TEXTBOX Text_1 ;
parent Form_1;
value cMon;
inputmask "99" On LostFocus chkmon() On Enter chkmon()


@ 40,150 TEXTBOX Text_2; 
PARENT Form_1;
value cYear;
inputmask "9999" On LostFocus chkYear() On Enter chkyear()

@ 150,20 LABEL lbl3 value 'Ende mit F-10';
     FONTCOLOR BLUE

on key F10 action fill_MonYear()

END WINDOW

ACTIVATE WINDOW Form_1 
close databases

RETURN NIL

function fill_MonYear
SetProperty('Form_1','text_1','Value',GetProperty('Form_1','Text_1','Value'))
cMon:=GetProperty('Form_1','Text_1','Value')
SetProperty('Form_1','text_2','Value',GetProperty('Form_1','Text_2','Value'))
cYear:=GetProperty('Form_1','Text_2','Value')
pend()
return

function pend
MsgInfo( cMon+'/'+cYear)
release Window Form_1 
return


function chkmon
use mon // this table contains all valid values for cMon
index on mon to mon
seek Form_1.Text_1.value
if !found()
   MsgInfo('Only 01 - 12 allowed')
   //Form_1.Text_1.value:="  "
   Form_1.Text_1.SetFocus
endif
return

function chkyear
if val(Form_1.Text_2.value)>year(date())
   MsgInfo('Only current or former years allowed')
   //Form_1.Text_2.Value:="    "
   Form_1.Text_2.SetFocus
endif
return 

But in case the input is incorrect, MsgInfo() shows up twice :?

Who can explain that?

Kind Regards
Robert

Fun with programming HMG is increasing. :D

Re: Check for valid

Posted: Wed Sep 13, 2017 6:55 pm
by Rathinagiri
BTW: How can I put these lines of code into a kind of frame in which you can scroll up and down, as I have seen so often?

For that you have to use BBCode (which is the standard HTML like tags used in forums.)

For example,

Code: Select all

[code]This is code.[/ code]
Don't forget to remove the space between '/' and 'code'

Re: Check for valid

Posted: Wed Sep 13, 2017 7:04 pm
by Rathinagiri
It is run twice because when you show the msginfo() window, the lostfocus event is also fired as the focus is lost.

Try to use 'disable control event ' and 'enable control event' before and after msginfo() and it will run only once as you expected.

Re: Check for valid

Posted: Wed Sep 13, 2017 7:37 pm
by ROBROS
Rathinagiri wrote: Wed Sep 13, 2017 7:04 pm It is run twice because when you show the msginfo() window, the lostfocus event is also fired as the focus is lost.

Try to use 'disable control event ' and 'enable control event' before and after msginfo() and it will run only once as you expected.
Thank you, for formatting my lines of code, next time I will use BBCode.

As I don't know how to enable or disable control event, I just left out the 'ON ENTER', because by pressing ENTER focus is lost and by moving with the mouse to the year field, focus is also lost. It works.
So you could say, it was "over-checked" :lol:

Robert

Re: Check for valid

Posted: Wed Sep 13, 2017 7:49 pm
by Rathinagiri
Actually ON ENTER works only for "ENTER" or "TAB" key press. In other words, it covers only keyboard events. However, if the user uses mouse to move the focus between the textboxes (by clicking directly on the other textbox) you can see that 'ON ENTER' event is not fired. So, it is better to add validation using 'ON LOSTFOCUS' as it will be definitely fired whether the user uses mouse or keyboard. :)

Re: Check for valid

Posted: Wed Sep 13, 2017 8:01 pm
by ROBROS
Rathinagiri wrote: Wed Sep 13, 2017 7:04 pm It is run twice because when you show the msginfo() window, the lostfocus event is also fired as the focus is lost.

Try to use 'disable control event ' and 'enable control event' before and after msginfo() and it will run only once as you expected.
Exactly this answer was making me use my brain. And by trial and error I found out, that lostfocus was the solution.

I like hmg. :D

Re: Check for valid

Posted: Thu Sep 14, 2017 5:20 am
by mol
It's better to validate whole form if it's possible.
If you will validate control on lost focus, you must detect if user want to leave data without saving it, eg. by ESCAPE key, or [Cancel] button.

Re: Check for valid

Posted: Thu Sep 14, 2017 6:14 am
by ROBROS
Marek, you are right,
therefore user can leave the form by entering F-10.

Re: Check for valid

Posted: Thu Sep 14, 2017 7:53 am
by Rathinagiri
I do second Marek and it is the way of programming in GUI, I do.

Unless one input is dependent on another input, the validation can be made at the point of 'Save'. Because of this, number one, we consolidate the validation procedure into one and number two, the user can use any mode of navigation.

Re: Check for valid

Posted: Thu Sep 14, 2017 8:32 am
by ROBROS
That means, validation should be done within function fill_MonYear(), right?