我正在用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);
}
}有人能帮我一下吗。
发布于 2013-03-26 18:50:01
使用此Console.WriteLine(Form.Getlogpath());而不是Console.WriteLine(x);
或者创建Form类的对象,而不是X类:
Form obj = new Form();
Console.WriteLine(obj.Getlogpath());发布于 2013-03-26 18:53:28
为什么不使用关键字protected,而不是private或public。更多信息请点击此处- http://msdn.microsoft.com/en-us/library/wxh6fsc7(v=vs.71).aspx
https://stackoverflow.com/questions/15635073
复制相似问题