Skip to main content
 首页 » 编程设计

c#之如何从主 GUI 打开新窗口并将变量传递给新窗口

2024年02月23日23linjiqin

我有一个带有包含文件和菜单的列表框的应用程序。当我从我的列表框中右键单击一个项目时,我有一个菜单,例如发送。当我按下“发送”时,我想要打开另一个窗口(我已经有了新窗口),并且在新窗口中我想要我选择的项目路径(我在主窗口中有这个路径)。

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 
}  

我该怎么做?

谢谢

请您参考如下方法:

为什么不为窗口创建构造函数?

代替

new IpStatisticsWindow(); 

这个:

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

你当然也可以创建一个属性或一个处理它的方法,然后你可以将它传递到那里,例如

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