ebmDevMag.com home
ebm Developer's Magazine
links

developer's mag
main page

article
part 1
part 2
part 3
part 4
part 5


4 - And Now to Wrap It Up

Once you have the list box, it's a simple matter to attach it to a window. Here we create a popup, complete with a Cancel button.


static int Choice=0;
//------------------------------------------------------------------------------
// CListPopUp - wrapper for CStringList
//------------------------------------------------------------------------------
class CListPopUp : public CWindow
{
private:
S32 MsgHandler( MSG_TYPE type, CViewable *from, S32 data );
public:
CListPopUp(int x,int y,int dx,int dy,
const char**array,int total,
const char *caption);
};
//------------------------------------------------------------------------------
CListPopUp::CListPopUp(int x,int y,int dx,int dy,
const char**array,int total,
const char caption)
: CWindow(100,x,y,dx,dy,caption,false,false,false)
{
// window is one big listbox, with caption at top, and cancel button at bottom
AddChild( new CButton(101,dx-8,20,"Cancel"),2,dy-40);
AddChild( new CStringList(102,dx-10,dy-48,array,total), 5,5 );
}
//------------------------------------------------------------------------------
S32 CListPopUp::MsgHandler( MSG_TYPE type, CViewable *from, S32 data )
{
if ( MSG_ROW_SELECT==type ) // listbox choice made?
{
Choice=data; // then save it
Close();
}
else if ( MSG_BUTTON_SELECT==type ) // cancel button?
{
Choice=-1; // then set flag - invalid index
Close();
}
return CWindow::MsgHandler(type, from, data);
}

MsgHandler() picks up the row selection message MSG_ROW_SELECT, which has the row number in the data variable. We store this in the static variable Choice so the result is available to the program after this form closes. The only other point to note is that the Cancel button sets an invalid index of -1, so we can easily determine if no row was selected.

To call this dialog up, we can use GUI_EventLoop(), and pass it an array of string selections and a total - upon return, Choice is set:


GUI_EventLoop( new CListPopUp(10,10,180,200,
myStringArray,
myTotalStrings,
"My String List") );
This creates a modal form (one that doesn't return until it is closed); of course, we could also add the list box to the main form with AddChild().The documentation goes into detail about what you can do with it there, but unless you are planning on doing intensive programming with an interactive list box, you might find many of them unnecessary, with the exception of SetTopRow() and SetCurrentRow() for display positioning. An important note about display is that the CList constructor flag LISTOPTION_ALWAYS_HIGHLIGHT (which keeps the entry inverted instead of flashing it) affects the result of GetCurrentRow(). Rather than using it, I prefer to store the pick in a variable in the message loop, as in the example code.

Previous Section
Next Section

Copyright © 2001-2006 ebmDevMag.com - Legal Notice