1

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

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

表格3:

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));
    }
}

表格1:

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

先感谢您。

4

1 回答 1

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
于 2013-01-23T10:57:24.337 回答