我创建了一个FormControlTracker类,用于处理表单控件跟踪。这似乎按预期工作。现在,我想在按下保存按钮时从formControlTracker类中访问public 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();
}
最佳答案
您可以在课堂上将其设为字段
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