On 2006-12-12, Lynn Allan <[EMAIL PROTECTED]> wrote: ><alert comment="newbie using CW9.3" /> > > I've got an editable field MyFindField that can be up to 80 characters long. > Also, there is a list with strings that can be used to replace the > MyFindField. These strings are typically shorter than the contents of > MyFindField. > > MyFindField: 01234_this_is_a_long_find_field > List: > ..zero > ..one > ..two > ..three > > When "two" is clicked, I was getting the result of having MyFindField being: > two34_this_is_a_long_find_field > > I tried a number of combinations to get this fixed, but my "solution" seems > less than appropriate. Is there a better series of statements to > accomplish this than the following? > > Int16 curSelIndex = pEvent->data.lstSelect.selection; // is 2 > char* selectedStr = m_szListContents[curSelIndex]; // has "two" > Int16 len = StrLen(selectedStr); > MemHandle hFindWords = MemHandleNew(len + 2); > MemPtr pFindWords = MemHandleLock(hFindWords); > StrCopy((char*)pFindWords, selectedStr); > FldSetTextHandle(m_pFindWordsField, hFindWords); > FldDrawField(m_pFindWordsField); > MemHandleUnlock(hFindWords); > MemHandleFree(hFindWords); >
Have you run this code on the Emulator with a debug ROM? It should have complained about this. MemHandleUnlock has to be called before FldSetTextHandle. Do not call MemHandleFree as long as the string/handle is used by the field. Look at the sample code in the description of FldSetTextHandle how to free the previous handle of the field. Your code leaks memory. An other approach is to keep the same handle for the field and edit/replace the string as needed. Of course you have to take care not to exceed the allocated size for the handle. As a brute force solution you could call FldEraseField before calling FldDrawField. HTH Ton van Overbeek P.S. May be worthwhile to look at the sample code included with CW and the SDK. Especially the big ones (Address, Memo, Datebook) in the Palm OS 5 SDK. -- For information on using the PalmSource Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/
