C# 使用拖放重新排序 winform 列表框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/805165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reorder a winforms listbox using drag and drop?
提问by Gareth Simpson
Is this a simple process?
这是一个简单的过程吗?
I'm only writing a quick hacky UI for an internal tool.
我只是为一个内部工具编写一个快速的 hacky UI。
I don't want to spend an age on it.
我不想在它上面花上一个年龄。
采纳答案by BFree
Here's a quick down and dirty app. Basically I created a Form with a button and a ListBox. On button click, the ListBox gets populated with the date of the next 20 days (had to use something just for testing). Then, it allows drag and drop within the ListBox for reordering:
这是一个快速而肮脏的应用程序。基本上我创建了一个带有按钮和列表框的表单。单击按钮时,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);
}
回答by Daniel Brückner
The first time it takes a few hours if you never implemented drag and drop, want to get it done right and have to read through the docs. Especially the immediate feedback and restoring the list if the user cancels the operation require some thoughts. Encapsulating the behavior into a reusable user control will take some time, too.
如果您从未实现过拖放,第一次需要几个小时,想要正确完成它并且必须通读文档。尤其是用户取消操作时的即时反馈和恢复列表需要一些思考。将行为封装到可重用的用户控件中也需要一些时间。
If you have never done drag and drop at all, have a look at this drag and drop examplefrom the MSDN. This would be a good starting point and it should take you maybe half a day to get the thing working.
如果您从未做过拖放操作,请查看MSDN 中的这个拖放示例。这将是一个很好的起点,您可能需要半天时间才能完成工作。
回答by Simon Buchan
An alternative is using the list-viewcontrol, which is the control Explorer uses to display the contents of folders. It is more complicated, but implements item dragging for you.
另一种方法是使用列表视图控件,这是资源管理器用来显示文件夹内容的控件。它更复杂,但为您实现了项目拖动。
回答by Wally Modz
7 Years Late. But for anybody new, here is the code.
晚了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;
}