如何从文件中读取文本并在列表框中显示 [英] How to read text from files and display in listbox

查看:107
本文介绍了如何从文件中读取文本并在列表框中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

        private void button1_Click(object sender, EventArgs e)
        {

            const int size = 7;
            int[] numbers = new int[size];
            int index = 0;
            StreamReader inputFile;
            inputFile = File.OpenText("sales.txt.txt");
            while (index < numbers.Length && !inputFile.EndOfStream)
            {
                numbers[index] = int.Parse(inputFile.ReadLine());
                index++;
            }



            foreach (int value in numbers)
            {
                listBox1.Items.Add(value);


            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            double[] numbers = { 1245.67, 1189.55, 1098.72, 1456.88, 2109.34, 1987.55, 1872.36 };
            int total = 0;
            foreach (int value in numbers)
            {
                total += value;
            }
            MessageBox.Show(total.ToString());
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}

推荐答案

您拥有的代码将可以正常工作-如果不是很好的话.
有几种方法可以解决此问题:
1)系统找不到文件"sales.txt.txt".可能与应用程序不在同一个文件夹中,因此找不到该文件夹​​.将文件移动到特定位置,然后更改File.Open命令以引用绝对路径:
The code you have will work - if not very well.
There are a couple of ways in which this can go wrong:
1) The system cannot find the file "sales.txt.txt". Probably, this is not in the same folder as the application, so it can''t find it. Move the file to a specific location, and change the File.Open command to refer to an absolute path:
inputFile = File.OpenText(@"D:\Temp\sales.txt.txt");

字符串前面的"@"会关闭特殊的字符处理,因此您只需要一个反斜杠字符即可,而不是两个
2)文件中的数据不仅是数字,或者对于int而言太大.检查它-数字必须仅是0-9位数字,并且必须小于int.MaxValue(2,147,483,647)

如果通过调试器运行代码,应该会收到一条消息,向您显示问题所在.


虽然有很多样式"要点-魔术数字,固定数组长度,很幸运,因为int具有默认的ToString moverride,这意味着ListBox将显示值,如果您是第一个使用数组则毫无意义.仅使用它来填充第二个循环,使用VS默认名称,而不使用描述对象作用,缺少注释等的名称,但是作为第二个程序,它还不错.

The ''@'' in front of the string turns off special cahracater processing, so you only need the single backslash character instead of double
2) The data in the file is not just numerics, or is too big for an int. Check it - numbers must be just teh digits 0-9 and must be a number less than int.MaxValue (2,147,483,647)

If you run your code through the debugger, you should get a message to show you where the problem is.


There are quite a few "style" points though - magic numbers, fixed array length, luck in that int has a default ToString moverride that means the ListBox will display the values, the pointlessness of using an array in teh first place, if you are only going to use it to feed the second loop, use of VS default names instead of using names that describe what teh object does, lack of comments, and so on, but as a second ever program, it''s not too bad.


private void button1_Click(object sender, EventArgs e)
{

    const int size = 7;
    double[] numbers = new double[size];
    int index = 0;
    StreamReader inputFile;
    inputFile = File.OpenText("sales.txt.txt");
    while (index < numbers.Length && !inputFile.EndOfStream)
    {
        numbers[index] = double.Parse(inputFile.ReadLine());
        index++;
    }



    foreach (int value in numbers)
    {
        listBox1.Items.Add(value);


    }
}



当然,您可以通过执行以下操作避免整个问题:



Of course your whole problem could have been avoided by doing:

private void button1_Click(object sender, EventArgs e)
{
    StreamReader inputFile;
    inputFile = File.OpenText("sales.txt.txt");
    while (!inputFile.EndOfStream)
    {
        listBox1.Items.Add(inputFile.ReadLine());
    }
}


这篇关于如何从文件中读取文本并在列表框中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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