Using a Transfer Buffer with a List Box

The transfer buffer for a TListBox is of type TListBoxData. This is the main way of transfering information between a TListBox in a Dialog and the program using this dialog. This example is a relatively trivial example, but will suffice to show the process. In this problem we are creating new items in a list box using a dialog box. New items will have a name (string), status (on/off) and a classification (first, second, third or fourth). The name is transfered as a TEdit box, the status as a TRadio button and the classification is derived from a list box.

These choices are presented in this dialog box. Notice the list of choices is alphabetical. If you want a different order, you must deselect the ordered box when creating the list box in the resource workshop. To review creating a new dialog box click here.

The function that uses this listbox is a command for add an item. The item is added to a listbox that is represented by the class ListAllChild.

//{{TMDIChild = ListAllChild}}
class ListAllChild : public TMDIChild {
public:
    ListAllChild (TMDIClient& parent, const char far* title = 0, TWindow* clientWnd = 0, bool shrinkToClient = false, TModule* module = 0);
    virtual ~ListAllChild ();

//{{ListAllChildRSP_TBL_BEGIN}}
protected:
    void cm_AddToList ();
//{{ListAllChildRSP_TBL_END}}
protected:
	TListBox *listBox; 
DECLARE_RESPONSE_TABLE(ListAllChild);
};    //{{ListAllChild}}

The function itself has a local variable tB of XBufTestDialogXfer (the transfer buffer type for this dialog box). The name field is initialized as empty, the on box is checked and the off box unchecked. Finally the choices allowed in the list box are added to the buffer.

void ListAllChild::cm_AddToList ()
{
    // INSERT>> Your code here.
	TBufTestDialogXfer tB;
	tB._name[0] = '\0';// empty name box
	tB._on = BF_CHECKED;
	tB._off = BF_UNCHECKED;
	tB._listBox.AddString("First");
	tB._listBox.AddString("Second");
	tB._listBox.AddString("Third");
	tB._listBox.AddString("Fourth");
	if(TBufTestDialog(&tB,this).Execute()==IDOK)
	{
		char buff[255];
		buff[0]='\0';
		tB._listBox.GetSelString(buff,255);
		char buff2[255];
		buff2[0]='\0';
		strcpy(buff2,tB._name);
		strcat(buff2,"  ");
		strcat(buff2,buff);
		if(tB._on ==BF_CHECKED)
			strcat(buff2,"  on");
		else
			strcat(buff2,"  off");
		listBox->AddString(buff2);
	}
}

The Transfer Buffer for this dialog box has the following structure.

struct TBufTestDialogXfer {
//{{TBufTestDialogXFER_DATA}}
    char    _name[ 255 ];
    TListBoxData  _listBox;
    bool    _off;
    bool    _on;
//{{TBufTestDialogXFER_DATA_END}}
};

Complete Program

For the complete project click here.