文本文件进入选中的列表框 [英] Text file into checked listbox

查看:71
本文介绍了文本文件进入选中的列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将文本文件中的整行作为项目导入到选中的列表框中.

为了准确起见,每一行都包含用空格分隔的名称,姓氏和数字值.
我想将这些行作为项导入到选中的列表框中,并稍后使用它们的值(阅读它们等.).

有人可以帮助我,也可以给别人一些技巧技巧"的提示?

我的导入尝试很少,但无济于事:

I need to import whole lines from text file into checked listbox as items.

To be accurate every line contains Name, Surname, and number values separated with spaces.
I would like to import those lines into the checked listbox as items and work with their values later (read them etc..).

Can someone help me or maybe give tip on differend "technique" how to do that?

My little try on importing which failed and do nothing:

private void Setup_Load(object sender, EventArgs e)
{
    string path = @"Data/text.txt";
    using (StreamReader stRead = new StreamReader(path))
    {
        while (!stRead.EndOfStream)
        {
            checkedListBox1.Items.Add(Convert.ToString(stRead.ReadLine()));
        }
}



谢谢!



Thanks!

推荐答案

这很简单.看我的解决方案.在这里,我试图读取列出了国家/地区的文本文件,然后将每行添加到CheckedListBox.

您不能使用
This is very easy. Look at my solution. Here I am trying to read a text file with countries listed and then add each line to the CheckedListBox.

You cannot use
Items.Add()//[This is wrong]


正确的方法是


The correct way is ,

Items.Insert(<index>, <string>)</string></index>

.

检查下面的代码.

with proper parameters.

Check my code below.

private void button1_Click(object sender, EventArgs e)
       {

           int counter = 0;
           string line;

           // Read the file and display it line by line.
           System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Users\srikaran\Desktop\Countries.txt");

           //Now read the file line by line
           while ((line = file.ReadLine()) != null)
           {
               //add the line to CheckedListBox, you need to pass the parameters "index" & "string"
               ChkListBox1.Items.Insert(counter, line);

               //increase the index
               counter++;
           }

           //close the file
           file.Close();
       }



默认情况下,不检查项目.

尝试使用此代码,让我知道在获取此输出时是否遇到任何问题.

祝你好运.



By default the items are not checked.

Try this code and let me know if you have any issues getting this output.

Good luck.


这篇关于文本文件进入选中的列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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