Check for valid

Topic Specific Tutorials and Tips.

Moderator: Rathinagiri

ROBROS
Posts: 61
Joined: Thu May 25, 2017 6:30 pm
DBs Used: DBF
Has thanked: 32 times
Been thanked: 1 time

Check for valid

Post by ROBROS » Wed Sep 13, 2017 6:33 pm

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

User avatar
Rathinagiri
Posts: 5172
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Has thanked: 136 times
Been thanked: 131 times
Contact:

Post by Rathinagiri » Wed Sep 13, 2017 6:55 pm

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'
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.

User avatar
Rathinagiri
Posts: 5172
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Has thanked: 136 times
Been thanked: 131 times
Contact:

Post by Rathinagiri » 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.
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.

ROBROS
Posts: 61
Joined: Thu May 25, 2017 6:30 pm
DBs Used: DBF
Has thanked: 32 times
Been thanked: 1 time

Post by ROBROS » Wed Sep 13, 2017 7:37 pm

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

User avatar
Rathinagiri
Posts: 5172
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Has thanked: 136 times
Been thanked: 131 times
Contact:

Post by Rathinagiri » Wed Sep 13, 2017 7:49 pm

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. :)
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.

ROBROS
Posts: 61
Joined: Thu May 25, 2017 6:30 pm
DBs Used: DBF
Has thanked: 32 times
Been thanked: 1 time

Post by ROBROS » Wed Sep 13, 2017 8:01 pm

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

User avatar
mol
Posts: 2853
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Has thanked: 124 times
Been thanked: 65 times
Contact:

Post by mol » Thu Sep 14, 2017 5:20 am

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.
Last edited by mol on Thu Sep 14, 2017 10:53 am, edited 1 time in total.

ROBROS
Posts: 61
Joined: Thu May 25, 2017 6:30 pm
DBs Used: DBF
Has thanked: 32 times
Been thanked: 1 time

Post by ROBROS » Thu Sep 14, 2017 6:14 am

Marek, you are right,
therefore user can leave the form by entering F-10.

User avatar
Rathinagiri
Posts: 5172
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Has thanked: 136 times
Been thanked: 131 times
Contact:

Post by Rathinagiri » Thu Sep 14, 2017 7:53 am

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.
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.

ROBROS
Posts: 61
Joined: Thu May 25, 2017 6:30 pm
DBs Used: DBF
Has thanked: 32 times
Been thanked: 1 time

Post by ROBROS » Thu Sep 14, 2017 8:32 am

That means, validation should be done within function fill_MonYear(), right?

Post Reply