Hello group -
With assistance of several in the group, I succeeded in extracting the text
in a Listbox selection and wrote it in a field. I cleaned up the code a
bit, commented it, and have included it here - hoping it will help someone
else down the road.
To use it:
1) Make a form.
2) Make a List and a field control on the form.
[rb}
/*****************************************************************************
* ListToField.c
* -------------------------------------------------------------------------
* EXAMPLE FOR PLACING TEXT FROM A SELECTION IN A LIST CONTROL INTO A FIELD.
* -------------------------------------------------------------------------
* Created : 02/20/2001 9:11:40 AM
* Creator : Randy Brown (with help from several Palm.Dev groups members)
* -------------------------------------------------------------------------
*
* ASSUMPTIONS OF EXAMPLE
* ======================
* 1) Everything is on one form.
* 2) One list and one field.
*
* LESSONS LEARNED
* ===============
* 1) Place case lstSelectEvent AFTER case frmOpenEvent, otherwise
* form flickers when updating the field.
* 2) SetFieldHandle function can be placed in separate source file
* by adding proper prototype in the project's header file.
*
****************************************************************************/
#include <PalmOS.h>
#include "ListToField.h"
#include "ListToField_res.h"
static Boolean frmMain_lstMyList_OH(UInt16 selection); //
prototype
static void SetFieldHandle(FieldType *ptrToField, MemPtr ptrToText); //
prototype
Boolean frmMain_HandleEvent(EventPtr event)
{
FormPtr form;
Boolean handled = false;
switch (event->eType)
{
case frmOpenEvent:
// Repaint form on open
form = FrmGetActiveForm();
FrmDrawForm(form);
handled = true;
break;
case lstSelectEvent:
switch ( event->data.lstSelect.listID )
{
case lstMyList:
/* Control lstMyList receives an
event*/
handled =
frmMain_lstMyList_OH(event->data.lstSelect.selection);
break;
}
}
return handled;
}
static Boolean frmMain_lstMyList_OH(UInt16 selection)
{
/* frmMain_lstMyList_OH
====================
This function takes the list selection (as UInt16)
and places the text associated to it in the list
into a field (fldMyField).
selection = event->data.lstSelect.selection
(comes from the lstSelectEvent).
lstMyList = name of the List control.
fldMyField = name of Field control.
*/
FormPtr ptrToForm; // Pointer to form where
list is.
FieldPtr ptrToField; // Pointer
to the field.
ListPtr ptrToList; // Pointer to the
list.
Char *ptrToText; // Pointer to text
in list selection.
// Get pointer to active form (where list is located).
ptrToForm = FrmGetActiveForm();
// Get pointer to the list object.
ptrToList = FrmGetObjectPtr(ptrToForm,
FrmGetObjectIndex(ptrToForm, lstMyList));
//Get pointer to the field.
ptrToField = FrmGetObjectPtr(ptrToForm,
FrmGetObjectIndex(ptrToForm, fldMyField));
// Store the selected text into a pointer.
ptrToText = LstGetSelectionText(ptrToList, selection);
// Call SetFieldHandle and pass in pointer
// to field and pointer to selected text from list.
SetFieldHandle(ptrToField, ptrToText);
return true;
}
static void SetFieldHandle(FieldType *ptrToField, MemPtr ptrToText)
{
MemHandle newTextH; // The text that is selected.
MemHandle oldTextH; // Text that may already
be in the field.
MemPtr tempPtr; // Pointer to newTextH.
// Allocate handle to store the text from the list (+ 16 extra).
newTextH = MemHandleNew(StrLen(ptrToText)+16);
// Get pointer to memory and lock.
tempPtr = MemHandleLock(newTextH);
// Copy list text into memory pointer.
StrCopy(tempPtr, ptrToText);
// Get old text handle.
oldTextH = FldGetTextHandle(ptrToField);
// Set the field's new text.
FldSetTextHandle(ptrToField, newTextH);
// Unlock the new text handle.
MemHandleUnlock(newTextH);
// Update the field.
FldDrawField(ptrToField);
// Free the old text handle to prevent a memory leak.
if (oldTextH != NULL)
MemHandleFree(oldTextH);
}
[rb}
http://www.randybrown.net
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/tech/support/forums/