我的项目中有2个表单(Form3和Form5)。在Form3中,程序会生成5个随机数,并将它们放在列表框中。几秒钟后,Form5打开,并询问用户列表框中特定数字的计数。在Form5中有一个检查按钮和一个textBox。所以我在Form5的检查按钮中写了检查部分,以检查列表框中有多少个特定数字。问题是我无法从form5访问form3中的列表框。我还将listbox修饰符设置为public。但是它没有用!这是我的代码:
public partial class Form3 : Form
{
private int ticks=0;
public Form3()
{
InitializeComponent();
}
int i;
int n;
Random r = new Random();
private void Form3_Load(object sender, EventArgs e)
{
for (i = 0; i < 5; i++)
{
n = r.Next(0, 5);
listBox1.Items.Add(n);
}
timer1.Start();
}
---------------------------------------------------------------------
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
}
public int number;
int x;
int k = 0;
public int count=0;
Form3 frm3 = new Form3();
Random r = new Random();
private void Form5_Load(object sender, EventArgs e)
{
number = r.Next(0, 5);
txtRandom.Text = String.Format("Numbers of {0}", number);
}
private void btn_chk_Click(object sender, EventArgs e)
{
for (k = 0; k < frm3.listBox1.Items.Count; k++)
{
if (frm3.listBox1.Items[k].Equals(number))
{
count++;
}
}
int countOfnumber = Convert.ToInt32(txtCount0.Text);
if (countOfnumber == count)
{
MessageBox.Show("well done !");
}
else
{
MessageBox.Show("Oops ! wrong !");
}
}
任何人都可以帮忙吗?