Board index » cppbuilder » Newbie question: how to get and validate a value on screen form

Newbie question: how to get and validate a value on screen form


2004-02-20 12:27:52 AM
cppbuilder75
Hi All,
Lately I join a project which uses Borland C++ Builder with VCL components. We write scientific application with a lot of screen forms filled with float values. It is a rule that all of these floats have meaningfull range and values must be checked against own range. Each range violation should be reported to the user. That has number of consequences which are boring to enumerate. Let's call functionality of checking/validation/reporting etc. as "book keeping".
I notice that I litter the code with book keeping code at a place where I need to use the value from a screen form. Attempt to write single function for book keeping falls short due to number of reasons. And one of them is that other programmers just do not know where to put that book keeping code.
Actually, for many years I use Visual C++ and MFC. There is a virtual DoDataExchange function provided which is one place to do that book keeping, namely:
1. exchange the data between screen forms and memory and back
2. provides data validation
3. error reporting
I miss this functionality in VCL. I understand that there is no exactly the same functionality but my trouble that I can't find anything equivalent. Could someone point to me where or how I can get anything similar with Borland.
To make it clear, this book keeping function DoDataExchangde is demonstrated in the code snippet below:
------------------- cut here ------------------
class MyDlg : public CDialog
{
public:
double m_x;
// .. all the rest
protected:
virtual void DoDataExchange(CDataExchange* pDX);//DDX/DDV support
};
void MyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, m_v);
DDV_MinMaxDouble(pDX, m_v, 1.0, 10.0);
}
void MyDlg::OnAction()
{
if ( !UpdateData( true ) ) // get data from screen
return; // attempt to get data from screen has failed - exit
m_v = SomeFunc( m_v * 2.5 ); // just use m_v
UpdateData( false ); // send data back to screen
}
------------------- cut here ------------------
Another question is I can't find the way to link just a double or an int to an item on screen form. I'm forced to use TEdit mediator and I have to conver its "Text" property to a value. Using of the TRzNumericEdit from Raize components is not better - I have to use its "Value" property. But my wish is very simple - I want a double to go to edit control and back.
If you understand the problem and know the answer - please, share your knowledge. I'm not interested in discussion what is better MFC or VCL, Borland or Microsoft, capitalism or socialism. I have a problem and I'm looking for a solution.
Regards,
Alexander
 
 

Re:Newbie question: how to get and validate a value on screen form

"Alexander" < XXXX@XXXXX.COM >wrote in message news:4034e408$ XXXX@XXXXX.COM ...
Quote

Hi All,

Lately I join a project which uses Borland C++ Builder with VCL components. We write scientific
application with a lot of screen forms filled with float values. It is a rule that all of these
floats have meaningfull range and values must be checked against own range. Each range violation
should be reported to the user. That has number of consequences which are boring to enumerate. Let's
call functionality of checking/validation/reporting etc. as "book keeping".
Quote

I notice that I litter the code with book keeping code at a place where I need to use the value
from a screen form. Attempt to write single function for book keeping falls short due to number of
reasons. And one of them is that other programmers just do not know where to put that book keeping
code.
I'm not sure if this will work for you, but I have a lot of IO config forms
provided for imputing set values etc. The approach that I've taken
is to put the TEdit's into vectors. When a user clicks "save" I just
itrerate through the vectors. In some cases, I need to validate
that input values are within engineering unit ranges. In this case,
I use a small structure with a TEdit, float minEU and float maxEU.
Again, I can iterate the containers easily. Maybe something like
this could help?
 

Re:Newbie question: how to get and validate a value on screen form

Alexander wrote:
A lot of vey long unwrapped lines.
Please wrap your text manually, so to give the opportunity to
reply selective.
You did not tell how floats ant ints came on the TForm ?
Just for reading ? Or can the user type in values in TEdit's ?
If typing in TEdits use the TEdit OnChange event to check
if a value is a float/double int.
You could also be moere precise in what you mean with
"the way to link just a double or an int to an item on screen".
If you have a double variable then there are several ways
to make a textual representations for it. If you
have the text then you can decide if you want
to put that in a file or in a TLabel a TEdit a TMemo and
so on. Or use TextOut to draw it to a Canvas.
Please tell what really has to be done.
Hans.
 

{smallsort}

Re:Newbie question: how to get and validate a value on screen form

Hans Galema < XXXX@XXXXX.COM >wrote:
Quote
Alexander wrote:

A lot of vey long unwrapped lines.

Please wrap your text manually, so to give the opportunity to
reply selective.
At the memoment of writing, my lines were wrapped ;-( Anyway,
I repost my whole mail here with prefix "|"
|
|Hi All,
|
|Lately I join a project which uses Borland C++ Builder with
|VCL components. We write scientific application with a lot of
|screen forms filled with float values. It is a rule that all
|of these floats have meaningfull range and values must be
|checked against own range. Each range violation should be
|reported to the user. That has number of consequences which
|are boring to enumerate. Let's call functionality of
|checking/validation/reporting etc. as "book keeping".
|
|I notice that I litter the code with book keeping code at a
|place where I need to use the value from a screen form.
|Attempt to write single function for book keeping falls short
|due to number of reasons. And one of them is that other
|programmers just do not know where to put that book keeping
|code.
|
|Actually, for many years I use Visual C++ and MFC. There is a
|virtual DoDataExchange function provided which is one place
|to do that book keeping, namely:
|
|1. exchange the data between screen forms and memory and back
|2. provides data validation
|3. error reporting
|
|I miss this functionality in VCL. I understand that there is
|no exactly the same functionality but my trouble that I can't
|find anything equivalent. Could someone point to me where or
|how I can get anything similar with Borland.
|
|To make it clear, this book keeping function DoDataExchangde
|is demonstrated in the code snippet below:
|
|------------------- cut here ------------------
|class MyDlg : public CDialog
|{
|public:
| double m_v;
| // .. all the rest
|
|protected:
| virtual void DoDataExchange(CDataExchange* pDX);//DDX/DDV support
|};
|
|void MyDlg::DoDataExchange(CDataExchange* pDX)
|{
| CDialog::DoDataExchange(pDX);
| DDX_Text(pDX, IDC_EDIT1, m_v);
| DDV_MinMaxDouble(pDX, m_v, 1.0, 10.0);
|}
|
|void MyDlg::OnAction()
|{
| if ( !UpdateData( true ) ) // get data from screen
| return; // attempt to get data from screen has failed - exit
|
| m_v = SomeFunc( m_v * 2.5 ); // just use m_v
|
| UpdateData( false ); // send data back to screen
|}
|------------------- cut here ------------------
|
|Another question is I can't find the way to link just a
|double or an int to an item on screen form. I'm forced to use
|TEdit mediator and I have to conver its "Text" property to a
|value. Using of the TRzNumericEdit from Raize components is
|not better - I have to use its "Value" property. But my wish
|is very simple - I want a double to go to edit control and
|back.
|
|If you understand the problem and know the answer - please,
|share your knowledge. I'm not interested in discussion what
|is better MFC or VCL, Borland or Microsoft, capitalism or
|socialism. I have a problem and I'm looking for a solution.
|
|Regards,
|Alexander
|
Quote

You did not tell how floats ant ints came on the TForm ?
Just for reading ? Or can the user type in values in TEdit's ?
Does it matter from where my values came from? In my partical
case, usually they were from some configuration files or they
are results of calculations, which should be shown to the user.
Quote

If typing in TEdits use the TEdit OnChange event to check
if a value is a float/double int.

OnChange event is generated if user has changed one character
in the edit box. Right? That's too early - user did not finish
yet editing the value.
Did you mean OnExit event? That's possible. But that's exactly
what I do not want - if my form has 10 floats then you advise
me to write 10 OnExit event handlers? By the way, did you know
OnExit event is not guaranteed to come?
Quote
You could also be moere precise in what you mean with
"the way to link just a double or an int to an item on
screen".
I have a member variable m_v of type double which should be
shown on screen form in edit control edit1 of type TEdit.
My code above demonstrates how I do that with MFC: "double
m_v" is shown on screen form in edit box "IDC_EDIT1" and it is
validated to be in range from 1.0 to 10. If user enters not a
number or number outside of the range from 1 to 10, DDX/DDV
routines will report to user about it. The call to UpdateData
will return "false".
Does Borland has anythink similar or I have to do that myself?
Do you know any third party library which provide similar
functionality?
Alexander
 

Re:Newbie question: how to get and validate a value on screen form

Hi, Alexander,
Quote
I miss this functionality in VCL.
As I know it does not exist. I use own component - simple extension
of TEdit with additional properties:
Type (int or float),
Precision (symbols after decimal point when show your value),
RangeType (positive, interval, nolimited etc),
Value + some more, including range setting/checking.
If check fails the control is focused.
I am sure you can find free third party components with
more functionality.
Bye,
Alexander (another one)
 

Re:Newbie question: how to get and validate a value on screen form

Alexander wrote:
Quote
Does it matter from where my values came from?
Yes. If you place them you could check the values before placing.
If the user types in then check during type in.
Quote
In my partical
case, usually they were from some configuration files or they
are results of calculations, which should be shown to the user.
If it is from textfiles you had to check them before placing.
If they were the result of calculations you had to check the range
to.
Quote
OnChange event is generated if user has changed one character
in the edit box. Right? That's too early - user did not finish
yet editing the value.
In that event you can check if the ReturnKey was pressed.
Quote
Did you mean OnExit event? That's possible. But that's exactly
what I do not want - if my form has 10 floats then you advise
me to write 10 OnExit event handlers?
No. Of course not. You can assign the same eventhandler to
a lot of TEdit's. In the IDE and by code.
Quote
By the way, did you know
OnExit event is not guaranteed to come?
No. When it does not ? In the last TEdit where the user does not
press Tab or Return ?
Quote
My code above demonstrates how I do that with MFC: "double
m_v" is shown on screen form in edit box "IDC_EDIT1" and it is
validated to be in range from 1.0 to 10. If user enters not a
number or number outside of the range from 1 to 10, DDX/DDV
routines will report to user about it. The call to UpdateData
will return "false".

Does Borland has anythink similar or I have to do that myself?
float myfloat = StrToFloat ( Edit1->Text );
// I leave the error checking to you
if ( myfloat>= 1.0 && myfloat <= 10.0 )
{
// pass
}
Quote
Do you know any third party library which provide similar
functionality?
Why do you need a library for three lines of code ?
Hans.
 

Re:Newbie question: how to get and validate a value on screen form

Hans Galema < XXXX@XXXXX.COM >wrote:
Quote
Alexander wrote:

>Does it matter from where my values came from?

Yes. If you place them you could check the values before placing.
If the user types in then check during type in.

>In my partical
>case, usually they were from some configuration files or they
>are results of calculations, which should be shown to the user.

If it is from textfiles you had to check them before placing.
If they were the result of calculations you had to check the range
to.
Well, I could do a range checking before loading the values,
but I never did. If the range values are somehow linked to a
control and to a variable, then I will just dump values to
controls and validation will happen on loading values from
screen.
Another question, assume that some value is out of range. Then
what should happen? And what if there is more than one
variable is out of range?
Quote

>OnChange event is generated if user has changed one character
>in the edit box. Right? That's too early - user did not finish
>yet editing the value.

In that event you can check if the ReturnKey was pressed.
You forget to mention TabKey. And what about accelerators? In
my many years experience of Windows programming I never wrote
WM_KEYDOWN message handlers - handcrafted code for this
message is too brittle.
Quote

>Did you mean OnExit event? That's possible. But that's exactly
>what I do not want - if my form has 10 floats then you advise
>me to write 10 OnExit event handlers?

No. Of course not. You can assign the same eventhandler to
a lot of TEdit's. In the IDE and by code.
Yes, that's right. I can assign 1 event handler to 10
controls. That means, 1 range hardcoded inside event handler
will be applied to 10 variables. Do you want it?
Quote

>By the way, did you know
>OnExit event is not guaranteed to come?

No. When it does not ? In the last TEdit where the user does not
press Tab or Return ?
If user presses accelerator key. For instance, if you have a button "&Apply", and user pressed Alt-A, OnExit event for active edit box does not come. The same is valid for Alt-F4.
Quote

>My code above demonstrates how I do that with MFC: "double
>m_v" is shown on screen form in edit box "IDC_EDIT1" and it is
>validated to be in range from 1.0 to 10. If user enters not a
>number or number outside of the range from 1 to 10, DDX/DDV
>routines will report to user about it. The call to UpdateData
>will return "false".
>
>Does Borland has anythink similar or I have to do that myself?

float myfloat = StrToFloat ( Edit1->Text );
// I leave the error checking to you

if ( myfloat>= 1.0 && myfloat <= 10.0 )
{
// pass
}

>Do you know any third party library which provide similar
>functionality?

Why do you need a library for three lines of code ?
Because in my form I has to write those 3 lines of code many times. About once per variable. And I have many forms.
Alexander
 

Re:Newbie question: how to get and validate a value on screen form

You could have a look at orpheus from turbo-power, which is now open-source
and available at sourceforge.net/projects/tporpheus/
It contains a lot of controls for data-entry and validation. Unfortunately I
have no personally experience with the validation controls but the
documentation is quite good. The installation can be quite tricky because a
license block must be removed from nearly every file. But more information
about installation can be found in the forum (at least when I installed it).
Palle
 

Re:Newbie question: how to get and validate a value on screen form

Hi All,
As far as I understand, required funcitonality "as is" is not available in VCL. That's great that there is a lot of components with formatting and validation. But I have to tie screen form, controls and variables together myself.
Anyway, I thank everyone who has found the time for a thought.
Regards,
Alexander