如何在Winforms应用程序中回调上一个表单?

时间:2017-08-28 12:39:15

标签: c# .net winforms

我想创建一个名为Filter的表单。所有形式的其他形式都会调用该表格。

例如

  

我有10个表单和一个过滤器表单。我在所有10个表单中都有一个名为过滤器的按钮。因此,每当用户单击“过滤器”按钮时,将调用Filter form并传递值

ReportForm1

//Send Values to Filter Form
private void OnButton1Click(object sender, EventArgs e)
{
    this.Hide();
    FilterForm filter = new FilterForm(txtFieldName.Text,txtValues.Text);
    filter.Show();
}

//Get back the values from Filter Form
public ReportForm1(string x, string y)     
{
    s1 = x;
    s2 = y;

    // I will do some process after I get back the values from Filter Form
}

过滤表格

public filter(string FieldName, string Values)     
{
    s1 = FieldName;
    s2 = Values;

    // I will do some process after I get back the values from Filter Form
}

private void OnSubmitClick(object sender, EventArgs e)
{
    this.Hide();

    //it has to send two variables to previous form.
}

我将在Filter Form中添加一些组件,如文本框,组合框,列表,网格和一些按钮单击功能。最后,当用户点击提交按钮时,它应该向Previous表单发送一些值。

注意

  

请不要建议我拨打ReportForm1 report1=new ReportForm1(x,y)这样的表格。我期待它必须调用以前的表格。因为当我创建一个新表格ex。 ReportForm2,该函数在FilterForm中仍然相同。所以我不想为所有表单创建对象

1 个答案:

答案 0 :(得分:3)

尝试以下解决方案..

  1. 而不是致电Show()来电ShowDialog()
  2. 在Filter窗体中为属性(public)分配必要的值,并使用Filter窗体的对象访问Main窗体中的值。
  3. 主要表格:

    private void OnButton1Click(object sender, EventArgs e)
    {
        FilterForm filter = new FilterForm(txtFieldName.Text,txtValues.Text);
        if (filter.ShowDialog() == DialogResult.OK)
        {
            TextBox a = filter.a; //Here you can able to access public property from Filter form.
        }
    }
    

    过滤表格

    public class FilterFom
    {
        public TextBox a { get; private set; }
    
        public filter(string FieldName, string Values)     
        {
            s1 = FieldName;
            s2 = Values;
            a = new TextBox(); //Here I can assign value to public property of this class.
        }
    
        private void OnSubmitClick(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }