简体   繁体   English

在listview C#上加载简单文本文件列表

[英]Loading simples text file list on listview C#

so i was working with a text file data that contains a lot of simple lines and i want to put them on the list view exatly the same way as the listbox do. 所以我正在使用包含许多简单行的文本文件数据,我想将它们放在列表视图中,与列表框一样。 I need that because after i loading a long list on listbox, even it showing all my items, i cannot make a FindString() on it. 我需要这个,因为我在列表框上加载一个长列表,即使它显示我的所有项目,我也无法在它上面创建一个FindString()。 i attached the comand to a combo box, and with other small lists it worked, but with this larger, seems that index reference doesn't work because of listbox limit. 我将命令附加到一个组合框,并与其他小的列表一起工作,但是由于列表框限制,似乎索引引用不起作用。 So i was wondering if is possible to put, as example: 所以我想知道是否可以放,例如:

  • line1 一号线
  • line2 2号线
  • line3 3号线
  • line4 4号线

My text files hasn't this dots, i pu them just to make the example vertical. 我的文本文件没有这个点,我只是为了使示例垂直。 On a list view. 在列表视图上。 i used on listbox the method file.readllines to get it loading to it, and if exists a string find method to help me get the text on the lines. 我在listbox上使用了方法file.readllines来加载它,如果存在一个字符串find方法来帮助我获取行上的文本。 What should i do? 我该怎么办?

You have to add an eventhandler. 您必须添加一个事件处理程序。 For example for the event of the ListView Load. 例如,对于ListView Load的事件。 Then in the event handler you could load the contents of the file using the File class. 然后在事件处理程序中,您可以使用File类加载文件的内容。

For example you could do it like this: 例如,您可以这样做:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        // ListView Loaded - Eventhandler
        private void ListView_Loaded(object sender, RoutedEventArgs e)
        {
           string[] lines = File.ReadAllLines("E:\\test.txt");
           foreach (string line in lines)
           {
               listview.Items.Add(line);
           }
        }
    }
}

I have just tested the solution and it works fine. 我刚刚测试了解决方案,它工作正常。 I hope I got your intention. 我希望我有你的意图。

结果

You can write the searcher you want by yourself. 您可以自己编写所需的搜索者。 It's very easy. 这很容易。 Just iterate on each data that exists in your ListView. 只需迭代ListView中存在的每个数据。 Then check your condition through an if-statement and do anything you want with the result! 然后通过if语句检查您的情况,并根据结果做任何您想做的事情!

Like this : 像这样 :

    this.listView1.Items.Add("Test1");
    this.listView1.Items.Add("Test2");

    int Index = 0;

    foreach (ListViewItem t in this.listView1.Items)
    {
         if (t.Text == "Test1")
            Index = t.SelectedIndex;
            break;
    }

    this.listView1.Items[Index].Selected = true;

I added couple of items to the ListView then iterate on it's items using a foreach, filter the items using an if-statement and finally show the item that I want. 我向ListView添加了几个项目,然后使用foreach迭代它的项目,使用if语句过滤项目,最后显示我想要的项目。

thanks a lot of helping me, but i gandle it, i search a lot for extending the limit of listbox but no one was capable to help me out, but with your "foreach" code, this new way to search helped me to select my string on listbox, here's the code using a combo box to evertime updated get me the selected value from a gourgeous list: 非常感谢帮助我,但是我对它进行了解决,我搜索了很多内容以扩展列表框的限制,但没有人能够帮助我,但是使用你的“foreach”代码,这种新的搜索方式帮助我选择了列表框上的字符串,这里是使用组合框进行evertime更新的代码,从gourgeous列表中获取所选值:

declaring: 声明:

string result;

and then the combobox event: 然后是组合框事件:

private void comboBox1_TextUpdate(object sender, EventArgs e)
    {
        foreach (string item in listBox1.Items)
        {
            listBox1.SelectionMode = SelectionMode.One;

                if (item == comboBox1.Text)
                {
                    result = item;
                }

        }
        listBox1.SelectedItem = result;

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM