C#-event handler - 将值从一种形式传递到另一种形式 [英] C# -event handler -passing value from one form to another
问题描述
嗨!
我创建了委托和事件,并尝试在form1上订阅事件,但如果我将事件订阅代码移动到form2那么它就会获得subscibe,那么就没有问题了。可以任何人找我plz。在此先感谢::::
Hi!
I have created delegate and event and try to subscribe event on form1 but it dot get subscibe if i will move event subscribe code to form2 then there is not problem. can any one have look for me plz. thanks in advance::::
_mainform(form1):
namespace KasseDelegate
{
public delegate void ListViewUpdatedEventHandler(object sender, ListViewUpdatedEventArgs e);
public partial class Form1 : Form
{
private Form3 frm3;
public Form1()
{
InitializeComponent();
frm3 = new Form3();
frm3.ListViewUpdated += new ListViewUpdatedEventHandler(Frm3_ListViewUpdated1);
}
private void Frm3_ListViewUpdated1(object sender, ListViewUpdatedEventArgs e)
{
MessageBox.Show("hi");
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
}
}
_searchform(form2):
_searchform(form2) :
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (SQLiteConnection con = new SQLiteConnection(Properties.Settings.Default.ConnectionString))
{
con.Open();
SQLiteCommand cmd = new SQLiteCommand("select * from varer where varenummer=@Varenummer", con);
cmd.Parameters.AddWithValue("@Varenummer", "101");
SQLiteDataReader dr = cmd.ExecuteReader();
Form3 frm3 = new Form3(dr);
frm3.Show();
}
}
}
表格3:
Form 3 :
namespace KasseDelegate
{
public partial class Form3 : Form
{
public event ListViewUpdatedEventHandler ListViewUpdated;
SQLiteDataReader dr1;
public Form3()
{
InitializeComponent();
}
public Form3(SQLiteDataReader dr)
{
InitializeComponent();
dr1 = dr;
}
private void Form3_Load(object sender, System.EventArgs e)
{
if (dr1 != null)
{
while (dr1.Read() == true)
{
ListViewItem LVI = new ListViewItem();
LVI.SubItems.Add(dr1[0].ToString());
LVI.SubItems.Add(dr1[1].ToString());
LVI.SubItems.Add(dr1[2].ToString());
LVI.SubItems.Add(dr1[3].ToString());
LVI.SubItems.Add(dr1[4].ToString());
listView1.Items.Add(LVI);
}
}
我的尝试:
我已经提交了代码,我已尽力找出问题,因为nd无法理解。 plz plz帮帮我
What I have tried:
I have submitted the code and i have tried my best to find out what is problem in that nd not able to understand. plz plz help me out
推荐答案
嗯......这很简单易懂。
你在Form1中创建一个Form3实例附上一个处理程序:
Well...that's pretty simple to understand.
You create an instance of Form3 in Form1 and you attach a handler:
frm3 = new Form3();
frm3.ListViewUpdated += new ListViewUpdatedEventHandler(Frm3_ListViewUpdated1);
但是你从不显示表单,所以实际的Listview永远不会更新。
相反,在Form2中你创建一个Form3的实例并显示它,但是你从不附加处理程序:
But you never show teh form, so the actual Listview never gets updated.
Conversely, in Form2 you create an instance of Form3 and show it, but you never attach a handler:
Form3 frm3 = new Form3(dr);
frm3.Show();
因此,该版本更新了ListView,并且(可能是你没有表明这样做)发出信号,但你还没有一个处理程序,所以没有任何反应。
看看这个:在两种形式之间传递信息,第2部分:儿童到父母 [ ^ ]
示例代码应该显示您要做什么。
So that version gets it's ListView updated, and (presumably, you don't show it doing that) signals the event, but you don't have a handler so nothing happens.
Have a look at this: Transferring information between two forms, Part 2: Child to Parent[^]
The sample code should show you what to do.
这篇关于C#-event handler - 将值从一种形式传递到另一种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!