Form1中的代码是:
public int[] recordNumber()
{
int[] a=new int [9];
for(int i=0;i<9;i++)
{
a[i]=0;
}
if(checkBox1.Checked&&comboBox1.SelectedIndex!=-1)
{
a[0]++;
a[1]=comboBox1.SelectedIndex+1;
}
return a;
}
Form2中代码是:
public void check()
{
Form1 abc=new Form1();
int[] a=abc.recordNumber();
listBox1.Items.Clear();
listBox2.Items.Clear();
string[] type=new string[6];
string[] animals= new string[8];
string[] recode = new string[16];
for (int j = 0; j < 16; j++)
{
recode[j] = null;
}
type[0] = null;
type[1]="哺乳类";
type[2]="鸟类";
type[3]="肉食类";
type[4]="有碲类";
type[5]="碲类";
animals[0]=null;
animals[1]="金钱豹";
animals[2]="老虎";
animals[3]="长颈鹿";
animals[4]="斑马";
animals[5]="鸵鸟";
animals[6]="企鹅";
animals[7]="信天翁";
if (a[0] == 0)
{
listBox1.Items.Add("请先输入你想知道的动物的几个特征!!");
}
......
但是数组就是米有传过去啊
public int[] recordNumber()
{
int[] a=new int [9];
for(int i=0;i<9;i++)
{
a[i]=0;
}
if(checkBox1.Checked&&comboBox1.SelectedIndex!=-1)
{
a[0]++;
a[1]=comboBox1.SelectedIndex+1;
}
return a;
}
Form2中代码是:
public void check()
{
Form1 abc=new Form1();
int[] a=abc.recordNumber();
listBox1.Items.Clear();
listBox2.Items.Clear();
string[] type=new string[6];
string[] animals= new string[8];
string[] recode = new string[16];
for (int j = 0; j < 16; j++)
{
recode[j] = null;
}
type[0] = null;
type[1]="哺乳类";
type[2]="鸟类";
type[3]="肉食类";
type[4]="有碲类";
type[5]="碲类";
animals[0]=null;
animals[1]="金钱豹";
animals[2]="老虎";
animals[3]="长颈鹿";
animals[4]="斑马";
animals[5]="鸵鸟";
animals[6]="企鹅";
animals[7]="信天翁";
if (a[0] == 0)
{
listBox1.Items.Add("请先输入你想知道的动物的几个特征!!");
}
......
但是数组就是米有传过去啊
if(checkBox1.Checked&&comboBox1.SelectedIndex!=-1)
{
a[0]++;
a[1]=comboBox1.SelectedIndex+1;
}
没执行
{
a[0]++;
a[1]=comboBox1.SelectedIndex+1;
}
我估计可能是这里有问题吧因为你都没有选啊这个语句快应该是不会执行的啊
方法
之后再new 和 show Form2并把form1 的数组通过重载一个form2的构造方法来传递到form2中
这是我的思路可能比较笨的一个方法但应该是有效的吧.
int[] a=abc.recordNumber();这里执行的结果, a里面的肯定全部是0你应该把Form1作为一个parameter放到Form2的Constructor里面, 把它作为一个全局变量Form2:private Form1 abc;public Form2(Form1 frm)
{
//some other code
this.abc = frm;
}public void check()
{
//Form1 abc=new Form1();
//当Form1 abc里面的CheckBox或ComboBox有选中时, a得到的就不会全部是0
int[] a=abc.recordNumber();
listBox1.Items.Clear();
listBox2.Items.Clear();
string[] type=new string[6];
string[] animals= new string[8];
string[] recode = new string[16];
for (int j = 0; j < 16; j++)
{
recode[j] = null;
}
type[0] = null;
type[1]="哺乳类";
type[2]="鸟类";
type[3]="肉食类";
type[4]="有碲类";
type[5]="碲类";
animals[0]=null;
animals[1]="金钱豹";
animals[2]="老虎";
animals[3]="长颈鹿";
animals[4]="斑马";
animals[5]="鸵鸟";
animals[6]="企鹅";
animals[7]="信天翁";
if (a[0] == 0)
{
listBox1.Items.Add("请先输入你想知道的动物的几个特征!!");
}
{
a[0]++;
a[1]=comboBox1.SelectedIndex+1;
}问题出在这里哈~~
我帮你改进后的code, 是想你可以自由的在CheckBox/ComboBox被处理过后再调用Check(), 不然, 按你原来的code, a[]里面永远都只会是0