【问题标题】:How can I make that the user will be able to search for a multiple texts with textBox?如何使用户能够使用 textBox 搜索多个文本?
【发布时间】:2016-07-02 13:16:51
【问题描述】:

今天,当我在 textBox1 中键入任何文本然后单击开始按钮时,它会在文件中搜索我在 textBox1 中键入的文本。

现在我想以某种方式添加一些内容,例如,如果用户在 textBox1 中键入:hello,hi,它将在文件中搜索 hello 和 hi。不是一个字符串/文本,而是两个分开的。如果我现在输入:hello,hi,world,它会同时在相同的文件中搜索 hello hi 和 world。

textchanged 事件

private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text != "" && textBox3.Text != "" && Directory.Exists(textBox3.Text))
            {
                startButton.Enabled = true;
                Properties.Settings.Default["Setting2"] = textBox1.Text;
                Properties.Settings.Default.Save();
            }
            else
            {
                startButton.Enabled = false;
            }
        } 

开始按钮点击事件

private void startButton_Click(object sender, EventArgs e)
        {
            label21.Visible = true;
            startButton.Enabled = false;
            stopButton.Enabled = true;
            pauseresumeButton.Enabled = true;
            timer1.Start();
            if (!backgroundWorker1.IsBusy)
            {
                SetWorkerMode(true);
                backgroundWorker1.RunWorkerAsync();
            }
        }

办事活动

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            _stopwatch.Restart();
            DirSearch(textBox3.Text, textBox2.Text, textBox1.Text, worker, e);
            _stopwatch.Stop();
        }

我在文件中搜索文本的 DirSearch 方法。

void DirSearch(string rootDirectory, string filesExtension, string textToSearch, BackgroundWorker worker, DoWorkEventArgs e)
        {
            List<string> filePathList = new List<string>();
            List<string> restrictedFiles = new List<string>();
            int overallfiles = 0;
            int numberoffiles = 0;
            int numberofdirs = 0;

            try
            {  
                filePathList = SearchAccessibleFilesNoDistinct(rootDirectory, null).ToList();
            }
            catch (Exception err)
            {
                string ad = err.ToString();
            }
            foreach (string file in filePathList)
            {
                try
                {
                    _busy.WaitOne();
                    if (worker.CancellationPending == true)
                    {
                        e.Cancel = true;
                        return;
                    }
                    List<MyProgress> prog = new List<MyProgress>();
                    int var = File.ReadAllText(file).Contains(textToSearch) ? 1 : 0;
                    overallfiles++;
                    if (var == 1)
                    {
                        numberoffiles++;
                        prog.Add(new MyProgress { Report1 = file, Report2 = numberoffiles.ToString() });
                        backgroundWorker1.ReportProgress(0, prog);
                    }
                    numberofdirs++;
                    label1.Invoke((MethodInvoker)delegate
                    {
                        label1.Text = numberofdirs.ToString();
                        label1.Visible = true;
                    });
                }
                catch (Exception)
                {
                    restrictedFiles.Add(file);
                    continue;
                }
            }
        }

在 DirSearch 中,变量 textToSearch 包含我在 textBox1 中键入的文本。 如果我只在 textBox1 中输入 HI,就像现在一样,它将在每个文件中搜索现有的 HI。

但是如果我输入 HI,HELLO,WORLD 现在我希望它在 HI HELLO WORLD 的每个文件中搜索存在的不是一个文本字符串,而是它自己存在的每个单词。

如果我输入 Hi HELLO WORLD,那么它会将其作为一个字符串/文本进行搜索,但一旦用户输入,它应该搜索每个单词/文本。

【问题讨论】:

  • 只是一点评论,你不应该把你的变量称为varvar在C#中有特定的含义

标签: c# .net winforms


【解决方案1】:

您可以根据空格、逗号或任何其他分隔符拆分文本框中的输入,然后将这些作为单独的输入传递给您的搜索方法,希望这会有所帮助

【讨论】:

    猜你喜欢
    • 2018-11-19
    • 2018-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多