添加TextBox.Text使用一个for循环列表 [英] Add TextBox.Text to a list using a for loop

查看:1167
本文介绍了添加TextBox.Text使用一个for循环列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把在文本框,从0-9顺序命名的值,并添加到使用for循环列表。我有语法什么的问题。

I am trying to take the values in the textboxes, named sequentially from 0-9, and add that to a List using a for loop. I am having problems with the syntax or something.

这里是我现在有。

for (int i = 0; i <= amt.Count(); i++)
        {
            amt[i] = int.Parse(amtBox[i].Text);
        }



该错误是amtBox犯规存在于当前上下文中。

The error is that amtBox doesnt exist in the current context.

我的问题是循环在那里我有amtBox [I]。文本中。我已经试过这几种方式和VS总是抛出一个错误。我曾尝试amtBox+我和编译,但随后会导致一个错误,当我尝试用它做什么,并说数据是错误的类型。

My problem is within the loop where i have amtBox[i].Text. I have tried this several ways and VS always throws an error. I have tried "amtBox" + i and that compiles but then causes an error when I try to do something with it and says "data is of wrong type".

我是新的C#和来自PHP所以也许这就是为什么我认为这种做法会工作。 PHP不关心有关的数据类型,C#确实。我已经做在PHP中很多次这个确切的事情,没有任何问题。

I am new to C# and come from PHP so maybe that is why I think this approach will work. PHP doesnt care about data types where C# really does. I have done this exact thing in PHP many times without any issue.

另一个方式做到这一点任何建议都赞赏,因为我可能在这个完全错误的到来。

Any suggestions on another way to do this are appreciated as I am probably coming at this all wrong.

感谢

推荐答案

一个解决办法是声明数组,分配amtBox'es到阵列中的各个指标,然后您可以在阵列上进行迭代。

One solution would be to declare an array and assign amtBox'es to the individual indexes in the array and then you can iterate on that array.

var amtBoxes = new TextBox[] { amtBox0, amtBox1, .... };
for (int i = 0; i <= amt.Count(); i++)
{
    amt[i] = int.Parse(amtBoxes[i].Text);
}

如果你最终需要重复在其他地方的TextBox控件我会考虑数组的对象的实例成员。

If you end up needing to iterate on your TextBox controls in other places I would consider making the array an instance member of your object.

这篇关于添加TextBox.Text使用一个for循环列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆