listbox控件如何选择删除?为什么我不能删除第一个?

流浪若相惜 2014-01-07 07:45:57
代码附上:
void C共享服务器端Dlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
if(m_list.GetCurSel()==NULL){
MessageBox("你删除为空,请选择所要删除的内容!!");
}
else
m_list.DeleteString(m_list.GetCurSel());
}

m_list是CONTROL型
...全文
218 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaohuh421 2014-01-08
  • 打赏
  • 举报
回复
引用 13 楼 ztzi321 的回复:
引用 12 楼 xiaohuh421 的回复:
不选择时也能删除, 说明默认选择了第一项了.
那如何去掉呢?
14楼已经给你解答了.
「已注销」 2014-01-08
  • 打赏
  • 举报
回复
又见一个滥用 NULL 的。虽然说 NULL 在 C++ 里等于 0,但 GetCurSel 很明显返回的是整数值。MSDN 上也说过了,像 ComboBox 和 ListBox 的错误值都是 -1,而 0 表示第一个项。
凌乱哥 2014-01-08
  • 打赏
  • 举报
回复
看看你初始化时是不是选中了第一条 实在不行就在初始化的时候 m_list.SetCurSel(-1);
流浪若相惜 2014-01-08
  • 打赏
  • 举报
回复
引用 12 楼 xiaohuh421 的回复:
不选择时也能删除, 说明默认选择了第一项了.
那如何去掉呢?
xiaohuh421 2014-01-08
  • 打赏
  • 举报
回复
不选择时也能删除, 说明默认选择了第一项了.
流浪若相惜 2014-01-08
  • 打赏
  • 举报
回复
引用 14 楼 dingxz105090 的回复:
看看你初始化时是不是选中了第一条 实在不行就在初始化的时候 m_list.SetCurSel(-1);
ok
流浪若相惜 2014-01-08
  • 打赏
  • 举报
回复
引用 15 楼 SXJIAKE 的回复:
又见一个滥用 NULL 的。虽然说 NULL 在 C++ 里等于 0,但 GetCurSel 很明显返回的是整数值。MSDN 上也说过了,像 ComboBox 和 ListBox 的错误值都是 -1,而 0 表示第一个项。
了解了,查了msdn发现了!
流浪若相惜 2014-01-07
  • 打赏
  • 举报
回复
引用 10 楼 zhuyf87 的回复:
[quote=引用 8 楼 ztzi321 的回复:] [quote=引用 7 楼 ztzi321 的回复:] [quote=引用 5 楼 zhuyf87 的回复:] 改一下:
int curSel = m_list.GetCurSel();
if (LB_ERR == curSel)
    MessageBox("你删除为空,请选择所要删除的内容!!");
else
    m_list.DeleteString(curSel);
OK!!LB_ERR是什么东西??[/quote]问题是这样的话就不能选择删除了[/quote] 为什么说不能选择删除?DeleteString(curSel),删的就是你选择的。[/quote]不选择时还是可以删不知为什么??
zhuyf87 2014-01-07
  • 打赏
  • 举报
回复
引用 8 楼 ztzi321 的回复:
[quote=引用 7 楼 ztzi321 的回复:] [quote=引用 5 楼 zhuyf87 的回复:] 改一下:
int curSel = m_list.GetCurSel();
if (LB_ERR == curSel)
    MessageBox("你删除为空,请选择所要删除的内容!!");
else
    m_list.DeleteString(curSel);
OK!!LB_ERR是什么东西??[/quote]问题是这样的话就不能选择删除了[/quote] 为什么说不能选择删除?DeleteString(curSel),删的就是你选择的。
zhuyf87 2014-01-07
  • 打赏
  • 举报
回复
引用 7 楼 ztzi321 的回复:
[quote=引用 5 楼 zhuyf87 的回复:] 改一下:
int curSel = m_list.GetCurSel();
if (LB_ERR == curSel)
    MessageBox("你删除为空,请选择所要删除的内容!!");
else
    m_list.DeleteString(curSel);
OK!!LB_ERR是什么东西??[/quote] 你可以看MSDN: int GetCurSel( ) const; The zero-based index of the currently selected item if it is a single-selection list box. It is LB_ERR if no item is currently selected.
流浪若相惜 2014-01-07
  • 打赏
  • 举报
回复
引用 7 楼 ztzi321 的回复:
[quote=引用 5 楼 zhuyf87 的回复:] 改一下:
int curSel = m_list.GetCurSel();
if (LB_ERR == curSel)
    MessageBox("你删除为空,请选择所要删除的内容!!");
else
    m_list.DeleteString(curSel);
OK!!LB_ERR是什么东西??[/quote]问题是这样的话就不能选择删除了
流浪若相惜 2014-01-07
  • 打赏
  • 举报
回复
引用 5 楼 zhuyf87 的回复:
改一下:
int curSel = m_list.GetCurSel();
if (LB_ERR == curSel)
    MessageBox("你删除为空,请选择所要删除的内容!!");
else
    m_list.DeleteString(curSel);
OK!!LB_ERR是什么东西??
zhuyf87 2014-01-07
  • 打赏
  • 举报
回复
Retrieves the zero-based index of the currently selected item, if any, in a single-selection list box. int GetCurSel( ) const;
zhuyf87 2014-01-07
  • 打赏
  • 举报
回复
改一下:
int curSel = m_list.GetCurSel();
if (LB_ERR == curSel)
    MessageBox("你删除为空,请选择所要删除的内容!!");
else
    m_list.DeleteString(curSel);
zhuyf87 2014-01-07
  • 打赏
  • 举报
回复
if(m_list.GetCurSel()==NULL) 是这句代码的问题吧。 选中第一行的话,GetCurSel()会返回0,NULL的值也是0,所以相等。 所以执行的是“MessageBox("你删除为空,请选择所要删除的内容!!");”
流浪若相惜 2014-01-07
  • 打赏
  • 举报
回复
引用 2 楼 ztzi321 的回复:
[quote=引用 1 楼 zhuyf87 的回复:] 第一行肯定能删的吧。只不过删掉了第一行之后,后面的会补到第一行。 deleting the first item will cause the remaining item to now be in the first position. nIndex=0 for the item in the first position.
[/quote]删不掉,貌似不存在的样子!
流浪若相惜 2014-01-07
  • 打赏
  • 举报
回复
引用 1 楼 zhuyf87 的回复:
第一行肯定能删的吧。只不过删掉了第一行之后,后面的会补到第一行。
deleting the first item will cause the remaining item to now be in the first position. nIndex=0 for the item in the first position.

zhuyf87 2014-01-07
  • 打赏
  • 举报
回复
第一行肯定能删的吧。只不过删掉了第一行之后,后面的会补到第一行。 deleting the first item will cause the remaining item to now be in the first position. nIndex=0 for the item in the first position.

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧