别让MSDN忽悠了你

来源:互联网 发布:使命召唤13优化补丁 编辑:程序博客网 时间:2024/06/02 15:40

虽说MSDN代码是微软的高手写的,很正确很权威.不过它有时候也有很龌龊的一面,为了不影响到广大的青少年和小朋友们,下面举例说明:MSDN为VS2005版,查询函数CListBox::DeleteString的说明如下

Deletes the item in position nIndex from the list box.

 
int DeleteString(   UINT nIndex );

 

Parameters

nIndex

Specifies the zero-based index of the string to be deleted.

Collapse imageReturn Value

A count of the strings remaining in the list. The return value is LB_ERR if nIndex specifies an index greater than the number of items in the list.

Collapse imageRemarks

All items following nIndex now move down one position. For example, if a list box contains two items, 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.

Collapse imageExample

  CopyCode imageCopy Code
// The pointer to my list box.extern CListBox* pmyListBox;// Delete every other item from the list box.for (int i=0;i < pmyListBox->GetCount()/*每次大小都减一*/;i++){   pmyListBox->DeleteString( i );

//并不是我们预料的那样删除,因为删除一个后,后面的元素索引将前移一位

}

 正确的做法应该是

for (int i(pmyListBox->GetCount()-1);i >=0;--i){   pmyListBox->DeleteString( i );}

以上只是为了说明DeleteString的用法,如果想达到上述目的,

可以while(LB_ERR!=pComBox->DeleteString(0));

或者更直接pComBox->ResetContent();

原创粉丝点击