我创建了一个处理表单控件跟踪的formControlTracker类。这似乎按预期工作。现在,我想在按下保存按钮时从formControlTracker类中访问公共string [] getDirtyControls()方法。我试图访问cTracker.getDirtyControls(),总线作为代码站,我认为我实例化的类formControlTracker是在范围之外。如何在不重新实例化formControlTracker类的情况下访问getDirtyControls()方法?
代码段:
public partial class settingsForm : Form
{
public settingsForm()
{
InitializeComponent();
settingsTabControl.Appearance = TabAppearance.Buttons;
settingsTabControl.SizeMode = TabSizeMode.Fixed;
settingsTabControl.ItemSize = new System.Drawing.Size(0,1);
formControlTracker cTracker = new formControlTracker(this);
wLogSettingsFormProperties settingsFormProp = new wLogSettingsFormProperties();
}
public void lDirtyControls()
{
/*foreach (string con in cTracker.getDirtyControls())
{
MessageBox.Show(con);
}*/
}
public void saveSettingsButton_Click(object sender, EventArgs e)
{
lDirtyControls();
}
答案 0 :(得分:4)
你可以把它变成课堂上的一个字段
public partial class settingsForm : Form
{
private formControlTracker _cTracker;
public settingsForm()
{
//set the field value in the constructor.
_cTracker = new formControlTracker(this);
}
public void lDirtyControls()
{
//use the field variable here
/*foreach (string con in _cTracker.getDirtyControls())
{
MessageBox.Show(con);
}*/
}
}
请阅读此处以获取有关c#中字段的更多信息:
http://msdn.microsoft.com/en-us/library/ms173118.aspx