c#中在一个listbox中,对元素进行拖拽,排序

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


namespace List拖拽排序
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//当窗体被加载的时候发生
private void Form1_Load(object sender, EventArgs e)
{
this.lbSort.AllowDrop = true;//允许拖拽
}
//拖拽操作完成时发生
private void lbSort_DragDrop(object sender, DragEventArgs e)
{
//GetDataPresent()确定此实例中存储的数据是否与指定的格式关联,或是否可以转换成指定的格式。
if (e.Data.GetDataPresent(DataFormats.StringFormat))
{
//PointToClient()将指定屏幕点的位置计算成工作区坐标。
//IndexFromPoint()返回指定坐标处的项的从零开始的索引。
int indexPos = ((ListBox)sender).IndexFromPoint(((ListBox)sender).PointToClient(new Point(e.X, e.Y)));
if (indexPos > -1)
((ListBox)sender).Items.Insert(indexPos, lbSort.SelectedItem);
else
((ListBox)sender).Items.Add(lbSort.SelectedItem);
}
}
//在用鼠标将某项拖拽到该控件的工作区时发生
private void lbSort_DragEnter(object sender, DragEventArgs e)
{
//GetDataPresent()确定此实例中存储的数据是否与指定的格式关联,或是否可以转换成指定的格式。
if (e.Data.GetDataPresent(DataFormats.Text))
{
e.Effect = DragDropEffects.Move;
}
else
{
e.Effect = DragDropEffects.None;
}
}


//当鼠标被按下时候发生
private void lbSort_MouseDown(object sender, MouseEventArgs e)
{
isDrag = true;
if (((ListBox)sender).Items.Count == 0)
{
return;
}
// int index = ((ListBox)sender).IndexFromPoint(e.X, e.Y);
int index = 0;
for (int i = 0; i < lbSort.Items.Count; i++)
{//取得选中项的下表
if (lbSort.GetSelected(i))
{
index = i;
break;
}
}
//在指定坐标处找到的项的从零开始的索引;如果找不到匹配项,则返回 ListBox.NoMatches。
if (index < 0)
{
return;
}//index为listbox中的索引
string s = ((ListBox)sender).Items[index].ToString();
// DragDropEffects 指定拖放操作的可能效果
DragDropEffects dde1 = DoDragDrop(s, DragDropEffects.Move);//开始拖拽操作,s为要拖拽的数据
if (isDrag)
{
if (s == ((ListBox)sender).Items[index].ToString())
{
((ListBox)sender).Items.RemoveAt(index);//是把自己位置的删除掉
}
else
{
((ListBox)sender).Items.RemoveAt(index + 1);
}
}
}
bool isDrag = true;
/// <summary>
/// 将对象拖出控件的边界时候发生
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void lbSort_DragLeave(object sender, EventArgs e)
{
isDrag = false;
}
/// <summary>
/// 自动排序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSort_Click(object sender, EventArgs e)
{
int i = 0, j = 0;
string[] strtemp = new string[lbSort.Items.Count];
int[] str1 = new int[lbSort.Items.Count];
for (i = 0; i < lbSort.Items.Count; i++)
{
str1[i] = Convert.ToInt32(lbSort.Items[i].ToString().Split('\t', ' ')[0]);
strtemp[i] = lbSort.Items[i].ToString();
}
string temp = "";

int temp2;

//冒泡排序的实现

for (i = 0; i < lbSort.Items.Count-1; i++)
{
for (j = 0; j < lbSort.Items.Count-1-i; j++)
{
if (str1[j] > str1[j+1])
{//因为现在是对两个表的操作,所以对应的两个数组都做改变改变。
temp2 = str1[j];
str1[j] = str1[j+1];
str1[j+1] = temp2;
temp = strtemp[j];
strtemp[j] = strtemp[j+1];
strtemp[j+1] = temp;
}
}
}
lbSort.Items.Clear();
for (i = 0; i < strtemp.Length; i++)
{
lbSort.Items.Add(strtemp[i]);
}
}


private void btnSingleSort_Click(object sender, EventArgs e)
{
int i = 0, j = 0;
string[] strtemp = new string[lbSort.Items.Count];
int[] str1 = new int[lbSort.Items.Count];
for (i = 0; i < lbSort.Items.Count; i++)
{
str1[i] = Convert.ToInt32(lbSort.Items[i].ToString().Split('\t', ' ')[0]);
strtemp[i] = lbSort.Items[i].ToString();
}
string temp = "";
//
for (i = 0; i < lbSort.Items.Count; i++)
{
for (j = i; j < lbSort.Items.Count; j++)
{
if (str1[i] > str1[j])
{
temp = strtemp[i];
strtemp[i] = strtemp[j];
strtemp[j] = temp;
}
}
}
lbSort.Items.Clear();
for (i = 0; i < strtemp.Length; i++)
{
lbSort.Items.Add(strtemp[i]);
}
}
}

}

实验截图如下:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值