2011-04-07 50 views
0

我正在嘗試執行它從數據庫加載一些信息。傳遞給已經打開的表單

爲此,我打開一個表單,列出可以加載的所有內容。

當你點擊加載時,我想把ID傳回原始表單。

但是我似乎無法調用這種形式的方法。

任何幫助,將不勝感激。

+0

你能告訴我們你在哪裏試圖調用窗體上的方法的代碼? – 2011-04-07 13:05:02

回答

1

我會翻轉過來,這樣的:

  1. 作出選擇的形式加入所創建並要加載的東西
  2. 通過暴露在對話框中所做的選擇形式顯示一個模式對話框在該對話框的形式

這樣的選擇形式將呼叫者不會脫鉤,無論在哪裏,是有道理的,而不需要修改它可以重複使用屬性或方法

在選擇對話框中窗體類:

public string GetSelectedId() 
{ 
    return whateverIdThatWasSelected; 
} 

在調用形式:

using(var dlg = new SelectionDialogForm()) 
{ 
    if (dlg.ShowDialog() == DialogResult.OK) 
    { 
     DoSomethingWithSelectedId(dlg.GetSelectedId()); 
    } 
} 
0

您可以將屬性添加到您的窗體類,並從其他形式引用它。

例如。

public class FormA : Form 
{ 

private string _YourProperty = string.empty; 

public string YourProperty 
{ 
get 
{ 
    return _YourProperty; 
} 
set 
{ 
    _YourProperty = value; 
} 
} 

} 

public class FormB : Form 
{ 

public void ButtonClick(object sender, EventArgs args) 
{ 
using (FormA oForm = new FormA) 
{ 
    if (oForm.ShowDialog() == DialogResult.OK) 
    { 
    string Variable = oForm.YourProperty; 
    } 
} 
} 

你只需要設置對形式的一個按鈕,點擊你的財產,那麼你可以從B型 }訪問

0

爲什麼在對話形式不能創建所選項目的公共屬性,像這樣的東西。

public int SelectedItemId {get;private set;} 

//In your item selected code, like button click handler.. 
this.SelectedItemId = someValue; 

然後,只需打開窗體作爲對話框

//Open the child form 
using (var form = new ChildForm()) 
{ 
    if (form.ShowDialog(this) == DialogResult.OK) 
    { 
     var result = form.SelectedItemId;//Process here.. 
    } 
} 
0

正確的方式做,這是介紹這是用來通過兩種形式控制器類。然後,您可以使用Controller上的屬性,設置該屬性將觸發NotifiyPropertyChanged事件。

看到INotifyPropertyChanged更多信息

相關問題