Numeric check

General help with the eC language.
Post Reply
shakeshuck
Posts: 31
Joined: Tue Dec 01, 2015 6:52 pm

Numeric check

Post by shakeshuck »

Hi again,

Is there an isNumeric for strings or something similar? FloatFromString needs some input vetting before it's used.

I can do it by hand, but just thought I'd ask as I would've thought a lot of folk would use it?
If I'm the first then of course I could be wrong... :lol:
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: Numeric check

Post by jerome »

Hi shakeshuck,

There are some options here.
eC being a C super-set, anything C will work (e.g. strtod(), strtol()).

The current eC way is probably calling OnGetDataFromString() on your numeric type:

Code: Select all

float x;
if(a.OnGetDataFromString(string))
   PrintLn("Successfully parsed float value: ", x);
This will however return success even if there are further characters after a numeric value.
Use strtol() or strtod() for finer control, which returns a pointer saying where the parsing ended.

You may also want to look into using a SavingDataBox with a data type set to float or double, which will automatically convert to numbers and reject invalid input.

Regards,

-Jerome
shakeshuck
Posts: 31
Joined: Tue Dec 01, 2015 6:52 pm

Re: Numeric check

Post by shakeshuck »

jerome wrote:You may also want to look into using a SavingDataBox with a data type set to float or double, which will automatically convert to numbers and reject invalid input.
The input fields are ListBoxes; I did initially try setting the data type, but then discovered alignment doesn't work if you do. :(
(With reference to the other thread on number alignment, in this case I wanted the value centered).
Use strtol() or strtod() for finer control, which returns a pointer saying where the parsing ended.
I'll take a look at these, thanks.
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: Numeric check

Post by jerome »

If you set the data type to e.g. float for a ListBox field, the data will normally be displayed with the alignment you setup for that field. But only when editing that data, it will be left-aligned.

This could be a work around for now, if you want the editing to be e.g. right-aligned as well.

Code: Select all

class MyFloat : float
{
   Window OnEdit(DataBox dataBox, DataBox obsolete, int x, int y, int w, int h, void * userData)
   {
      EditBox editor = (EditBox)class::OnEdit(dataBox, obsolete, x, y, w, h, userData);
      editor.anchor = { top = 2, right = -4 };
      editor.autoSize = true;
      editor.OnPostCreate();
      return editor;
   }
}
However, this requires a tweak in DataBox.ec, otherwise the DataBox will currently unset that autoSize property if autoSize is not set on the DataBox itself (and auto-size DataBox don't work well with the ListBox):

Code: Select all

if(autoSize) // Need to add this if check
   ((EditBox)editor).autoSize = autoSize;
This is the line in the source:
https://github.com/ecere/ecere-sdk/blob ... ox.ec#L132

Then this should work:

Code: Select all

DataField fld3 { dataType = class(MyFloat), alignment = right, width = 120, editable = true };
shakeshuck
Posts: 31
Joined: Tue Dec 01, 2015 6:52 pm

Re: Numeric check

Post by shakeshuck »

jerome wrote:If you set the data type to e.g. float for a ListBox field, the data will normally be displayed with the alignment you setup for that field. But only when editing that data, it will be left-aligned.
That's what I thought, but it isn't doing that for me; with alignment set to center, centering (at display time) is not occurring if I change the dataType to int or float.
Of course it's highly possible I've unset something somewhere else that's causing this to happen... :roll:
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: Numeric check

Post by jerome »

Please post some sample code to compare.
If you follow my example above it should work.
(But you will need the custom type and the libecere tweak to get the 'edited' data box to follow the alignment as well)
shakeshuck
Posts: 31
Joined: Tue Dec 01, 2015 6:52 pm

Re: Numeric check

Post by shakeshuck »

Video sent, using same MakeActive sample as before...

Cheers. :)
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: Numeric check

Post by jerome »

Hi shakeshuck,

I see what you mean.
What happens is that the data types have a 'default alignment', and setting the dataType property after the alignment will set it to that default alignment. If you change the code to set the alignment after the dataType it will work again.

Regards,

Jerome
Post Reply