1
嗨,我是C#的新手,我沒有編程背景,但我對它感興趣。在窗體之間發送ListView數據
我想發送數據到一個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
預先感謝您。
喜mihirj,感謝您的回覆,錯誤現在沒有出現,但結果是一樣的。它不起作用。請幫忙。 – drax
是否調試過,並看到e.PartPrefix和其他變量的值?你也確定你需要使用Form3.ItemAdded而不是ListView.ItemAdded事件? – mihirj