搜索并移动

时间:2016-07-11 13:09:38

标签: c# winforms

我设置了搜索功能来搜索我的富文本框。它将通过文本框并突出显示匹配的所有不同案例。富文本框列出了一个名称然后转到下一行,所以我想要做的不仅是将突出显示的内容导出,而是将整个名称导出到另一个文本框。这是我到目前为止的搜索功能。

      private void Search_Button_Click(object sender, EventArgs e)
       {
        int index = 0;
        int count = 0;
        string temp = Display_Rich_Text_Box.Text;
        //bool k;
        Display_Rich_Text_Box.Text = "";
        Display_Rich_Text_Box.Text = temp;
        string[] fullName;

        while (index <= Display_Rich_Text_Box.Text.LastIndexOf(Search_Text_Box.Text))
        {
            //Searches and locates the text you are searching for
            Display_Rich_Text_Box.Find(Search_Text_Box.Text, index, Display_Rich_Text_Box.TextLength, RichTextBoxFinds.None);

            //Color Selection: Hightlights in yellow
            Display_Rich_Text_Box.SelectionBackColor = Color.Yellow;
            count = count + 1;

             fullName =  Display_Rich_Text_Box.split("\n")

            //Will search through rest of document or until it cannot continue
            index = Display_Rich_Text_Box.Text.IndexOf(Search_Text_Box.Text, index) + 1;
        }

        // if (count > 0)
        //{
        Form2 f = new Form2(count.ToString(), fullName.tostring());
        f.ShowDialog();
        }

(文本框位于不同的表单上,此表单还显示找到的匹配项数(计数))。

因此,当文本框在突出显示文本后突破显示新行时,我尝试将其拆分。

例如:搜索fold(在Search_Text_Box中输入)

文件夹A. 文件夹B. 新设计 图片

所以当它搜索它时会突出显示在Search_Text_Box中输入的内容。我想要做的是拆分负责Display_Text_Box的字符串,这样如果用户输入fold,它将在单独的文本框中显示文件夹A和文件夹B.文本框的格式不同。感谢您提前提供任何帮助。

1 个答案:

答案 0 :(得分:1)

我已将您的功能更改为拆分行:

    private void Search_Button_Click(object sender, EventArgs e)
    {
        var lines = Display_Rich_Text_Box.Text.Split('\n');
        var count = 0;

        // To get the text from the whole line (Which is the whole name you're looking for)
        foreach (string line in lines)
        {
            // If the line doesn't have the text you're looking for
            if (!line.Contains(Search_Text_Box.Text)) continue;

            count++;

            // Add the index of the whole input plus the index of the text within the line
            var index = lines.IndexOf(line) + line.IndexOf(Search_Text_Box.Text);

            //Searches and locates the text you are searching for
            Display_Rich_Text_Box.Find(Search_Text_Box.Text, index,
                Display_Rich_Text_Box.TextLength, RichTextBoxFinds.None);

            //Color Selection: Hightlights in yellow
            Display_Rich_Text_Box.SelectionBackColor = Color.Yellow;

            //DO SOMETHING WITH LINE
            var wholeName = line;
        }

        Form2 f = new Form2(searchCount);
        f.ShowDialog();
    }