下面是我开始调试时导致错误的代码。当我从组合框 1 或组合框 2 中选择选项时,我收到一条弹出消息:索引超出了数组的范围。我怎么能解决这个问题?
感谢您花时间阅读。:)
public Form1()
{
InitializeComponent();
String[] arr1 = new String[2];
arr1[0] = "SG";
arr1[1] = "MY";
comboBox1.Items.AddRange(arr1);
comboBox2.Items.AddRange(arr1);
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
double[,] arr = new double[2, 2];
for(int i = 0; i <2; i++)
{
arr[0, 0] = 1;
arr[0, 1] = 1.24;
arr[1, 0] = 0.80;
arr[1, 1] = 1;
label1.Text =
arr[comboBox1.SelectedIndex,
comboBox2.SelectedIndex].ToString();//Index was outside the bounds of the array.
}
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
double[,] arr = new double[2, 2];
for(int i = 0; i < 2; i++)
{
arr[0, 0] = 1;
arr[0, 1] = 1.24;
arr[1, 0] = 0.80;
arr[1, 1] = 1;
label1.Text =
arr[comboBox1.SelectedIndex,
comboBox2.SelectedIndex].ToString();//Index was outside the bounds of the array.
}
}