C#-Event Handler - 从一个表单到另一个表单的值

时间:2016-07-07 09:28:30

标签: c#

我正在开发窗口应用程序POS。要求是:当用户点击'seach item'上的按钮_mainform(form 1)时,它会打开_Searchform(form2),然后在搜索表单上显示结果listview,并在表单3上选择项目按钮并关闭{ {1}}。我们从_searchform(form2)中选择的项目,我们在listview中添加。

我尝试使用委托和事件实现此功能。在_mainform(form1) listview(搜索结果表单)上,我已经声明了委托和事件,并且甚至在form1(主表单)上订阅了它。但是当我在form3点上运行应用程序事件时,会被解雇。

遵循此代码:

form 1

表格3:

 _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) :




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

那么我如何从list3(displayitem)获取listview所选项的值到form1。(mainform)。

2 个答案:

答案 0 :(得分:2)

如果我理解正确,您Form1会启动Form2,而Form3会启动Form3。在Form1上选择某个项目时,您需要在Form1上重新更新。

所以在Form2上声明一个委托。将此委托传递给Form2 [Form2构造函数应该接受此作为默认参数 - 这样如果从其他地方触发Form2,我们就不需要对委托有严格的依赖。]

Form3上维护此委托类型的私有变量,并在启动Form3时将同一委托传递给Form3构造函数。

From1上,您现在拥有一个代表,该代表持有Form3上方法的引用,因此每当选择一个项目时,您都可以在Form1上分配此代理,这将触发 public delegate void MyDelegate(string selectedItem); public class Form1 { private MyDelegate delegate1; public Form1() { delegate1 = new MyDelegate(ShowSelectedItem); var form2 = new Form2(delegate1); } public void LaunchForm2() { } private void ShowSelectedItem(string result) { } } public class Form2 { private MyDelegate form2Delegate; public Form2(MyDelegate del = null) { form2Delegate = del; var form3 = new Form3(form2Delegate); } public void LaunchForm3() { } } public class Form3 { private MyDelegate form3Delegate; public Form3(MyDelegate del = null) { form3Delegate = del; SelectedItemTriggered("tes"); } public void SelectedItemTriggered(string selectedItem) { form3Delegate(selectedItem); //This will trigger method ShowSelectedItem of Form1 } } 上的方法。

所以这是一个有效的例子。

=IF(OR(E(B32>0;H32>0;N32>0;B35>0;D35>0;F35>0;H35>0;F35>=H24;F35<H35;D35>B35;N32>=N21);AND(B32="";H32="";N32="";H35="";F35="";D35="";B35=""));"";"Inválido")

答案 1 :(得分:0)

您可以在Form3中设置一个活动,然后从外部访问该活动:

public partial class Form2 : Form
{
    public Form2()
    {
        Form3 new3 = new Form3();
        // Access the event of form3 from outsite
        new3.DisplayedItemChanged += ItemChanged;
    }

    public void ItemChanged(object sender, EventArgs e)
    {
        // This will be triggered.
    }
}

public class Form3 : Form
{
    // Create an event
    public event EventHandler DisplayedItemChanged;

    public void button1_Click(object sender, EventArgs e)
    {
        if (this.DisplayedItemChanged != null)
        {
            // Raise the event and pass any object
            this.DisplayedItemChanged(yourObjectToPass, e);
        }
    }
}

根据您的评论:

public partial class Form1 : Form
{
    public Form1()
    {
        // This one is an instance of Form3
        Form3 newForm = new Form3();
        newForm.SomethingHappens += RaiseHere;
    }

    public void RaiseHere(object sender, EventArgs e)
    {
        // Do something...
    }
}

public partial class Form2 : Form
{
    public Form2()
    {
        // This one is NOT the same as on Form1. Its a NEW form.
        Form3 newForm = new Form3();
        newForm.Show();
    }
}

public partial class Form3 : Form
{
    public event EventHandler SomethingHappens;

    public Form3()
    {
        //...
    }
}

这不会工作,永远不会。你必须使用相同的实例:

Form3 newForm = new Form3();
newForm.SomethingHappens += RaiseHere;
newForm.Show();

如果这对你来说不清楚,我无法帮助,抱歉:

enter image description here