其他分享
首页 > 其他分享> > winform中使用listview控件(拖动item子项)

winform中使用listview控件(拖动item子项)

作者:互联网

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ListviewDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
          
           


            listView1.Columns.Add("", 50, HorizontalAlignment.Left);
            listView1.Columns.Add("", 50, HorizontalAlignment.Left);
            listView1.Columns.Add("", 50, HorizontalAlignment.Left);

            //listView1.Items.Add(new ListViewItem(new string[] { "张三", "13岁", "五班" }));
            //listView1.Items.Add(new ListViewItem(new string[] { "李四", "14岁", "六班" }));
            //listView1.Items.Add(new ListViewItem(new string[] { "王五", "15岁", "七班" }));
            listView1.Items.Add(new ListViewItem(new string[] { "张一", "13岁", "一班" }));
            listView1.Items.Add(new ListViewItem(new string[] { "网二", "14岁", "二班" }));
            listView1.Items.Add(new ListViewItem(new string[] { "李四", "15岁", "三班" }));
            listView1.Items.Add(new ListViewItem(new string[] { "孙五", "16岁", "四班" }));
            listView1.Items.Add(new ListViewItem(new string[] { "刘六", "17岁", "五班" }));
            listView1.Items.Add(new ListViewItem(new string[] { "齐七", "18岁", "六班" }));

           // listView1.Items.Insert(5, new ListViewItem(new string[] { "老八", "19岁", "七班" }));
        }
        /// <summary>
        /// 当拖动某项时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
        {
            listView1.DoDragDrop(e.Item, DragDropEffects.Move);
        }
        /// <summary>
        /// 鼠标拖动某项至该控件的区域
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listView1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }
        /// <summary>
        /// 拖动时拖着某项置于某行上方时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listView1_DragOver(object sender, DragEventArgs e)
        {
            Point ptScreen = new Point(e.X, e.Y);
            Point pt = listView1.PointToClient(ptScreen);
            ListViewItem item = listView1.GetItemAt(pt.X, pt.Y);
            if (item != null)
                item.Selected = true;
        }
        /// <summary>
        /// 结束拖动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listView1_DragDrop(object sender, DragEventArgs e)
        {
            ListViewItem draggedItem = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
            Point ptScreen = new Point(e.X, e.Y);
            Point pt = listView1.PointToClient(ptScreen);
            ListViewItem TargetItem = listView1.GetItemAt(pt.X, pt.Y);//拖动的项将放置于该项之前   
            if (null == TargetItem)
                return;

            listView1.Items.Insert(TargetItem.Index, (ListViewItem)draggedItem.Clone());
            listView1.Items.Remove(draggedItem);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int my_count = listView1.Items.Count;
            Console.WriteLine(my_count);
            listView1.Items.Insert(listView1.Items.Count, new ListViewItem(new string[] { "老八", "19岁", "七班" }));
        }
    }
}

在这里插入图片描述
在这里插入图片描述

标签:控件,string,listView1,Items,子项,item,Add,new,ListViewItem
来源: https://blog.csdn.net/xwwwill/article/details/112186027