题
这是一个简单的过程吗?
我只是为内部工具编写一个快速的hacky UI。
我不想花一点时间。
解决方案
这是一个快速下来的脏应用程序。基本上我用一个按钮和一个ListBox创建了一个Form。单击按钮时,ListBox将填充接下来20天的日期(必须使用仅用于测试的内容)。然后,它允许在ListBox中拖放以进行重新排序:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.listBox1.AllowDrop = true;
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i <= 20; i++)
{
this.listBox1.Items.Add(DateTime.Now.AddDays(i));
}
}
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
if (this.listBox1.SelectedItem == null) return;
this.listBox1.DoDragDrop(this.listBox1.SelectedItem, DragDropEffects.Move);
}
private void listBox1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void listBox1_DragDrop(object sender, DragEventArgs e)
{
Point point = listBox1.PointToClient(new Point(e.X, e.Y));
int index = this.listBox1.IndexFromPoint(point);
if (index < 0) index = this.listBox1.Items.Count-1;
object data = e.Data.GetData(typeof(DateTime));
this.listBox1.Items.Remove(data);
this.listBox1.Items.Insert(index, data);
}
其他提示
晚了7年。但对于任何新人来说,这是代码。
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
if (this.listBox1.SelectedItem == null) return;
this.listBox1.DoDragDrop(this.listBox1.SelectedItem, DragDropEffects.Move);
}
private void listBox1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void listBox1_DragDrop(object sender, DragEventArgs e)
{
Point point = listBox1.PointToClient(new Point(e.X, e.Y));
int index = this.listBox1.IndexFromPoint(point);
if (index < 0) index = this.listBox1.Items.Count - 1;
object data = listBox1.SelectedItem;
this.listBox1.Items.Remove(data);
this.listBox1.Items.Insert(index, data);
}
private void itemcreator_Load(object sender, EventArgs e)
{
this.listBox1.AllowDrop = true;
}
如果你从未实施过拖放,第一次需要几个小时,想要正确完成并且必须阅读文档。特别是如果用户取消操作,立即反馈和恢复列表需要一些想法。将行为封装到可重用的用户控件中也需要一些时间。
如果您从未完成过拖放操作,请查看拖放示例。这将是一个很好的起点,它可能需要半天时间让事情发挥作用。
另一种方法是使用 list-view control,这是控件资源管理器用来显示文件夹的内容。它更复杂,但实现了项目拖动。
不隶属于 StackOverflow