How can I display data in a listbox using MFC?
To display data in the MFC list box, you can follow the following steps:
- Add a list box control to the resource editor and assign it a unique ID.
- A list box widget
- A list control widget
- Obtain the item by calling GetDlgItem()
- Insert a string
- AddString()
- InsertString()
- RemoveString()
- ClearContent()
Here is a simple example code:
// 在对话框类的头文件中声明列表框成员变量
class CMyDialog : public CDialog
{
// ...
CListBox m_listBox;
// ...
};
// 在对话框初始化函数中初始化列表框
BOOL CMyDialog::OnInitDialog()
{
CDialog::OnInitDialog();
// 获取列表框的指针
CListBox* pListBox = (CListBox*)GetDlgItem(IDC_LISTBOX);
// 添加数据到列表框
pListBox->AddString(_T("Data 1"));
pListBox->AddString(_T("Data 2"));
pListBox->AddString(_T("Data 3"));
return TRUE;
}
// 在其他地方更新列表框
void CMyDialog::UpdateListBox()
{
// 清空列表框
m_listBox.ResetContent();
// 添加新的数据到列表框
m_listBox.AddString(_T("New Data 1"));
m_listBox.AddString(_T("New Data 2"));
m_listBox.AddString(_T("New Data 3"));
}
This is just a simple example that you can expand and modify according to your needs.