【问题】
折腾:
的过程中,主窗体写好了,在写另外一个子窗体时,需要获得主窗体中的一个Label控件的值。
【解决过程】
1.参考:
去给子窗体中添加一个函数,用于初始化当前的某些变量,即把需要的一些值,在主窗体new的时候,通过此函数赋值给子窗体中的变量,这样子窗体就可以使用了。
2.然后就写了如下代码:
主窗体中:...
namespace downloadSongtasteMusic
{
public partial class frmDownloadSongtasteMusic : Form
{
...
private string curFullFilename;
...
private void btnDownload_Click(object sender, EventArgs e)
{
...
//test open hint window
completeHint hintWindow = new completeHint();
hintWindow.initParameter(curFullFilename, txbSaveTo.Text);
hintWindow.Show();
}
...
}
子窗体中:...
namespace downloadSongtasteMusic
{
public partial class completeHint : Form
{
private string curFullFilename;
private string curFolderPath;
...
public void initParameter(string fullFilename, string folderPath)
{
curFullFilename = fullFilename;
curFolderPath = folderPath;
}
...
private void lklOpenFolder_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
//open folder
//System.Diagnostics.Process.Start("Explorer.exe","C:\\");
//System.Diagnostics.Process.Start("C:\\");
System.Diagnostics.Process.Start(curFolderPath);
//System.Diagnostics.Process.Start("Explorer.exe", txbSaveTo.Text);
}
}
}
【总结】
通过使用new去初始化新窗体之前,通过函数实现赋值,如此即可实现:
从一个(子)窗体(completeHint)中,获得另外一个(父)窗体中的参数(txbSaveTo.Text,即赋值给子窗体中的curFolderPath)。