我试图创建程序的一部分,其中用户按下按钮并将图像放置在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;
}
}
}
}
编辑: 我不似乎能够发表评论,出于某种原因,所以我只是在这里问。我将如何将数组声明为实例变量而不是本地?实例变量是否有另一个我可能知道的名字?
欢呼
您可以参考[此(作用域在MSDN(C#))](http://msdn.microsoft.com/en-us/ library/aa691132(v = vs.71).aspx) – 2012-07-16 16:02:14