我有一个表单,其中填充了从主表单创建的一组 userControl。一旦按下按钮时弹出窗口关闭,我需要能够从主窗体访问这个 userControls 数组。如果我填写表单,然后在不关闭弹出窗口的情况下按下主表单上的按钮,则存在值。但是,如果我关闭弹出窗口,则值不存在。我的主要形式是静态的,所以我可以在其他形式中使用它的变量。
弹出窗口的代码:
public ScanChannel[] controls;
public ScanListSetup()
{
InitializeComponent();
int numChans = Convert.ToInt32(Form1.Self.numChannels.Text);
controls = new ScanChannel[numChans];
// Create the UserControls
for(int i = 0; i < numChans; i++)
{
controls[i] = new ScanChannel();
}
// Place them
for (int i = 0; i < numChans; i++)
{
controls[i].Location = new Point(13,(35+25*(i)));
this.Controls.Add(controls[i]);
}
doneButton.Location = new Point(82, 35 + (25 * (numChans + 1)));
this.Size =new Size(280, 110 + (25 * (numChans + 1)));
}
private void doneButton_Click(object sender, EventArgs e)
{
Form1.Self.setChannelsToScan(controls);
}
我需要访问controls主窗体中的数组。主窗体的代码如下:
private ScanChannel[] channelsToScan;
private void configureScanListButton_Click(object sender, EventArgs e)
{
var form = new ScanListSetup();
form.Show(this);
scanListConfigured = true;
this.channelsToScan = new ScanChannel[Convert.ToInt32(numChannels.Text)];
}
public void setChannelsToScan(ScanChannel[] arr)
{
for (int i = 0; i < arr.Length; i++)
{
this.channelsToScan[i] = arr[i];
}
}
private void scanButton_Click(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("Test: " + this.channelsToScan[0].getDeviceType());
// THIS PRINTS AN EMPTY STRING
}
因此,如果scanButton在弹出窗体仍然打开时单击 Debug writeLine 会输出正确的值。但是,如果我在单击doneButton弹出窗体上的后关闭窗体,则 Debug writeLine 输出Test:没有其他内容。
对此的任何帮助将不胜感激。谢谢。
相关分类