【发布时间】:2014-06-04 03:03:37
【问题描述】:
这与一个文本框数组和一个 int 数组有关。不知何故,数组的值似乎在 main 方法中起作用,但在其他方法中不起作用。
所以从第 34 行到第 44 行,我在 main 中为我的数组赋值,然后从第 48 行到第 56 行,我使用 for 循环来实现它,因此在输入文本框时只允许某些键。然后在第 64 到 70 行的另一种方法中,我尝试使用另一个 for 循环使“”字符串值等于 0,或者以其他方式获取文本框中的值并将该值设置为 numbers[] 数组中的变量。这似乎不起作用。更改文本框中的值似乎不会更改 numbers[] 数组的值。但是,如果我输入它确实有效,就像我从第 71 行到第 94 行所做的那样。我不想采取简单的方法直接输入,我想知道为什么这个循环不起作用所以我可以了解有关数组和方法的更多信息,因此我知道将来如何解决类似问题。
为方便起见:
public Form1()
{
InitializeComponent();
inputs[0] = textBox1;
inputs[1] = textBox2;
inputs[2] = textBox3;
inputs[3] = textBox4;
inputs[4] = textBox5;
numbers[0] = oneYear;
numbers[1] = twoYear;
numbers[2] = threeYear;
numbers[3] = fourYear;
numbers[4] = moreYear;
textBox6.ReadOnly = true;
for (int i = 0; i <= 4; i++)
{
inputs[i].KeyDown += (obj, args) =>
{
Keys[] allowedKeys = { Keys.Back, Keys.Next, Keys.Delete, Keys.Left, Keys.Right };
args.SuppressKeyPress = !allowedKeys.Contains(args.KeyCode) && (args.KeyValue < 48 || args.KeyValue > 57);
};
inputs[i].Text = 0.ToString();
}
}
private void autobots()
{
try
{
//find out why this doesn't work
for (int i = 0; i <= 4; i++)
{
if (inputs[i].Text == "")
numbers[i] = 0;
else
numbers[i] = Convert.ToInt32(inputs[i].Text);
}
/*if (textBox1.Text == "")
oneYear = 0;
else
oneYear = Convert.ToInt32(textBox1.Text);
if (textBox2.Text == "")
twoYear = 0;
else
twoYear = Convert.ToInt32(textBox2.Text);
if (textBox3.Text == "")
threeYear = 0;
else
threeYear = Convert.ToInt32(textBox3.Text);
if (textBox4.Text == "")
fourYear = 0;
else
fourYear = Convert.ToInt32(textBox4.Text);
if (textBox5.Text == "")
moreYear = 0;
else
moreYear = Convert.ToInt32(textBox5.Text);*/
oneTotal = oneYear * 24;
twoTotal = twoYear * 27;
threeTotal = threeYear * 30;
fourTotal = fourYear * 33;
moreTotal = moreYear * 36;
total = oneTotal + twoTotal + threeTotal + fourTotal + moreTotal;
textBox6.Text = total.ToString();
//label6.Text = ("$") + total.ToString();
}
catch
{
textBox6.Text = "";
//label6.Text = "";
}
}
【问题讨论】:
-
您如何检查数字数组是否已更改?如果您正在查看 oneYear 等变量:这些变量将保存自己的值,并且确实指向与 numbers 数组中的值相同的值存储。
-
我们没有您的行号,这使这成为一个大难题。尝试在小型(10-20 行)控制台应用中复制问题。
-
@HenkHolterman 您对行号也有初步反应,但它们包含在顶部链接的粘贴箱中。除此之外,关于令人费解的部分,我偷偷怀疑这是对结构存储方式的误解。
标签: c# arrays loops for-loop methods