2017-08-28 53 views
2

我想創建一個名爲Filter的窗體。其餘形式將被稱爲其他形式。如何在WinForms應用程序中回調以前的表單?

對於實施例

我有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中仍然相同。所以我不希望爲所有形式

回答

3

嘗試這種解決方案如下創建對象..

  1. 與其說Show()呼叫ShowDialog()的。
  2. 在Filter窗體中爲屬性(public)指定必要的值,並使用Filter窗體的對象在Main窗體中訪問這些值。

主要形式:

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

的filter:

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

我有一個疑問,如果'(filter.ShowDialog()== DialogResult.OK)'。它會像對話框一樣執行嗎?如果是的話,我可以添加文本框,網格控件,列表框和4個按鈕點擊事件等組件嗎? –

+0

@mohamedfaiz是的。它的確如此。看到我更新的答案。 –

相關問題