在表单之间发送ListView数据

时间:2013-01-23 10:51:38

标签: c# winforms

嗨,我是C#的新手,我没有编程背景,但我很感兴趣。

我想将数据发送到ListView,但数据是另一种形式。我已经在这里看到了所有相关的帖子。我尝试从其中一个帖子中复制代码并根据我的需要进行更改,但它不起作用。

Form3:

public partial class Form3 : Form
{
    public Form3()
    {
        InitializeComponent();
    }

    public delegate void HandleItemAdded(object sender, ItemAddedEventArgs e);

    public struct ItemAddedEventArgs: EventArgs
    {
        public string PartPrefix;
        public string PartStartNumber;
        public string AssemblyPrefix;
        public string AssemblyStartNumber;
        public string Name;
        public string Profile;
        public string Material;
        public string Finish;
        public string Class;

        public ItemAddedEventArgs(string partprefix, string partstartnumber, string assemblyprefix, string assemblystartnumber, string name, string profile, string material, string finish, string classes)
        {
            PartPrefix = partprefix;
            PartStartNumber = partstartnumber;
            AssemblyPrefix = assemblyprefix;
            AssemblyStartNumber = assemblystartnumber;
            Name = name;
            Profile = profile;
            Material =  material;
            Finish = finish;
            Class = classes;
        }
    }

    public event HandleItemAdded ItemAdded;

    public void RaiseItemAdded(ItemAddedEventArgs e)
    {
        if (ItemAdded != null)
            ItemAdded(this, e);
    }

    public void AddToList()
    {  
        RaiseItemAdded (new ItemAddedEventArgs (textBox221.Text, textBox222.Text, textBox223.Text, textBox224.Text, textBox225.Text, textBox226.Text, textBox227.Text, textBox228.Text, textBox229.Text));
    }
}

Form1中:

public void HandleItemAdded(object sender, WindowsFormsApplication1.Form3.ItemAddedEventArgs e)
{
    ListViewItem item1 = new ListViewItem(textBox221.Text);
    item1.SubItems.Add(textBox222.Text);
    item1.SubItems.Add(textBox223.Text);
    item1.SubItems.Add(textBox224.Text);
    item1.SubItems.Add(textBox225.Text);
    item1.SubItems.Add(textBox226.Text);
    item1.SubItems.Add(textBox227.Text);
    item1.SubItems.Add(textBox228.Text);
    item1.SubItems.Add(textBox229.Text);
    listView1.Add(item1);

    Form3.ItemAdded += Form1.HandleItemAdded; *<-( i dont know if this is the correct place for this.)  
}

我得到的错误是:type EventArgs in interface list is not an interface

提前谢谢。

1 个答案:

答案 0 :(得分:0)

您需要在Form1中执行以下操作:

public void HandleItemAdded(object sender, WindowsFormsApplication1.Form3.ItemAddedEventArgs e)
        {

            ListViewItem item1 = new ListViewItem(e.PartPrefix);
            item1.SubItems.Add(e.PartStartNumber);
            item1.SubItems.Add(e.<Member_Name>);
.
.
.

            listView1.Add(item1);

        }  

对于错误,我认为以下内容适合您:

public class ItemAddedEventArgs: EventArgs