我正在用 C# 编写一个 Windows 窗体应用程序。我将设计更改为有菜单。我有每个菜单项的基类和几个子类。我的问题是在我的基类中,我正在访问一个 GUI 元素并将其值存储在一个公共变量中。现在我想从我的子班访问这个。
public partial class x: Form
{
# calling this public method from child class to to get the variable value
public string Getlogpath()
{
Console.WriteLine(this.logpath);
return logsdirectory.Text;
}
private void reportFromLogsToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 child1 = new Form2();
this.LayoutMdi(System.Windows.Forms.MdiLayout.Cascade);
child1.Show();
}
public void browse_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.SelectedPath = (@"\\comprion02\ots\SHARED\T\COMPRION SIMfony\Log-Files");
fbd.ShowDialog();
logsdirectory.Text = fbd.SelectedPath;
logpath = logsdirectory.Text;
# this print i get value i need
Console.WriteLine(logpath);
}
}
#child form class
public partial class Form2 : Form
{
private void button1_Click(object sender, EventArgs e)
{
string data;
x obj = new x();
data = obj.Getlogpath();
#got nothing for this print
Console.WriteLine(x);
}
}
有人可以帮我解决这个问题。