Home » U++ Library support » LineEdit, EditFields, DocEdit » Is there a way to make EditDouble return 0 if it has no value?
Is there a way to make EditDouble return 0 if it has no value? [message #27596] |
Sun, 25 July 2010 10:13  |
MatthiasG
Messages: 27 Registered: January 2008 Location: Germany
|
Promising Member |
|
|
Hello,
as written in the topic, is there a way to let EditDouble return 0.0 for a field without a value in it? e.g. if i have two EditDouble fields d1 and d2, with d1 containing 1.0 and d2 has empty value, i want double d = d1 + d2 to return 1.0...
greetings,
Matthias
|
|
|
Re: Is there a way to make EditDouble return 0 if it has no value? [message #27598 is a reply to message #27596] |
Sun, 25 July 2010 13:01   |
|
MatthiasG wrote on Sun, 25 July 2010 10:13 | Hello,
as written in the topic, is there a way to let EditDouble return 0.0 for a field without a value in it? e.g. if i have two EditDouble fields d1 and d2, with d1 containing 1.0 and d2 has empty value, i want double d = d1 + d2 to return 1.0...
greetings,
Matthias
|
Hi Matthias,
I usually solve this by prohibiting empty field: editfield.NotNull(true), also can be set in layout designer. But sometimes this solution might annoy user.
Another option is to check before using the value, just adding something like d1=IsNull(d1)?0:d1 in you code should be enough.
I'm not aware of any way how to do this directly with current EditDouble, but you can easily define your own control. Actually, you only have to create you own converter and since you can inherit most of it from the original ConvertDouble, it is really simple. All the code you need is class MyConvertDouble:public ConvertDouble{
public:
Value Scan(const Value& text) const {
Value v = UPP::Scan(DOUBLE_V, text);
if(IsError(v)) return v;
if(IsNull(v)) return notnull ? NotNullError() : Value(0); // <-- This is the important line
double m = v;
if(m >= minval && m <= maxval) return v;
return ErrorValue(UPP::Format(t_("Number must be between %g and %g."), minval, maxval));
}
};
typedef EditMinMax<double, MyConvertDouble> MyEditDouble;
Best regards,
Honza
|
|
|
|
Re: Is there a way to make EditDouble return 0 if it has no value? [message #27604 is a reply to message #27603] |
Sun, 25 July 2010 16:44  |
|
MatthiasG wrote on Sun, 25 July 2010 15:35 | Hi Honza,
thanks for your answer. i am using your converter now 
though it would be cool if such an option could be integrated in the original editdouble. had some nasty errors in my application when i forgot to check for a null value.
greetings,
matthias
|
I know from my own experience that it can be sometimes tedious to debug all the additions and multiplying by null, but still I don't think it is worthed the trouble to add the option to regular EditDouble.
As I said above, I usually force the user to input something Actually in most cases I also supply the default value (e.g 0) to the field in the constructor, so it doesn't bother users at all.
Honza
|
|
|
Goto Forum:
Current Time: Sat Apr 26 03:22:55 CEST 2025
Total time taken to generate the page: 0.01128 seconds
|