如何在C#中将项目从一个ListView复制到另一个 [英] How to Copy Items from one ListView to Another in C#

查看:80
本文介绍了如何在C#中将项目从一个ListView复制到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我在不同的项目中有两个ListView控件.

1)客户项目中的lstclnt
2)服务器项目中的lstsv

我想在更新按钮"的单击事件上从lstsv更新lstclnt中的副本值

我已经使用了这4个代码,它们没有给出任何错误,但不起作用

Hello,

I have two ListView controls in different projects.

1)lstclnt in client project
2)lstsv in server project

I want to update copy values in lstclnt from lstsv on the click event of Update Button

I have used these 4 codes, they aren''t giving any error but doesn''t works

foreach (ListViewItem item in MainForm.lstsv.Items)
   {
      lstclnt.Items.Add(item.Clone() as ListViewItem);
   }





lstclnt.Items.AddRange((from ListViewItem item in MainForm.lstsv.Items
            select (ListViewItem)item.Clone()).ToArray());





ListViewItem[] LVIARRY_CopiedListViewItems = new
                              ListViewItem[MainForm.lstsv.Items.Count];
   for (int INT_Count=0; INT_Count < LVIARRY_CopiedListViewItems.Length; INT_Count++)
   {
            LVIARRY_CopiedListViewItems[INT_Count] =                        (ListViewItem)MainForm.lstsv.Items[INT_Count].Clone();
   }
lstclnt.Items.AddRange(LVIARRY_CopiedListViewItems);





ListViewItem[] dest = new ListViewItem[MainForm.lstsv.Items.Count];
   MainForm.lstsv.Items.CopyTo(dest, 0);
   lstclnt.Items.AddRange(dest);



请有人帮我
* MainFormlstsv所在的Form的名称



Please Somebody help me out
*MainForm is the name of Form in which lstsv is located

推荐答案

Hi
假设您的服务器项目引用了客户端项目,并且这两个项目在同一解决方案中.

让我们说这两种形式都有一个名为listview1的表单和一个listview.

然后在服务器窗体上的按钮上单击,打开客户端窗体,然后单击另一个按钮,它将列表视图项从服务器复制到客户端列表视图,如下所示.

服务器表单代码

Hi
Assuming your server project has reference to the client project and these two projects are in the same solution.

Let us say both these forms having a form and listview named listview1.

Then in the server form on button click it opens the client form and then cliek another button, it copy the listview items from server to client list view as shown below.

Server form code

public partial class Sever : Form
 {
     private Client.Client clientFrm;
     public Sever()
     {
         InitializeComponent();
     }
     //opens client form
     private void button1_Click(object sender, EventArgs e)
     {
         clientFrm = new Client.Client();
         clientFrm.Show();
     }
     //calls the client method and pass server's listview
     private void button2_Click(object sender, EventArgs e)
     {
         clientFrm.UpdateListView(this.listView1);
     }



在客户端表单上有一个公共方法



On the client form have a public method

public void UpdateListView(ListView listView)
{
    foreach (ListViewItem item in listView.Items)
    {
        this.listView1.Items.Add((ListViewItem)item.Clone());
    }
}



这是从服务器调用的,并通过listview.

这种方法对您有用吗?



This is called from the server and pass the listview.

Is this way work for you?


如果两个列表视图在不同的解决方案中,则您不能简单地从一个列表复制并插入另一个列表.我建议将所有listviewitems放入List< ListviewItem>在一个解决方案项目中,对其进行序列化,然后:
一个.如果客户端和服务器在同一台计算机上运行,​​请在一个应用程序中将序列化列表保存到物理驱动器,然后在另一个应用程序中打开(监视文件夹以进行更改,以及是否保存了新文件或更新了现有文件,请反序列化该文件并插入它们定位到listview)
b.如果客户端和服务器在不同的计算机上运行,​​请通过套接字将序列化的字节数组发送到目标
If the two listviews are in different solutions, then you cannot simply copy from one and insert to other. I suggest getting all listviewitems to a List<ListviewItem> in one solution project, serialize it, and then:
a. If client and server runs on the same machine, save the serialized List to physical drive in one app, and open in other (monitor the folder for changes and if new file is saved or an existing one is updated, deserialize the file and insert them to target listview)
b. If client and server runs on different machines, send the serialized byte array to target by socketing


在c#中使用远程处理
在此示例中,创建一个类库并添加以下代码.我的项目名称是RemotableObject.请记下您的项目名称,因为您需要在客户端和服务器项目中引用此名称
For using remoting with c#
In this example create a class library and add the following code. My project name is RemotableObject. Note your project name because you need to refer this in your client and server projects
[Serializable]
public  class Remotable : MarshalByRefObject
{
    public static Delegate setListView;

    public  void Test(ArrayList list)
    {
        object[] args = new object[1];
        args[0] = list;
        setListView.DynamicInvoke(args);

    }

}


将该类库编译为dll文件.
现在创建一个服务器项目,以以下形式添加一个列表视图(例如listview1)(在此示例中为简单列表视图,没有子列表).
将引用添加到创建的dll文件中.本示例的名称使用的是RemotableObject;

您还需要添加System.Runtime.Remoting(在.Net选项卡中,添加引用",然后选择确定",然后单击确定").

添加一个按钮,并在该按钮的单击事件中...


Compile this class library in to the dll file.
Now create a server project, add a listview (say listview1) in the form (simple listview for this example, no sublist).
Add refernce to the dll file created.The namespce for my example is using RemotableObject;

You need to add also the System.Runtime.Remoting (Add reference-> in the .Net tab select this and click ok)

Add a button and in the button''s click event...

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        ChannelServices.RegisterChannel(new TcpChannel(), false);
    }
    catch { }

    Type requiredType = typeof(Remotable);

    //Relapce the local host to network ip incase the client runs at a network machine, or other remote server name
    // where the configured port is open and ACESSIBLE, not restricted by firewalls and other stuff
    Remotable remoteObject = (Remotable)Activator.GetObject(requiredType, "tcp://localhost:9998/ListService");

    ArrayList list = new ArrayList();
    foreach (ListViewItem item in listView1.Items)
    {
        list.Add(item.Text);
    }
    remoteObject.Test(list);
    remoteObject = null;
}


注意:将这些附加库用于服务器和客户端
使用System.Collections;
使用System.Runtime.Remoting;
使用System.Runtime.Remoting.Channels;
使用System.Runtime.Remoting.Channels.Tcp;
使用RemotableObject;

在客户窗体中添加一个列表视图,并具有以下代码

您还需要添加System.Runtime.Remoting(在.Net选项卡中,添加引用",然后选择确定",然后单击确定").

将对dll的引用也添加到该项目中,并具有上述引用..


Note: use these additional libraries for server and as well as for client
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using RemotableObject;

In the client form add a list view anf have the following code

You need to add also the System.Runtime.Remoting (Add reference-> in the .Net tab select this and click ok)

Add the reference to the dll to this project as well and have the above references..

public partial class Form1 : Form
{
    private delegate void setList(ArrayList list);
    private ArrayList list;
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        TcpChannel tcpChannel = new TcpChannel(9998);
        ChannelServices.RegisterChannel(tcpChannel, false);
        Type commonInterfaceType = typeof(Remotable);

        RemotingConfiguration.RegisterWellKnownServiceType(commonInterfaceType,
        "ListService", WellKnownObjectMode.SingleCall);
        Remotable.setListView = new setList(setListView);

    }

    public void setListView(ArrayList list)
    {
        this.list = list;
        if (listView1.InvokeRequired)
        {
            listView1.Invoke(new MethodInvoker(invoker));
        }
    }

    public void invoker()
    {
        foreach (String listItemText in this.list)
        {
            ListViewItem item = new ListViewItem(listItemText);
            listView1.Items.Add(item);
        }
        listView1.Refresh();

    }
}

;
首先启动客户端,因为客户端在侦听并且服务器访问它,所以它执行其方法来更新列表视图.
然后运行服务器....
单击具有远程代码的按钮(我们之前已添加).现在您可以看到客户端中的列表视图已更新

;
START the client first, because client listens and server access it execute its method to update the listview.
Then run the server....
click the button (we added earlier) which has the remoting code at its event. Now you can see the listview in the client get updated


这篇关于如何在C#中将项目从一个ListView复制到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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