【问题标题】:c# with visual studio windows form | How to search a textbox input into a file and return the searchc#与visual studio windows窗体|如何搜索文本框输入到文件中并返回搜索
【发布时间】:2020-04-08 18:13:19
【问题描述】:

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

    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");

        }

【问题讨论】:

  • 也许您需要从该文本文件中粘贴一些行。在这种情况下,那个锯齿状数组games 的业务是什么? ReadIntoArray(); 究竟做了什么?

标签: c# textbox sortedlist


【解决方案1】:

请尝试以下方法(我写了一些cmets来帮助你理解我的方法):

    // 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++;
        }
    }

截图:

希望对你有帮助!

【讨论】:

  • 感谢您的回答!我没有使用您的 BrowseFile 方法,因为我已经加载了我的文件。所以我使用了你的 DoSearch_click 方法。但仍然没有返回值。
  • 当你加载文件时,你如何存储它的内容?你能分享文件加载的代码让我看看吗?
【解决方案2】:

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

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";
}

【讨论】:

    猜你喜欢
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 2016-03-04
    • 1970-01-01
    • 2018-01-12
    • 2010-12-03
    相关资源
    最近更新 更多