C#.NET WinForms如何从一个List< class>中复制一个类实例?到另一个列表< class>使用剪贴板? [英] C# .NET WinForms How do I copy a class instance from one List<class> to another List<class> using the Clipboard?

查看:147
本文介绍了C#.NET WinForms如何从一个List< class>中复制一个类实例?到另一个列表< class>使用剪贴板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个C#.NET WinForms应用程序,其中包含两个DataGridView控件,每个控件都有自己的List< DataItem>:¥b $ b $
DataGridView1,List1< DataItem>

DataGridView2,List2< DataItem>



FYI:DataItem是一个由整数,字符串,布尔值等组成的类,以及所有项目每个datagridview控件中列出的列表也列在其相应的List< DataItem>中。



用户将右键单击DataGridView1,然后在结果上下文菜单中单击" ;复制项目"从List1< DataItem>复制DataItem到剪贴板...
$


I'm writing a C# .NET WinForms app which contains two DataGridView controls, each with its own List<DataItem>:

DataGridView1, List1<DataItem>
DataGridView2, List2<DataItem>

FYI: DataItem is a class consisting of integers, strings, booleans, etc., and all items listed in each datagridview control are also listed in its corresponding List<DataItem>.

The user will right-click on a DataGridView1 and in the resulting context menu, click "Copy Item" to copy a DataItem from List1<DataItem> to the clipboard...

        private void ctxtContextMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            int SelectedRowIndex = DataGridView1.SelectedRows[0].Index;
            if (ctxtCopyItem == e.ClickedItem)
            {
                Clipboard.SetDataObject(List1[SelectedRowIndex]);
            }
            else if (ctxtPasteItem == e.ClickedItem)
            {
                PasteDataItem();
            }
         }






$
b $ b然后用户将转到DataGridView2,右键单击它,然后在上下文菜单中单击"粘贴项目";将DataItem添加到DataGridView2和List2< DataItem> ...
$





The user will then go to DataGridView2, right-click on it and in the context menu, click "Paste Item" to add that DataItem to DataGridView2 and to List2<DataItem>...

        private void PasteDataItem()
        {
            DataItem dataitem = new DataItem();
            dataitem = Clipboard.GetDataObject());   // This line is where the error occurs.
            List2.Add(dataItem);
            UpdateDataGridView2();
         }




$


这里是上下文菜单的开放事件处理程序的代码... ...





And here's the code for the context menu's Opening EventHandler...

        private void ctxtContextMenu_Opening(object sender, CancelEventArgs e)
        {
            // If clipboard contains a DataItem, enable ctxtPasteItem...
            if (Clipboard.GetDataObject() is DataItem)
                ctxPasteQuestion.Enabled = true;
        }






$
方法"PasteDataItem()"上述情况发生异常:



发生System.InvalidCastException

  HResult = 0x80004002

  Message =无法将类型为'System.Windows.Forms.DataObject'的对象强制转换为'DataItem'。
$


显然,我要么不将DataItem复制到剪贴板正确或我没有正确地从剪贴板中检索它。想到的第一个可能的错误是我需要正确地转换DataItem。我没有找到任何关于
的在线教程,使用剪贴板来复制和粘贴类的实例。这甚至可能吗?




In the method "PasteDataItem()" above, an exception occurs:

System.InvalidCastException occurred
  HResult=0x80004002
  Message=Unable to cast object of type 'System.Windows.Forms.DataObject' to type 'DataItem'.

Apparently, I'm either not copying DataItem to the clipboard correctly or I'm not retrieving it from the clipboard correctly. The first possible mistake that comes to mind is that I need to cast the DataItem properly. I haven't found any online tutorials on casting, copying and pasting an instance of a class using the clipboard. Is this even possible?

推荐答案

显然,我要么没有正确地将DataItem复制到剪贴板,要么我不是正确地从剪贴板中检索它。我想到的第一个可能的错误是我需要正确地转换DataItem。

你在哪里提出剪贴板用于传递类/对象在.NET中?

Where did you come up with the clipboard is to be used in passing classes/objects around in .NET?

发生System.InvalidCastException

  HResult = 0x80004002

  Message =无法将类型为'System.Windows.Forms.DataObject'的对象强制转换为'DataItem'

对象由其蓝图(类)定义。

An object is defined by its blueprint, the class.

https:// msdn.microsoft.com/en-us/library/system.windows.dataobject(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/microsoft.analysisservices.dataitem。 aspx

如果你看一下每个对象的蓝图,你会发现它们是不同的蓝图。这就像试图拍摄一个马对象并试图制作一个汽车对象。由于蓝图,它不可能发生。

If you look at each object's blueprint, you'll see that they are different blueprints. It's like attempting to take a horse object and trying to make a car object. It can't happen, because of the blueprint.

Java和.NET都是OO平台,OO的原则是相同的。

Both Java and .NET are OO platforms and the principles of OO are the same.

https://alfredjava.wordpress.com/2008/07/08/class -vs-object-vs-instance /


这篇关于C#.NET WinForms如何从一个List&lt; class&gt;中复制一个类实例?到另一个列表&lt; class&gt;使用剪贴板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆