在列表视图控件中上下移动项目 [英] Moving items up and down in a listview control

查看:88
本文介绍了在列表视图控件中上下移动项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何上下移动物品

假设如果在列表视图中选择了第4项并且选择了向上按钮,那么所选项目应该移到顶部,顶部项目应该到达第4位。< br $> b $ b

我的尝试:



我是mfc的新手所以不知道

所以我正在读这个如何实现

解决方案

你必须交换这些项目。通过索引(在您的示例中为索引0和3)获取项目内容到变量并使用其他变量设置它们。



如何执行此操作取决于您的列表样式(报告或不报告)和每个项目存储的信息量(文本,图像,复选框,用户数据)。



对于报告列表,您还必须迭代在列上并交换每列的文本。



参见 CListCtrl类 [ ^ ]用于要使用的功能。候选者是 GetItemText SetItemText 的文本(带报告模式的列的文本), GetItemData用户数据的 SetItemData ,或 GetItem SetItem 用于多种设置(图像,状态,文本,格式)。



示例(假设代码是的一部分> CListView 派生类):

  / *  热门项目* /  
int item1 = 0 ;
/ * 所选项目(仅限单项选择)* /
int item2 = GetListCtrl()。GetNextItem( - 1 ,LVNI_SELECTED);
/ * 将item1和item2的数据转换为变量* /
/ * 设置数据* /
/ * 对于报表样式:* /
int columns = GetListCtrl()。GetHeaderCtrl() .GetItemCount();
for int i = 0 ; i< columns; i ++)
{
CString str1 = GetListCtrl()。GetItemText(item1,i);
CString str2 = GetListCtrl()。GetItemText(item2,i);
GetListCtrl()。SetItemText(item1,i,str2);
GetListCtrl()。SetItemText(item2,i,str1);
}
// 可选择更改选择状态


how to move items up and down
suppose if selected item 4 at list view and and selected the up button then selected item should move to top and top item should come to 4 th place.

What I have tried:

I'm new to mfc so no idea
so I am reading about this how to implement

解决方案

You have to exchange the items. Get the item contents by index (index 0 and 3 in your example) into variables and set them using the other variable.

How to do this depends on your list style (report or not) and the amount of information stored per item (text, image, check box, user data).

For a report list you have to iterate also over the columns and exchange the text for each column.

See CListCtrl Class[^] for the functions to be used. Candidates are GetItemText and SetItemText for the text (text of columns with report mode), GetItemData and SetItemData for user data, or GetItem and SetItem for multiple settings (image, state, text, format).

Example (assuming the code is part of a CListView derived class):

/* Top item */
int item1 = 0;
/* Selected item (single selections only) */
int item2 = GetListCtrl().GetNextItem(-1, LVNI_SELECTED);
/* Get data for item1 and item2 into variables */
/* Set data */
/* For report style: */
int columns = GetListCtrl().GetHeaderCtrl().GetItemCount();
for (int i = 0; i < columns; i++)
{
    CString str1 = GetListCtrl().GetItemText(item1, i);
    CString str2 = GetListCtrl().GetItemText(item2, i);
    GetListCtrl().SetItemText(item1, i, str2);
    GetListCtrl().SetItemText(item2, i, str1);
}
// Optionally change selection state


这篇关于在列表视图控件中上下移动项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆