> From: Johnathan Smith
>
> Thanks. but can you please help. I have the following
> code. I know how to get the data now. but I have to
> find a way to total it
>
>  FormPtr   form = FrmGetActiveForm();
>  FieldPtr  field;
>  MemHandle  h;
>  CharPtr s;
>
>  field = (FieldPtr)FrmGetObjectPtr(form,
FrmGetObjectIndex(form,MainField1Field));
>  h = FldGetTextHandle(field);
>  if (h)
>  {
>    s = MemHandleLock(h);
>    FldSetTextHandle(field, NULL);
>    FldDrawField(field);
>    field =  (FieldPtr)FrmGetObjectPtr(form,
FrmGetObjectIndex(form,MainTotalField));
>    FldSetTextHandle(field, h);
>    FldDrawField(field);
>

Well, this code doesn't do what you say you are trying to do.

Here is the only code I'm going to write entirely for you.  (I already told
you how to do it.)  Next time you have to figure it out yourself.

Comments below are from my previous instructions.

/***********************************************************************
 * add the numbers in 2 fields and store the result in a third field
 ***********************************************************************/
static void foo(void)
{
        // To get text from a field:
        Char text1[SIZE], text2[SIZE], text3[SIZE];
        Int32 val1, val2, val3;
        FormPtr frmP = FrmGetActiveForm();
        FieldType * fld1P = FrmGetObjectPtr( frmP, FrmGetObjectIndex( frmP,
Main1Field ) );
        FieldType * fld2P = FrmGetObjectPtr( frmP, FrmGetObjectIndex( frmP,
Main2Field ) );
        FieldType * fld3P = FrmGetObjectPtr( frmP, FrmGetObjectIndex( frmP,
Main3Field ) );

        StrCopy( text1, FldGetTextPtr( fld1P ) );
        StrCopy( text2, FldGetTextPtr( fld2P ) );

        // To total two fields, convert them to numbers (e.g., with StrAToI),
        val1 = StrAToI( text1 );
        val2 = StrAToI( text2 );

        // then add them together,
        val3 = val1 + val2;

        // then convert the result to a string (e.g. with StrPrintF).
        StrPrintF( text3, "%ld", val3 );

        // To display the data in a non-editable field, use FldSetTextPtr().
        // *** I prefer use SetFieldTextFromStr since it also works on editable
fields. ***
        SetFieldTextFromStr( Main3Field, text3 );
}

You can find the source code for SetFieldTextFromStr() in the archives.  It
was posted there just 10 days ago.


-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/tech/support/forums/

Reply via email to