c#使用visual studio windows窗体|如何搜索输入到文件中的文本框并返回搜索结果

c#使用visual studio windows窗体|如何搜索输入到文件中的文本框并返回搜索结果,c#,textbox,sortedlist,C#,Textbox,Sortedlist,我是C#的初学者,我正在写一个项目,在这个项目中我创建了一个读取txt文件的方法。 我有一个带搜索按钮的文本框。程序必须做的是读取文本框中的输入,搜索file方法并在列表框中显示匹配结果。 我已经有了一些这样的编码,但它什么也不返回。有人能帮我吗 private void searchButton_Click(object sender, EventArgs e) { String[] findValues = this.nameTextBox.Text.Spli

我是C#的初学者,我正在写一个项目,在这个项目中我创建了一个读取txt文件的方法。 我有一个带搜索按钮的文本框。程序必须做的是读取文本框中的输入,搜索file方法并在列表框中显示匹配结果。 我已经有了一些这样的编码,但它什么也不返回。有人能帮我吗

    private void searchButton_Click(object sender, EventArgs e)
    {
        String[] findValues = this.nameTextBox.Text.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
        string newline = string.Empty;
        gameListBox.Items.Clear();
        ReadIntoArray();
        string[][] games = new string[16][];

        var index = BinSrchByName(nameTextBox.Text);

        if (index != -1)
        {
            gameListBox.Items.Add(names[index] + " ==> $" + sales[index]);
        }
        else
        {
            MessageBox.Show("Data not found");

        }

此函数接收文件路径和搜索的单词,并遍历整个文本文件,返回找到请求单词的行

private string SearchText(string archivetxt, string word) {
    StreamReader sr = new StreamReader(archivetxt);
    while (!sr.EndOfStream) {
        string s = sr.ReadLine();
        if (s.IndexOf(word) > -1)
            return s;                
    }
    sr.Close();
    return word + " not found";
}
请尝试以下内容(我写了一些评论以帮助您理解我的方法):

//声明一个列表以保存文件行
列表文件行=新列表();
私有无效按钮\u浏览文件\u单击(对象发送者,事件参数e)
{
//打开一个文件对话框
使用(OpenFileDialog openDialog=新建OpenFileDialog())
{
//将文件对话框设置为仅显示*.txt文件或所有文件
openDialog.Filter=“文本文件(*.txt)|*.txt |所有文件(*.*)|*.”;
//仅允许单个文件选择
openDialog.Multiselect=false;
//确保用户没有单击“取消”按钮
if(openDialog.ShowDialog(this)=DialogResult.OK)
{
//仅使用文件名(而不是完整路径)更新当前文件标签
label_CurrentFile.Text=$“当前文件:{Path.GetFileName(openDialog.FileName)}”;
//将txt文件的每一行添加到列表中
foreach(File.ReadAllLines(openDialog.FileName,Encoding.UTF8)中的字符串行)
文件行。添加(行);
}
}
}
私有无效按钮\u DoSearch\u Click(对象发送者,事件参数e)
{
//清除列表
list_SearchResults.Items.Clear();
//计算行数,以便稍后在结果列表中显示
整数=1;
//对于“文件行”列表中的每个项目
foreach(文件行中的变量项)
{
//检查当前行是否包含用户在搜索框中键入的术语
//我用“ToLower()”来忽略这个案例
if(item.ToLower()包含(text\u SearchTerm.text.ToLower())
{
//创建新的ListViewItem,以便稍后添加到结果列表中
//将第一列添加到搜索框中包含术语的完整行中
ListViewItem lvi=新ListViewItem(项目);
//将行号添加到第二列
添加(ilineumber.ToString());
//将ListviewItem添加到结果列表中
列出搜索结果.Items.Add(lvi);
}
//增加行号变量
iLinember++;
}
}
截图:


希望有帮助

谢谢你的回答!我没有使用你的BrowseFile方法,因为我已经加载了我的文件。所以我用了你的DoSearch_click方法。但仍然不返回值。加载文件时,如何存储其内容?你能分享文件加载的代码让我看看吗?也许你需要从文本文件中粘贴一些行。在这种情况下,锯齿状数组
游戏有什么用?还有什么
ReadIntoArray()确实如此吗?
    // Declare a list to hold the file lines
    List<string> FileLines = new List<string>();

    private void button_BrowseFile_Click(object sender, EventArgs e)
    {
        // Open a file dialog
        using (OpenFileDialog openDialog = new OpenFileDialog())
        {
            // Set the file dialog to show only *.txt file or all files
            openDialog.Filter = "Text files (*.txt)|*.txt|All Files (*.*)|*.*";

            // Allow only single file selection
            openDialog.Multiselect = false;

            // Make sure the user didn't clicked the 'Cancel' button
            if (openDialog.ShowDialog(this) == DialogResult.OK)
            {
                // Update the current file label with the filename only (not the full path)
                label_CurrentFile.Text = $"Current file: {Path.GetFileName(openDialog.FileName)}";

                // Add each line of the txt file into the list
                foreach (string line in File.ReadAllLines(openDialog.FileName, Encoding.UTF8))
                    FileLines.Add(line);
            }
        }
    }

    private void button_DoSearch_Click(object sender, EventArgs e)
    {
        // Clear the list
        list_SearchResults.Items.Clear();

        // Count the number of line so you will be able to present it on the results list later on
        int iLineNumber = 1;

        // For each item in the 'FileLines' list
        foreach (var item in FileLines)
        {
            // Check whether the current line contains the term the user typed in the searchbox
            // I'm using 'ToLower()' to ignore case
            if (item.ToLower().Contains(text_SearchTerm.Text.ToLower()))
            {
                // Create new ListViewItem to be added later on to the results list
                // Add the first column the complete line that contains the term in the searchbox
                ListViewItem lvi = new ListViewItem(item);

                // Add the line number to the second column
                lvi.SubItems.Add(iLineNumber.ToString());

                // Add the ListviewItem to the results list
                list_SearchResults.Items.Add(lvi);
            }

            // Increment the line number variable
            iLineNumber++;
        }
    }