Skip to main content
 首页 » 编程设计

c#之在 ListView 的继承类上使用 DoubleClick 事件

2024年02月20日16tuyile006

我有一个名为 MyClass 的类,它继承自 ListView...所以基本上它是一个 ListView ,我可以在表单等上使用它。我已经覆盖了此类的拖放事件,所以现在如果我将其中两个控件放在一个窗体上,然后将项目从一个控件拖到另一个控件,它会做得很好。 我的问题是 DoubleClick,如何在此类上实现 DoubleClick()?所以当我稍后将这些控件中的两个放在一个窗体上并双击其中一个上的一个项目时,它会将它移动到另一个控件......这甚至可能吗?

请您参考如下方法:

我很好奇这是如何实现的,所以我尝试了一下。我创建了一个新的 Windows 窗体项目并创建了一个 myClass:ListView(请参阅下面的代码)然后我将两个控件添加到窗体中,并使用姐妹属性将它们绑定(bind)在一起(我并没有真正将其写为属性)

我向 myClass1 添加了 1 个项目,向 myClass2 添加了 2 个项目。当我双击它们中的任何一个时,它们会移动到另一个 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); 
    } 
 
}