2012-03-05 139 views
1

我有一個帶有文件和菜單的列表框的應用程序。當我從列表框中右鍵單擊一個項目時,我有一個菜單例如發送。當我按'發送'時,我想要打開另一個窗口(我已經有了新窗口),並且在新窗口中我想要選擇項目路徑(我在主窗口中有此路徑)。如何從主GUI打開新窗口,並將其傳遞給新窗口

private void MenuItemSend_Click(object sender, RoutedEventArgs e) 
{    
    if (listBoxFiles.SelectedIndex == -1) 
    { 
     return; 
    } 

    string filePath = (listBoxFiles.SelectedItem).ToString(); --- my file path 
    StatisticsWindow sForm = new StatisticsWindow(); 
    sForm.ShowDialog(); -- open the new window 
} 

我該怎麼辦?

謝謝

+0

定義'IpStatisticsWindow'額外的構造函數,它接受一個'字符串filePath' – 2012-03-05 18:21:55

回答

4

爲什麼不爲窗口創建構造函數?

而不是

new IpStatisticsWindow(); 

這樣的:

new IpStatisticsWindow(filePath); 
// In the IpStatisticsWindow class 
public IpStatisticsWindow(string path) 
{ 
    //do something with path 
} 

你當然也可以創建一個屬性或處理它的方法,那麼你可以通過它在那裏,例如

IPsForm.Path = filePath; 
IPsForm.HandlePath(filePath); 
+0

感謝您的快速重播! – user979033 2012-03-05 18:25:51

+1

@ user979033:很高興提供幫助;如果這回答你的問題,你可以[接受](http://meta.stackexchange.com/questions/5234)它。 – 2012-03-05 18:41:13