Using CListBox in windows especially in pop up windows

I had an opportunity to use CListbox. The scenario was that I had to pre populate the list box and display it in a pop up window.
Its very to use listbox when using it in a parent window but when you want to display it in a standalone pop up window, you would get an error.
This is because the data is present only after the window comes into picture completely and is gone when the window disappears. In order to rectify this you would need to over rider the Initdialog method as below:
BOOL OnInitDialog()
{
//check if the parent window dialog is also initialized
if(CDialog::OnInitDialog())
{
//enter the values into the listbox here
}
}
By the above code you can enter into a listbox before its displayed.
Declare the below methods in the header file for getting the list selection changes:
afx_msg void OnLbnSelchangeList1();
afx_msg void OnLbnDblclkList1();
Add them to the message map in the class as below:
BEGIN_MESSAGE_MAP(YourClassname, CWnd)
ON_LBN_SELCHANGE(IDC_LIST1, &YourClassname::OnLbnSelchangeList1)
ON_LBN_DBLCLK(IDC_LIST1, &YourClassname::OnLbnDblclkList1)
END_MESSAGE_MAP()
Over ride them with the functionalities you need as below:
void YourClassname::OnLbnSelchangeList1()
{
}
void YourClassname::OnLbnDblclkList1()
{
// TODO: Add your control notification handler code here
}
Use CArray to get the values:
CArray aryListBoxSel;
aryListBoxSel.SetSize(nCurrentSelected);
m_ListBox1.GetSelItems(nCurrentSelected, aryListBoxSel.GetData());
CString sTitle;
int indexVal = 0;
for(int i=0;i 0)
{
int *pSels = new int[Count];
m_ListBox1.GetSelItems(Count,pSels);
for (int i = 0; i < Count;i++)
{
//pSels[i] this is the index of the item that was selected
CString Temp;
m_ListBox1.GetText(pSels[i],Temp);
}
delete [] pSels;
}

Leave a Reply

Your email address will not be published. Required fields are marked *