2012-07-16 61 views
0

我试图创建程序的一部分,其中用户按下按钮并将图像放置在9个图片框之一中。每次点击按钮时,都应该选择不同的图片框。然而,在我开始之前,我无法让我的方法看到我想要传递的数组。将数组传递给事件C中的方法#

我有2个数组,插槽和插槽使用,我试图在程序启动时将它们初始化。但是,当我尝试将它们传递给在“Button1”内调用的方法“randomBox”时,Visual Studio称它们不存在。我如何使这些数组在我的代码中可见?

非常感谢 安东尼

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace pin_program 
{ 
    public partial class Mainscreen : Form 
    { 
     //Sets where users files are to be stored (for later use) 
     string activeDir = @"C:\Users\Tony\Downloads\Programs\pin program\Users"; 

     public Mainscreen() 
     { 
      InitializeComponent(); 
     } 

     //method to generate random number 
     private int RandomNumber(int min, int max) 
     { 
      Random random = new Random(); 
      return random.Next(min, max); 
     } 

     public void randomBox(int pictureVal, PictureBox[] Slots, bool[] SlotsUsed) 
     { 
      //generate random number 
      int j = RandomNumber(0, 9); 

      if (SlotsUsed[j] == false) 
      { 
       // Create image, assign it and set slots value to used 
       Image newImage = Image.FromFile(@"C:\Users\Tony\Downloads\Programs\pin program\pin program\pin program\Images\" + pictureVal + ".jpg"); 
       Slots[j].Image = newImage; 
       SlotsUsed[j] = true; 
      } 
      else 
       do 
       { 
        j = RandomNumber(0, 9); 
       } while (SlotsUsed[j] == false); 

      return; 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      //for use later 
      string userName = textBox1.Text; 
      //for use later 
      label1.Visible = true; 

      //test call of method. 
      randomBox(1, Slots, SlotsUsed); 
     } 

     public void Mainscreen_Load(object sender, EventArgs e) 
     { 
      //array for slots 
      PictureBox[] Slots = new PictureBox[9]; 
      Slots[0] = pictureBox1; 
      Slots[1] = pictureBox2; 
      Slots[2] = pictureBox3; 
      Slots[3] = pictureBox4; 
      Slots[4] = pictureBox5; 
      Slots[5] = pictureBox6; 
      Slots[6] = pictureBox7; 
      Slots[7] = pictureBox8; 
      Slots[8] = pictureBox9; 

      //array for used slots 
      bool[] SlotsUsed = new bool[9]; 
      for (int i = 0; i != (SlotsUsed.Length); i++) 
      { 
       SlotsUsed[i] = false; 
      } 
     } 
    } 
} 

编辑: 我不似乎能够发表评论,出于某种原因,所以我只是在这里问。我将如何将数组声明为实例变量而不是本地?实例变量是否有另一个我可能知道的名字?

欢呼

+0

您可以参考[此(作用域在MSDN(C#))](http://msdn.microsoft.com/en-us/ library/aa691132(v = vs.71).aspx) – 2012-07-16 16:02:14

回答

2

目前你在Mainscreen_Load声明SlotsSlotsUsed当地变量。他们需要在您的表单中使用实例变量,否则您无法在其他地方引用它们 - 事实上,它们在其他地方不会在逻辑上存在。它们是表单状态的一部分,所以它们应该是实例变量。

此外,您的生成随机数的方法被破坏 - 请参阅我的article on random numbers了解更多信息。

我还想补充一点,你可能会考虑只使用一个集合,将它打乱并开始,然后随时移除项目 - 通过这种方式,您可以轻松地分辨何时您已经用完了图像,并且您不必循环,直到找到未使用的插槽。

+0

你能提供一个快速的例子来说明如何做到这一点,它听起来很有用,但是我找不到一个例子。Cheers – 2012-07-16 16:36:31

+0

@TonyGildea:多少钱你了解不同种类的变量吗?听起来你应该阅读一本书或教程中的实例变量与本地变量。 – 2012-07-16 18:08:17

0

好吧,最简单的方法是声明一个字段。

protected PictureBox[] Slots 

您的Form类中(在任何方法以外。