📄 draginfocontrol.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
// 实现可以将信息从一个集合拖动到另外的一个集合
namespace 课程安排
{
public partial class DragInfoControl : UserControl
{
private List<object> listSourceTag; // 对应于 listbox1 记录tag
private List<object> listDestinationTag; // 对应与 listbox2 记录tag
private List<DragInfoItem> source;
public List<DragInfoItem> Source
{
get
{
this.SourceToCollection();
return source;
}
set
{
source = value;
this.RefreshSource();
}
}
private List<string> sourceStrings;
public List<string> SourceStrings
{
get
{
this.SourceToCollection();
this.ToSourcesString();
return this.sourceStrings;
}
set
{
this.sourceStrings = value;
this.ToSourceObject();
this.RefreshSource();
}
}
private List<DragInfoItem> destination;
public List<DragInfoItem> Destination
{
get
{
this.DestinationToCollection();
return destination;
}
set
{
destination = value;
this.RefreshDestination();
}
}
private List<string> destinationStrings;
public List<string> DestinationStrings
{
get
{
this.DestinationToCollection();
this.ToDestinationString();
return this.destinationStrings;
}
set
{
this.destinationStrings = value;
this.ToDestinationObject();
this.RefreshDestination();
}
}
private bool hasModify = false;
public bool HasModify
{
get { return hasModify; }
set { hasModify = value; }
}
public DragInfoControl()
{
this.source = new List<DragInfoItem>();
this.destination = new List<DragInfoItem>();
this.listSourceTag = new List<object>();
this.listDestinationTag = new List<object>();
InitializeComponent();
}
~DragInfoControl()
{
throw new System.NotImplementedException();
}
public event EventHandler AfterDrag;
public void Dispose()
{
//throw new System.NotImplementedException();
}
// 刷新显示,将集合中的数据显示在列表中
public void Refresh()
{
RefreshSource();
RefreshDestination();
}
private void RefreshSource()
{
listBox1.Items.Clear();
listSourceTag.Clear();
if (this.source == null)
return;
for (int i = 0; i < this.source.Count; i++)
{
listBox1.Items.Add(source[i].Name);
listSourceTag.Add(source[i].Tag);
}
}
private void RefreshDestination()
{
listBox2.Items.Clear();
listDestinationTag.Clear();
if (this.destination == null)
return;
for (int i = 0; i < this.destination.Count; i++)
{
listBox2.Items.Add(destination[i].Name);
listDestinationTag.Add(destination[i].Tag);
}
}
// 将列表中的数据更新到集合中
public void ToCollection()
{
SourceToCollection();
DestinationToCollection();
}
private void SourceToCollection()
{
source.Clear();
for (int i = 0; i < listBox1.Items.Count; i++)
{
DragInfoItem dii = new DragInfoItem();
dii.Name = listBox1.Items[i].ToString();
dii.Tag = listSourceTag[i];
source.Add(dii);
}
}
private void DestinationToCollection()
{
destination.Clear();
for (int i = 0; i < listBox2.Items.Count; i++)
{
DragInfoItem dii = new DragInfoItem();
dii.Name = listBox2.Items[i].ToString();
dii.Tag = listDestinationTag[i];
destination.Add(dii);
}
}
#region 将字符串和对象转换
private void ToSourceObject()
{
if (this.source == null)
this.source = new List<DragInfoItem>();
else
this.source.Clear();
for (int i = 0; i < this.sourceStrings.Count; i++)
{
DragInfoItem dii = new DragInfoItem();
dii.Name = this.sourceStrings[i];
this.source.Add(dii);
}
}
private void ToDestinationObject()
{
if (this.destination == null)
this.destination = new List<DragInfoItem>();
else
this.destination.Clear();
for (int i = 0; i < this.destinationStrings.Count; i++)
{
DragInfoItem dii = new DragInfoItem();
dii.Name = destinationStrings[i];
destination.Add(dii);
}
}
private void ToSourcesString()
{
if (this.sourceStrings == null)
this.sourceStrings = new List<string>();
else
this.sourceStrings.Clear();
for (int i = 0; i < this.source.Count; i++)
{
this.sourceStrings.Add(source[i].Name);
}
}
private void ToDestinationString()
{
if (this.destinationStrings == null)
this.destinationStrings = new List<string>();
else
this.destinationStrings.Clear();
for (int i = 0; i < this.destination.Count; i++)
{
this.destinationStrings.Add(destination[i].Name);
}
}
#endregion
private class DragDropInfo
{
public object sender;
public object info;
}
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
DragDropInfo ddi = new DragDropInfo();
ddi.sender = sender;
ddi.info = this.listBox1.SelectedIndices;
this.listBox1.DoDragDrop(ddi, DragDropEffects.Move);
}
}
private void listBox2_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
DragDropInfo ddi = new DragDropInfo();
ddi.sender = sender;
ddi.info = this.listBox2.SelectedIndices;
this.listBox2.DoDragDrop(ddi, DragDropEffects.Move);
}
}
private void listBox1_DragDrop(object sender, DragEventArgs e)
{
DragDropInfo ddi = (DragDropInfo)e.Data.GetData(typeof(DragDropInfo));
ListBox lb = ddi.sender as ListBox;
ListBox.SelectedIndexCollection items = ddi.info as ListBox.SelectedIndexCollection;
if (lb == this.listBox1)
return;
for (int i = 0; i < items.Count; i++)
{
this.listBox1.Items.Add(this.listBox2.Items[items[i]]);
this.listSourceTag.Add(this.listDestinationTag[items[i]]);
}
for (int i = 0; i < items.Count; i++)
{
this.listDestinationTag.RemoveAt(items[i]);
this.listBox2.Items.RemoveAt(items[i]);
}
this.hasModify = true;
}
private void listBox2_DragDrop(object sender, DragEventArgs e)
{
DragDropInfo ddi = (DragDropInfo)e.Data.GetData(typeof(DragDropInfo));
ListBox lb = ddi.sender as ListBox;
ListBox.SelectedIndexCollection items = ddi.info as ListBox.SelectedIndexCollection;
if (lb == this.listBox2)
return;
for (int i = 0; i < items.Count; i++)
{
this.listBox2.Items.Add(this.listBox1.Items[items[i]]);
this.listDestinationTag.Add(this.listSourceTag[items[i]]);
}
for (int i = 0; i < items.Count; i++)
{
this.listSourceTag.RemoveAt(items[i]);
this.listBox1.Items.RemoveAt(items[i]);
}
this.hasModify = true;
}
private void DragInfoControl_Resize(object sender, EventArgs e)
{
int bianju = 10;
int jianju = 3; // lebel 和 list 之间的距离
if (this.Width < 200)
this.Width = 200;
if (this.Height < 150)
this.Height = 150;
this.label1.Left = bianju;
this.label1.Top = bianju;
this.label2.Left = this.Width / 2 + bianju;
this.label2.Top = bianju;
this.listBox1.Left = bianju;
this.listBox1.Top = bianju + this.label1.Height + jianju;
this.listBox1.Width = this.Width / 2 - 2 * bianju;
this.listBox1.Height = this.Height - bianju * 2 - jianju - label1.Height;
this.listBox2.Left = this.Width / 2 + bianju;
this.listBox2.Top = bianju + this.label2.Height + jianju;
this.listBox2.Width = this.Width / 2 - 2 * bianju;
this.listBox2.Height = this.Height - bianju * 2 - jianju - label2.Height;
}
public string SourceLabel
{
get
{
return this.label1.Text;
}
set
{
this.label1.Text = value;
}
}
public string DestinationLabel
{
get
{
return this.label2.Text;
}
set
{
this.label2.Text = value;
}
}
private void listBox1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void listBox2_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
}
[Serializable]
public class DragInfoItem
{
private string name = "";
public string Name
{
get { return name; }
set { name = value; }
}
private object tag = null;
public object Tag
{
get { return tag; }
set { tag = value; }
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -