> Let's say I have a form w/ several fields and a button. When the
> button is pressed, I'd like whichever field currently has the focus to have
> it's text set to "howdy". This is what I'm doing... and instead of
> "howdy", the selected field winds up with 5 spaces added to it. Could
> somebody tell me what I'm doing wrong?
Three things I'd change about your code:
First, StrCopy is quicker than StrPrintF:
StrCopy(textP, "Howdy");
Second, there's no need to to a FldDelete (plus, you're not guaranteed that
the FldDelete will free the existing handle). Instead of
> if (FldGetTextPtr(fldP))
> FldDelete(fldP, 0, FldGetTextLength(fldP));
do:
FldFreeMemory(fld);
The third addresses the problem you asked about. After setting the text
handle with FldSetTextHandle, you need to draw the field:
> FldSetTextHandle(fldP, textH);
FldDrawField(fldP);
>
> static Boolean MainFormHandleEvent(EventPtr eventP)
> {
> ...
> switch (eventP->eType) {
> case ctlSelectEvent: // A control button was pressed and released.
> switch (eventP->data.ctlEnter.controlID) {
> case ProgramMainTestButton: // the single button on the form
> if ((fldP = GetFocusObjectPtr()) != NULL) {
> textH = MemHandleNew(10);
> if (textH) {
> textP = MemHandleLock(textH);
> StrPrintF(textP, "Howdy");
> MemHandleUnlock(textH);
> if (FldGetTextPtr(fldP))
> FldDelete(fldP, 0, FldGetTextLength(fldP));
> FldSetTextHandle(fldP, textH);
> }
> }
> break;
> }
> break;
> }
> }
>
>
>