在C#中从文本框获取输入到数组 [英] Get input from a textbox to an array in C#

查看:173
本文介绍了在C#中从文本框获取输入到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计我想要的就是这个。从文本框输入输入并按OK按钮,输入存储在数组[0]元素中。然后用户输入另一个数字并按OK按钮并输入保存在数组[1]我怎么能实现这个呢?到目前为止,我可以从文本框中输入但是存储是一个问题。

示例=如果我的第一个输入是12而第二个输入是45,当我看到阵列12和1245 ..如何分离它?请提出任何想法:D提前致谢:)



Guys simply what I want is this.Get a input from a textbox and press OK button and the input stores in the array[0] element.And then user input another number and press OK button and input saves in array[1] and so on.How can I implement this?So far from this code I can input from text box but storing is a problem.
Example= If my first input is 12 and second input is 45,when i see the array its 12 and 1245..how to seperate it? Any ideas please :D Thanks in advance :)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Matrix
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private const int MAX_ITEMS = 10;
        private int CurrentIndex = 0;
        private double[] numArray = new double[MAX_ITEMS];
        private int index = 0;


        private void button1_Click(object sender, EventArgs e)
        {
            {
               if (this.index < 10)
                {
                    numArray[this.index] = int.Parse(textBox1.Text);
                    this.index++;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            for(int i=0;i<10;i++){

            listBox1.Items.Add(numArray[i]); //show array in a listbox

        }
    }
    }
}

推荐答案

这是您的代码,其中包含一些批评,其中包含上述其他人的一些评论。请注意,我已经注释掉了一行代码,并将其替换为替代建议,我通常只会编辑或删除原始版本

Here is your code with a critique which includes some of the comments made by others above. Note that where I''ve commented out a line of code and replaced it with an alternative suggestion I would normally just edit or delete the original
private const int MAX_ITEMS = 10;          // This is a good idea but you don't use it everywhere you could
    //private int CurrentIndex = 0;           // This is never used so remove it - stay tidy!
    private double[] numArray = new double[MAX_ITEMS];  // You've declared this array as a double but only ever use ints in it
    private int index = 0;

    private void button1_Click(object sender, EventArgs e)
    {
        //{ These extra braces aren't necessary and could cause confusion
            //if (this.index <10) - Avoid "magic" numbers - make it obvious what you are checking e.g.
            if (this.index < numArray.GetUpperBound(0)) // or you could have reused MAX_ITEMS here
            {
                // You should check that the text box actually contains a number rather than trusting the user
                // From your question you appear to enter the numbers one at a time so I haven't put anything
                // here about splitting strings as per SA's comment but you may need to consider that
                double dnum;
                if (double.TryParse(textBox1.Text, out dnum))
                {
                    numArray[this.index++] = dnum;  // Note that the index is incremented immediately after it is used
                    textBox1.Text = "";   // See comments from PhantomUpvoter - this fixes the problem listed in your question
                }
            }
        //} See comment on extra braces above
    }

    private void button2_Click(object sender, EventArgs e)
    {
        //for (int i = 0; i < 10; i++)
        for(int i = 0; i <= numArray.GetUpperBound(0); i++) // As before - no "magic" numbers or reuse MAX_ITEMS
        {
            // You should really only display stuff that has been entered
            if(i < index)
                listBox1.Items.Add(numArray[i]); //show array in a listbox
            // This bit would be easier if you use List<> or one of the other collections
            // Then you could get rid of the high level variable int index
        }
    }
}



在学习C#的网站方面,谢尔盖提出了一个很好的建议为了便于链接,我只是在这里重复 http://msdn.microsoft.com/en-us/ vstudio / hh341490.aspx [ ^ ]

还有很多其他人,但要注意他们的质量可能不同......有些网站在谷歌搜索中首先出现并不一定是最好的。

祝你好运并继续努力!


In terms of sites to learn C# Sergey gave an excellent suggestion which I''m only repeating here for ease of linking http://msdn.microsoft.com/en-us/vstudio/hh341490.aspx[^]
There are many others but be warned they can be of varying quality... there are sites that come up first in Google searches that aren''t necessarily the best.
Good luck and keep trying!


这篇关于在C#中从文本框获取输入到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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