簡體   English   中英

如何在Winforms應用程序中調用以前的表單?

[英]How to call back the previous form in Winforms application?

我想創建一種稱為過濾器的表格。 該表格將被所有其他表格調用。

例如

我有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.
}

我將在“過濾器表單”中添加一些組件,例如文本框,組合框,列表,網格和一些按鈕單擊功能。 最后,當用戶單擊提交按鈕時,它應該將一些值發送到上一個表單。

注意

請不要建議我致電ReportForm1 report1=new ReportForm1(x,y)類的表格。 我希望它必須調用以前的表格。 因為當我創建新表格時, ReportForm2,該功能在FilterForm仍然相同。 所以我不想為所有表單創建對象

請嘗試以下解決方案。

  1. 而不是調用Show()而是調用ShowDialog()
  2. 在“過濾器”表單中為屬性(公共)分配必要的值,並使用“過濾器”表單的對象在“主要”表單中訪問這些值。

主要形式:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM