Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从文本框数组中检索输入的文本?_C#_Arrays_Input_Textbox - Fatal编程技术网

C# 如何从文本框数组中检索输入的文本?

C# 如何从文本框数组中检索输入的文本?,c#,arrays,input,textbox,C#,Arrays,Input,Textbox,我正在创建一个计算器,用户在一个文本框中输入一个数字,指定用户想要的输入量(文本框)(代码未显示)。我使用了一个文本框数组来创建这些文本框。当我想从这些文本框中获取文本以执行计算时,问题就出现了,到目前为止我为此编写的代码如下所示: int n; TextBox[] textBoxes; Label[] labels; double[] values; public void GetValue() { n = Convert.ToInt16(txtInputFields.Text);

我正在创建一个计算器,用户在一个文本框中输入一个数字,指定用户想要的输入量(文本框)(代码未显示)。我使用了一个文本框数组来创建这些文本框。当我想从这些文本框中获取文本以执行计算时,问题就出现了,到目前为止我为此编写的代码如下所示:

int n;
TextBox[] textBoxes;
Label[] labels;
double[] values;

public void GetValue()
{
    n = Convert.ToInt16(txtInputFields.Text);
    values = new double[n];
    textBoxes = new TextBox[n];

    for (int i = 0; i < n; i++)
    {

    }
}
但它给了我一个错误:索引超出了数组的边界。

我对C#和编程一般来说都是新手,所以任何帮助都将不胜感激。 谢谢

编辑:创建文本框的代码如下所示:

public void InstantiateTextFields()
    {
        n = Convert.ToInt16(txtInputFields.Text);
        int posLeft = 100;
        textBoxes = new TextBox[n];
        labels = new Label[n];

        // Creates number of inputs and labels as specified in txtInputFields (n).
        for (int i = 0; i < n; i++)
        {
            textBoxes[i] = new TextBox();
            textBoxes[i].Top = 100 + (i * 30);
            textBoxes[i].Left = posLeft;
            textBoxes[i].Name = "txtInput" + (i + 1);


            labels[i] = new Label();
            labels[i].Top = 100 + (i * 30);
            labels[i].Left = posLeft - 50;
            labels[i].Text = "Input " + (i + 1);
            labels[i].Name = "lblInput" + (i + 1);

        }

        for (int i = 0; i < n; i++)
        {
            this.Controls.Add(textBoxes[i]);
            this.Controls.Add(labels[i]);
        }
    }
public void instanceetextfields()
{
n=Convert.ToInt16(txtInputFields.Text);
int-posLeft=100;
textboxs=新的TextBox[n];
标签=新标签[n];
//创建TXInputFields(n)中指定的输入数和标签数。
对于(int i=0;i
values[n]=Convert.toDouble(文本框[n].Text);因为n在数组之外,所以给您错误信息。分配一个大小为n的数组,该数组的索引为零,即最后一个元素位于位置n-1

for (int i = 0; i < n; i++)
{
    values[i] = Convert.toDouble(textBoxes[i].Text);
}
for(int i=0;i
您在
GetValue
方法中的代码会重新创建文本框数组,这样做会破坏原始内容(动态创建的文本框
实例化文本字段
)。 这样,您的循环在未设置
对象引用的情况下失败

您只需要使用全局变量,而无需重新初始化它

public void GetValue()
{
    n = Convert.ToInt16(txtInputFields.Text);
    values = new double[n];

    // textBoxes = new TextBox[n];

    for (int i = 0; i < n; i++)
    {
       values[i] = Convert.ToDouble(textBoxes[i].Text);

    }
}
public void GetValue()
{
n=Convert.ToInt16(txtInputFields.Text);
值=新的双精度[n];
//textboxs=新的TextBox[n];
对于(int i=0;i
关于读取输入文本并将其转换为不带检查的双精度文本,有一些值得一提的地方。如果用户键入的内容无法转换为双精度,则代码将在Convert.ToDouble行崩溃。改用

double temp;
for (int i = 0; i < n; i++)
{
   if(double.TryParse(textBoxes[i].Text, out temp)
       values[i] = temp;
   else
   {
       // Not a double value....
       // A message to your user ?
       // fill the array with 0 ?
       // Your choice....
   }
}
双温;
对于(int i=0;i
这是WinForm还是Web应用程序?显示用于创建文本框的代码。在哪里用实际用于计算的文本框填充文本框数组?上面的代码创建了数组,但它没有任何文本框来读取文本。这给了我一个错误:对象引用未设置为对象的实例。..Thanks.textboxs=new TextBox[n];分配一个TextBox数组,但是您还必须将每个引用分配给一个TextBox实例。谢谢您的建议,我会尝试一下。
double temp;
for (int i = 0; i < n; i++)
{
   if(double.TryParse(textBoxes[i].Text, out temp)
       values[i] = temp;
   else
   {
       // Not a double value....
       // A message to your user ?
       // fill the array with 0 ?
       // Your choice....
   }
}