MFC 列表框 CListBox


▲ 增删改查

6)列表框CListBox	
	a) 给列表框添加一个字符串 CListBox::AddString
	b) 选中列表列表框某一项,自动触发事件:LBN_SELCHANGE
		1)获取当前选中项 CListBox::GetCurSel
		2)获取指定位置的内容 CListBox::GetText
	c) 删除指定位置的字符串 CListBox::DeleteString
	d) 在指定位置插入字符串 CListBox::InsertString

/***新增***/

void CMFCApplication1LiBiaoDlg::OnBnClickedButton1()
{
    UpdateData(TRUE);
    if (m_Str.GetLength() == 0) {
        MessageBox(TEXT("内容不能为空"), TEXT("提示"));
        return;
    }

    m_listBox.AddString(m_Str);
    m_Str.Empty();  // 清一下列表框
    UpdateData(FALSE);
}

/***删除***/
void CMFCApplication1LiBiaoDlg::OnBnClickedButton2()
{
    int sel_idx = m_listBox.GetCurSel();
    m_listBox.DeleteString(sel_idx);
    UpdateData(FALSE);
}


/***选中项变化***/
void CMFCApplication1LiBiaoDlg::OnLbnSelchangeList1()
{
    int selIndex = m_listBox.GetCurSel();
    m_listBox.GetText(selIndex, m_Str);
    UpdateData(FALSE);
}

/***修改***/
void CMFCApplication1LiBiaoDlg::OnBnClickedButton3()
{
    UpdateData(TRUE);
    if (m_Str.GetLength() == 0) {
        MessageBox(TEXT("内容不能为空"), TEXT("提示"));
        return;
    }

    int selIndex = m_listBox.GetCurSel();
    if (selIndex < 0) {
        MessageBox(TEXT("请选中要修改的项"), TEXT("提示"));
        return;
    }

    m_listBox.DeleteString(selIndex);
    m_listBox.InsertString(selIndex, m_Str);

    UpdateData(FALSE);
}
posted @   double64  阅读(202)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示