【问题标题】:How to reach variables from initializing method in WinForms?如何从 WinForms 中的初始化方法中获取变量?
【发布时间】:2014-04-18 10:34:06
【问题描述】:

比标题更清楚地解释我的问题。这是一个代码示例:

public partial class TestForm : Form
{
    public static List<PictureBox> listPictureBox;

    public TestForm()
    {
        InitializeComponent();
        PictureBox[] pictureBoxArray = {pictureBox1, pictureBox2, pictureBox3};
    }

    public static bool testMethod
    {
        listPictureBox = new List<PictureBox>();

        for(int i = 0; i < ?????; i++) //The questionmarks should be pictureBoxArray.Length, but I don't know how to reach the code.
        {
            listPictureBox.Add(?????[i]; //Same here, the questionmarks should be pictureBoxArray.
        }
    }

我希望让问题更清楚。

【问题讨论】:

  • 您不能从静态上下文访问实例变量。你到底想用 listPictureBox 实现什么?
  • 我希望能够使用列表切换图片框的颜色。这应该由另一个类处理,并将发送执行此操作的 bool。
  • 好的。所以你的问题几乎可以肯定是你的方法(和你的领域)上的“静态”关键字。如果您想更改 TestForm 的特定实例 的颜色,尝试从静态上下文中执行此操作可能会徒劳无功。我建议阅读“静态”的含义,以便确保您理解正确 (msdn.microsoft.com/en-us/library/98f28cdx.aspx),然后找出一种方法来获取对您的 TestForm 的特定实例的引用,以指向正在执行操作的对象。跨度>

标签: c# arrays winforms list methods


【解决方案1】:

问题在于List&lt;PictureBox&gt; 和对应的testMethod 的静态关键字。

静态变量或静态方法属于该类的每个实例。
因此他们无法访问特定于每个类的实例变量。

作为第一次尝试,您应该将您的班级更改为

public partial class TestForm : Form
{
    public static List<PictureBox> listPictureBox;

    // Make this instance variable public 
    public  PictureBox[] pictureBoxArray;

    public TestForm()
    {
        InitializeComponent();

        // prepare the array with the 3 local pictureboxes
        pictureBoxArray = new PictureBox[] {pictureBox1, pictureBox2, pictureBox3};
    }

    // Calling this method requires that you pass the form instance where the 3 pictureboxes 
    // have been created
    public static bool testMethod(TestForm instance)
    {
        listPictureBox = new List<PictureBox>();

        for(int i = 0; i < instance.pictureBoxArray.Length; i++) 
        {
            listPictureBox.Add(instance.pictureBoxArray[i]; 
        }
    }

}

你在没有指定实例的情况下调用这个方法

TestForm t = new TestForm();
TestForm.testMethod(t);

然而此时我在问自己你是否真的需要这段代码.....

【讨论】:

  • 这给了我错误:“名称'实例'在当前上下文中不存在。
  • 你在testMethod中添加参数TestForm instance了吗?
  • 为了更清楚地解释我想做的事情,我想使用bool方法改变列表中四个第一个索引的颜色,第二次调用它时会是索引 4-8 等(在我的例子中只有 3 个索引,但我有 50 个)。
  • 嗯,这是一个不同的问题。您需要另一个变量来跟踪调用之间数组中的实际更改位置。现在,如果这个答案解决了您最初的问题,我建议您写一个新问题来解释第二个问题。
  • 添加我没有做过的 TestForm 实例,给了我一个新的错误。指向TestForm.testMethod(t)的“非静态字段、方法或属性需要对象引用”;
【解决方案2】:

试试这个:

public partial class TestForm : Form
{
     public List<PictureBox> listPictureBox;
     PictureBox[] pictureBoxArray = default(PictureBox[]);

     public TestForm()
     {
         InitializeComponent();
         pictureBoxArray = new PictureBox[] {pictureBox1, pictureBox2, pictureBox3};
     }

     public bool testMethod()
     {
           listPictureBox = new List<PictureBox>();

         for(int i = 0; i < pictureBoxArray.length; i++) 
         {
             listPictureBox.Add(pictureBoxArray[i]); 
         }

         return false;
     }
}

【讨论】:

  • 不,这不行。我收到错误消息“非静态字段、方法或属性需要对象引用。”
猜你喜欢
  • 2017-02-23
  • 1970-01-01
  • 2020-07-26
  • 2017-10-31
  • 2022-10-20
  • 1970-01-01
  • 2019-08-02
  • 2017-03-29
  • 1970-01-01
相关资源
最近更新 更多