Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
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 Go to next message
MatthiasG is currently offline  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 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1781
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

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 #27603 is a reply to message #27598] Sun, 25 July 2010 15:35 Go to previous messageGo to next message
MatthiasG is currently offline  MatthiasG
Messages: 27
Registered: January 2008
Location: Germany
Promising Member
Hi Honza,

thanks for your answer. i am using your converter now Smile

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
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 Go to previous message
dolik.rce is currently offline  dolik.rce
Messages: 1781
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

MatthiasG wrote on Sun, 25 July 2010 15:35

Hi Honza,

thanks for your answer. i am using your converter now Smile

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 Smile 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
Previous Topic: Scan an timestamp
Next Topic: [FEATURE] Horizontal scroll on Shift+mouse wheel
Goto Forum:
  


Current Time: Fri Oct 22 22:25:06 CEST 2021

Total time taken to generate the page: 0.01005 seconds