如何在notepad ++中执行类似find函数的操作? [英] How do I do something like the find function in notepad++?

查看:175
本文介绍了如何在notepad ++中执行类似find函数的操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在notepad ++中添加一个像find all这样的函数,它会打开一个新窗口,显示突出显示的单词的行号和单词本身。我得到了这个代码,但它只显示在1个句子中,它不会像在记事本++中那样迭代。



我尝试了什么:



 private void button3_Click(object sender,EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
form2.textBox11.Text =;
string search = textBox1.Text;
int index = 0;
System.Collections.ArrayList line = new System.Collections.ArrayList();
do
{
index = richTextBox1.Find(search,index + 1,RichTextBoxFinds.MatchCase);
if(index!= -1)
{
line.Add(richTextBox1.GetLineFromCharIndex(index));
}
}
while((index!= -1));
{
System.Collections.IEnumerator myenum = line.GetEnumerator();
if(line.Count< = 0)
{
form2.label11.Text = search +未找到;
}
其他
{
form2.textBox11.SelectedText = search +在行上找到:;
while(myenum.MoveNext())
{
form2.textBox11.SelectedText = myenum.Current +;
}
}
}

}

解决方案

< blockquote>您必须将行号附加到 form2.textBox11 。您实际上是在设置选择,以便它最终只包含最后的结果:

  //   form2.textBox11.SelectedText = search +在行上找到:;  
form2.textBox11.Text = search + ;
while (myenum.MoveNext())
{
// form2.textBox11.SelectedText = myenum.Current +;
form2.textBox11.Text + = myenum.Current + ;
}


对于初学者,请停止使用ArrayList - 它已被V2.0 of .NET中的Generic集合取代。它;对于遗留应用程序来说没问题,但是新项目应该使用像List< T>这样的通用集合。相反。

其次,停止使用枚举器,并使用 foreach 循环。

三,不要构建字符串 - 特别是如果它们可以变大 - 使用appends,而是使用StringBuilder。

 StringBuilder message = new StringBuilder(); 
string search = textBox1.Text;
int index = 0;
List< int> lines = new List< int>();
do
{
index = richTextBox1.Find(search,index + 1,RichTextBoxFinds.MatchCase);
if(index!= -1)
{
lines.Add(richTextBox1.GetLineFromCharIndex(index));
}
}
while((index!= -1));
if(lines.Count< = 0)
{
message.Append(找不到搜索+);
}
其他
{
message.Append(在行上找到了搜索+:);
string separator =;
foreach(行中的行)
{
message.AppendFormat({0} {1},separator,line);
separator =,;
}
}

然后显示结果:

 Form2 form2 = new Form2(); 
form2.textBox11.Text = message.ToString();
form2.Show();


I want to add a function like find all in notepad++ in which it will open a new window that displays the line number of the highlighted word and the word itself. I got this code but it only display in 1 sentence it doesn't iterate like in notepad++.

What I have tried:

private void button3_Click(object sender, EventArgs e)
       {
           Form2 form2 = new Form2();
           form2.Show();
           form2.textBox11.Text = "";
           string search = textBox1.Text;
           int index = 0;
           System.Collections.ArrayList line = new System.Collections.ArrayList();
           do
           {
               index = richTextBox1.Find(search, index + 1, RichTextBoxFinds.MatchCase);
               if (index != -1)
               {
                   line.Add(richTextBox1.GetLineFromCharIndex(index));
               }
           }
           while ((index != -1));
           {
               System.Collections.IEnumerator myenum = line.GetEnumerator();
               if (line.Count<=0)
               {
                   form2.label11.Text = search+ "was not found";
               }
               else
               {
                   form2.textBox11.SelectedText = search + " was found on line(s): ";
                   while (myenum.MoveNext())
                   {
                       form2.textBox11.SelectedText = myenum.Current+ " ";
                   }
               }
           }

       }

解决方案

You must append the line numbers to your form2.textBox11. You are actually setting the selection so that it finally contains only the last result:

//form2.textBox11.SelectedText = search + " was found on line(s): ";
form2.textBox11.Text = search + " was found on line(s): ";
while (myenum.MoveNext())
{
    //form2.textBox11.SelectedText = myenum.Current+ " ";
    form2.textBox11.Text += myenum.Current+ " ";
}


For starters, stop using ArrayList - it was superseded by Generic collections in V2.0 of .NET. It;'s ok for legacy apps, but new projects should use Generic Collections like List<T> instead.
Second, stop faffing with Enumerators, and use a foreach loop.
Third, don't build strings - particularly if they can get big - with appends, use a StringBuilder instead.

StringBuilder message = new StringBuilder();
string search = textBox1.Text;
int index = 0;
List<int> lines = new List<int>();
do
    {
    index = richTextBox1.Find(search, index + 1, RichTextBoxFinds.MatchCase);
    if (index != -1)
        {
        lines.Add(richTextBox1.GetLineFromCharIndex(index));
        }
    }
while ((index != -1));
if (lines.Count <= 0)
    {
    message.Append(search + "was not found");
    }
else
    {
    message.Append(search + " was found on line(s): ");
    string separator = "";
    foreach (int line in lines)
        {
        message.AppendFormat("{0}{1}", separator, line);
        separator = ", ";
        }
    }

Then display the result:

Form2 form2 = new Form2();
form2.textBox11.Text = message.ToString();
form2.Show();


这篇关于如何在notepad ++中执行类似find函数的操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆