Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用户能够使用文本框搜索多个文本?_C#_.net_Winforms - Fatal编程技术网

C# 如何使用户能够使用文本框搜索多个文本?

C# 如何使用户能够使用文本框搜索多个文本?,c#,.net,winforms,C#,.net,Winforms,今天,当我在textBox1中键入任何文本,然后单击开始按钮时,它将在文件中搜索我在textBox1中键入的文本 现在我想添加一些东西,如果用户在textBox1中键入例如:hello,hi,它将在文件中搜索hello和hi。不是一个字符串/文本,而是两个分开的字符串/文本。如果我输入:hello,hi,world now,它将同时在相同的文件中搜索hello hi和world textchanged事件 private void textBox1_TextChanged(object send

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

现在我想添加一些东西,如果用户在textBox1中键入例如:hello,hi,它将在文件中搜索hello和hi。不是一个字符串/文本,而是两个分开的字符串/文本。如果我输入:hello,hi,world now,它将同时在相同的文件中搜索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;
                }
            }
        }
void DirSearch(字符串根目录、字符串文件扩展名、字符串textToSearch、BackgroundWorker worker、DoWorkEventTargets e)
{
List filePathList=新列表();
List restrictedFiles=新列表();
int-overallfiles=0;
int numberoffiles=0;
int numberofdirs=0;
尝试
{  
filePathList=SearchAccessibleFileNodeDistinct(rootDirectory,null).ToList();
}
捕获(异常错误)
{
字符串ad=err.ToString();
}
foreach(文件路径列表中的字符串文件)
{
尝试
{
_忙。等一等();
if(worker.CancellationPending==true)
{
e、 取消=真;
返回;
}
List prog=新列表();
int var=File.ReadAllText(文件).Contains(textToSearch)?1:0;
总体文件++;
如果(var==1)
{
numberoffiles++;
prog.Add(new MyProgress{Report1=file,Report2=numberoffiles.ToString()});
backgroundWorker1.ReportProgress(0,prog);
}
numberofdirs++;
label1.Invoke((MethodInvoker)委托
{
label1.Text=numberofdirs.ToString();
标签1.可见=真;
});
}
捕获(例外)
{
restrictedFiles.Add(文件);
继续;
}
}
}
在DirSearch中,变量textToSearch包含我在textBox1中键入的文本。 如果我只输入textBox1 HI,它将在每个文件中搜索现有的HI

但如果我打“嗨,你好,世界” 现在,我希望它搜索HI HELLO WORLD的每个文件中的现有内容,而不是作为一个文本字符串,而是它自己的现有内容中的每个单词


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

您可以根据空格、逗号或任何其他分隔符在文本框中分割输入,然后将其作为单独的输入传递给您的搜索方法,希望这对您有所帮助,您不应该调用变量
var
var
在C中有特定的含义#