在从 ListView 继承的类上使用 DoubleClick 事件 [英] Using DoubleClick event on a inherited class from ListView
问题描述
我有一个名为 MyClass 的类,它是从 ListView 继承的......所以基本上它是一个列表视图,我可以在表单等上使用它......我已经覆盖了这个类的拖放事件,所以现在如果我将其中的两个控件放在一个表单上并将项目从一个拖到另一个,这样就可以了.我的问题是 DoubleClick,我如何在这个类上实现 DoubleClick()?因此,当稍后我将其中两个控件放在一个表单上并双击其中一个控件上的一个项目时,它会将其移动到另一个控件......甚至可能吗?
I have a single class called MyClass which is inheriting from ListView...so basically it is a list view and I can use it on a form,etc..I have overriden drag and drop events for this class so now if I put two of these controls on a form and drag items from one to the other, it will do it fine. My problem is with DoubleClick, How can I implement DoubleClick() on this class? so when later I put two of these controls on a form and double click on a item on one of them, it will move it to the other control... Is it even possible?
推荐答案
我很好奇这是如何实现的,所以我尝试了它.我创建了一个新的 windows 窗体项目并创建了一个 myClass:ListView(见下面的代码)然后我在窗体中添加了两个控件,并使用姊妹属性将它们绑定在一起(我并没有真正将其写为属性)
I was curious how this could be achieved so I tried it. I created a new windows forms project and created a myClass:ListView(see code below) Then I added two if the Controls to the Form, and tied them together using the sister property (which I did not really write up as a property)
我向 myClass1 添加了 1 个项目,向 myClass2 添加了 2 个项目.当我双击它们中的任何一个时,它们会移动到另一个列表视图.
I added 1 item to myClass1 and 2 Items to myClass2. When I double click on anyof them, they move to the other listview.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
myClass1.sister = myClass2;
myClass2.sister = myClass1;
}
}
public class myClass : ListView
{
//This is where we will store the information about the sister Control
private myClass _sister;
//**EDIT** I changed sister into a property and now it is selectable
//from the designer.
public myClass sister { get { return _sister; } set { _sister = value; } }
public myClass()
{
}
//here is where we override the OnDoubleCLick Event
protected override void OnDoubleClick(EventArgs e)
{
//Make sure this control has a sister
if (sister != null)
{
//add a temporary storage location for the selected Item
ListViewItem item = this.SelectedItems[0];
//Remove the Item from the clicked List
this.Items.Remove(item);
//Add the Item to the sister List
((myClass)sister).Items.Add(item);
}
base.OnDoubleClick(e);
}
}
这篇关于在从 ListView 继承的类上使用 DoubleClick 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!