How can I display data in a listbox using MFC?

To display data in the MFC list box, you can follow the following steps:

  1. Add a list box control to the resource editor and assign it a unique ID.
  2. A list box widget
  3. A list control widget
  4. Obtain the item by calling GetDlgItem()
  5. Insert a string
  6. AddString()
  7. InsertString()
  8. RemoveString()
  9. 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.

Leave a Reply 0

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


广告
Closing in 10 seconds
bannerAds