C# 由另一个窗体确定的全局数组大小

C# 由另一个窗体确定的全局数组大小,c#,winforms,C#,Winforms,我试图创建一个全局二维数组,它的大小取决于在另一个表单文本框中输入的两个值。但是,我得到一个错误,说这些值不是静态的 public partial class Form2 : Form { Form1 frm = new Form1(); PictureBox[,] MyArray = new PictureBox[Convert.ToInt32(frm.textbox1.text), Convert.ToInt32(frm.textbox1.text)]; } 由于这些文本框

我试图创建一个全局二维数组,它的大小取决于在另一个表单文本框中输入的两个值。但是,我得到一个错误,说这些值不是静态的

public partial class Form2 : Form
{
    Form1 frm = new Form1();
    PictureBox[,] MyArray = new PictureBox[Convert.ToInt32(frm.textbox1.text), Convert.ToInt32(frm.textbox1.text)];
}
由于这些文本框不是静态的(其中的值可以更改),因此给出了错误。我尝试了多种方法来绕过这个问题;我尝试从这些文本框初始化一个常量,但它给出了相同的错误,我也尝试过调整数组的大小,但是数组。调整大小不起作用,因为它是多维的和数组。复制不起作用,因为我需要数组是全局的

为了让您了解我要做的事情,在第一个表单Form1中,用户输入宽度和长度值。然后用户按下确认键,Form2打开。第二个表单Form2将有一个数组,其大小由用户输入的值决定。此数组将用于处理网格,该网格也由用户输入的值确定


如何绕过非静态错误并使用这些值创建数组?

由于尚未构造实例,因此无法在字段初始值设定项中引用实例(包括实例字段)


将代码移动到
表单1
构造函数

由于尚未构造实例,因此无法在字段初始值设定项中引用实例(包括实例字段)


将代码移动到
Form1
构造函数

在Form2上实例化“frm”时,您并不是通过创建Form1类型的新实例来引用现有表单。(此时您不需要“.show()”)

假设您是从form1启动form2,看起来像这样:

protected void launchForm2()
      {
         Form2 form2 = new Form2();
         form2.Show();
      }

// you need to change that to look like this:

protected void launchForm2()
      {
         Form2 form2 = new Form2();
         form2.Parent = this;
         form2.Show();
      }
//added to declare myArray global to the form.
PictureBox[,] MyArray;

// to make it 'global' to the application you will need to create some manner of "globalApplicationContext" and pass references of that around or use a 'baseForm' that holds a reference... there are (of course) other solutions as well to globalizing it..

      protected void updateArray()
      {
        string textFromForm1 = ((Form1)this.Parent).textBox1.Text;
// remarked out after @Justin's comment...thx.        
//PictureBox[,] MyArray = new PictureBox[Convert.ToInt32(textFromForm1), Convert.ToInt32(textFromForm1)];

MyArray = new PictureBox[Convert.ToInt32(textFromForm1), Convert.ToInt32(textFromForm1)];
      }
然后在表格2中,您需要这样称呼“家长”:

protected void launchForm2()
      {
         Form2 form2 = new Form2();
         form2.Show();
      }

// you need to change that to look like this:

protected void launchForm2()
      {
         Form2 form2 = new Form2();
         form2.Parent = this;
         form2.Show();
      }
//added to declare myArray global to the form.
PictureBox[,] MyArray;

// to make it 'global' to the application you will need to create some manner of "globalApplicationContext" and pass references of that around or use a 'baseForm' that holds a reference... there are (of course) other solutions as well to globalizing it..

      protected void updateArray()
      {
        string textFromForm1 = ((Form1)this.Parent).textBox1.Text;
// remarked out after @Justin's comment...thx.        
//PictureBox[,] MyArray = new PictureBox[Convert.ToInt32(textFromForm1), Convert.ToInt32(textFromForm1)];

MyArray = new PictureBox[Convert.ToInt32(textFromForm1), Convert.ToInt32(textFromForm1)];
      }

这将使您的引用保持有序…

当您在Form2上实例化“frm”时,您并不是通过创建Form1类型的新实例来引用现有的表单。(此时您不需要“.show()”)

假设您是从form1启动form2,看起来像这样:

protected void launchForm2()
      {
         Form2 form2 = new Form2();
         form2.Show();
      }

// you need to change that to look like this:

protected void launchForm2()
      {
         Form2 form2 = new Form2();
         form2.Parent = this;
         form2.Show();
      }
//added to declare myArray global to the form.
PictureBox[,] MyArray;

// to make it 'global' to the application you will need to create some manner of "globalApplicationContext" and pass references of that around or use a 'baseForm' that holds a reference... there are (of course) other solutions as well to globalizing it..

      protected void updateArray()
      {
        string textFromForm1 = ((Form1)this.Parent).textBox1.Text;
// remarked out after @Justin's comment...thx.        
//PictureBox[,] MyArray = new PictureBox[Convert.ToInt32(textFromForm1), Convert.ToInt32(textFromForm1)];

MyArray = new PictureBox[Convert.ToInt32(textFromForm1), Convert.ToInt32(textFromForm1)];
      }
然后在表格2中,您需要这样称呼“家长”:

protected void launchForm2()
      {
         Form2 form2 = new Form2();
         form2.Show();
      }

// you need to change that to look like this:

protected void launchForm2()
      {
         Form2 form2 = new Form2();
         form2.Parent = this;
         form2.Show();
      }
//added to declare myArray global to the form.
PictureBox[,] MyArray;

// to make it 'global' to the application you will need to create some manner of "globalApplicationContext" and pass references of that around or use a 'baseForm' that holds a reference... there are (of course) other solutions as well to globalizing it..

      protected void updateArray()
      {
        string textFromForm1 = ((Form1)this.Parent).textBox1.Text;
// remarked out after @Justin's comment...thx.        
//PictureBox[,] MyArray = new PictureBox[Convert.ToInt32(textFromForm1), Convert.ToInt32(textFromForm1)];

MyArray = new PictureBox[Convert.ToInt32(textFromForm1), Convert.ToInt32(textFromForm1)];
      }


这将使您的引用保持有序…

其他人已经指出了解决方案,因此这只是后续行动。表单之间的通信很快就会变得非常混乱。无论如何,你在表单上放置了太多的逻辑。业务逻辑应该与GUI分离。查看模型-视图-控制器模式。

其他人已经指出了解决方案,因此这只是后续内容。表单之间的通信很快就会变得非常混乱。无论如何,你在表单上放置了太多的逻辑。业务逻辑应该与GUI分离。查看模型-视图-控制器模式。

您的代码应该可以正常工作。这个错误的确切内容是什么?@Ramhound:这没什么错。将
新图片框[Convert.ToInt32(“42”),Convert.ToInt32(“5”)粘贴到LINQPad中查看。您手动键入了值。您无法获取用户输入并使用它初始化数组(问题在于
frm.textbox1.text
)。您复制和测试的内容与OP发布的内容不一样。@SpikeX:您在这两方面都完全错了。数组大小可以是任意表达式。C#编译器不知道Convert.ToInt32是什么意思;一旦它存在,它就不是一个常数。我承认我错了。我认为这是因为编译器无法确定它应该初始化的数组的大小(我认为这仍然是一个准确的语句)。除了代码本身的位置之外,代码没有任何问题。出于很多原因,我已经停止使用数组,转而使用纯泛型集合。您的代码应该可以正常工作。这个错误的确切内容是什么?@Ramhound:这没什么错。将
新图片框[Convert.ToInt32(“42”),Convert.ToInt32(“5”)粘贴到LINQPad中查看。您手动键入了值。您无法获取用户输入并使用它初始化数组(问题在于
frm.textbox1.text
)。您复制和测试的内容与OP发布的内容不一样。@SpikeX:您在这两方面都完全错了。数组大小可以是任意表达式。C#编译器不知道Convert.ToInt32是什么意思;一旦它存在,它就不是一个常数。我承认我错了。我认为这是因为编译器无法确定它应该初始化的数组的大小(我认为这仍然是一个准确的语句)。除了代码本身的位置之外,代码没有任何问题。出于很多原因,我已经停止使用数组,转而使用纯泛型集合。这是非常正确的,但这是一个单独的问题。此外,由于他想要一个全局数组,所以实际上不应该(重新)在内部声明MyArrayupdateArray@Slaks,在他发布的代码示例中,他对Form1的引用刚刚实例化,尚未显示,因此,他试图通过类型而不是实例访问属性。(因此非静态错误…)这怎么不是问题?@Justin,你是对的,我刚从他的代码中提取了这一行。更关心的是对Form1的错误引用,而不是对正在填充的数组的错误引用,这很好。@Cos:这是他的问题。然而,这并不是他在这个问题上所问的问题。现在他解决了这个问题,这就是他当前的问题。这是非常正确的,但这是一个单独的问题。而且,因为他想要一个全局数组,所以MyArray实际上不应该(重新)在内部声明updateArray@Slaks,在他发布的代码示例中,他对Form1的引用刚刚实例化,尚未显示,因此,他试图通过类型而不是实例访问属性。(因此非静态错误…)这怎么不是问题?@Justin,你是对的,我刚从他的代码中提取了这一行。更加关注fau