2012-10-09 172 views
1

我試過這段代碼:如何從richTextBox中刪除/刪除特定文本行?

private void RemoveLines(List<string> Keywords) 
     { 
      string NewText = ""; 
      String regex = string.Empty; 
      this.Invoke(new MethodInvoker(delegate { NewText = richTextBox1.Text; })); 
      Regex MyRegex = null; 
      foreach (string keyword in Keywords) 
      { 
       regex = String.Format(@"^.*\W{0}\W.*$",keyword); 
       MyRegex = new Regex(regex, RegexOptions.Multiline); 
       NewText = MyRegex.Replace(NewText, Environment.NewLine); 
      } 
      //Remove blank lines 
      NewText = Regex.Replace(NewText, @"^\s+$[\r\n]*", "", RegexOptions.Multiline); 
      this.Invoke(new MethodInvoker(delegate { richTextBox1.Text = NewText; })); 
      this.Invoke(new MethodInvoker(delegate { richTextBox1.Refresh(); })); 
     } 

而且removeExtrenals功能:

private List<string> removeExternals(List<string> externals) 
     { 
      if(!LocalyKeyWords.ContainsKey(mainUrl)) 
      { 
       return externals; 
      } 
      List<string> keywords = LocalyKeyWords[mainUrl]; 
      List<int> indices = new List<int>(); 
      foreach(string keyword in keywords) 
      { 
       //Accumulate a list of the indices of the items that match. 
       indices = indices.Concat(externals.Select((v, i) => v.Contains(keyword) ? i : -1)).ToList(); 
      } 
      //Filter out the -1s, grab only the unique indices. 
      indices = indices.Where(i => i >= 0).Distinct().ToList(); 
      //Filter out those items that match the keyword(s) related to mainUrl. 
      externals = externals.Where((v, i) => !indices.Contains(i)).ToList(); 
      return externals; 
     } 

和IM調用這個函數的removelines和removeexternals:

RemoveLines(removeExternals(webSites)); 

在removeExternals使用斷點後我看到兩個網站是不包含谷歌(關鍵字即時通訊使用的例子),所以從18個網址我應該看到在richTextBox○ nly 16 url的 我在removelines函數中也使用了一個斷點,沒有例外,但是我看到了richTextBox中的所有url。

+0

它必須是一個文本框?如果需要編輯,使用listbox或grid會容易得多 – slawekwin

+0

如果您可以通過舉一個簡短的例子來提供更多關於您確切需求的細節,這將有所幫助。 – Luftwaffe

回答

1

嘗試這樣的:
我加了一些隨機數據測試..

private void Form1_Load(object sender, EventArgs e) 
    { 
     richTextBox1.AppendText(@"Loading The Url: http://www.blogger.com/?tab=wj... Failed "); 
     richTextBox1.AppendText(Environment.NewLine); 
     richTextBox1.AppendText(@"Loading The Url: http://www.google.co.il/intl/iw/options/... Done "); 
     richTextBox1.AppendText(Environment.NewLine); 
     richTextBox1.AppendText(@"Loading The Url: http://www.xyz.com/?tab=wj... Failed "); 
     richTextBox1.AppendText(Environment.NewLine); 
     richTextBox1.AppendText(@"Loading The Url: http://www.abc.com/?tab=wj... Done "); 
     richTextBox1.AppendText(Environment.NewLine); 
     richTextBox1.AppendText(@"Loading The Url: http://www.so.com/?tab=wj... Failed "); 
     richTextBox1.AppendText(Environment.NewLine); 
     keys.Add("xyz"); 
     keys.Add("abc"); 
     RemoveLines(keys); 
    } 

和這裏的代碼RemoveLines方法..

private void RemoveLines(List<string> Keywords) 
    { 
     String regex = string.Empty; 
     string NewText = richTextBox1.Text;    
     Regex MyRegex = null; 
     foreach (string keyword in Keywords) 
     { 
      regex = String.Format(@"^.*\W{0}\W.*$",keyword); 
      MyRegex = new Regex(regex, RegexOptions.Multiline); 
      NewText = MyRegex.Replace(NewText, Environment.NewLine); 
     } 
     //Remove blank lines 
     NewText = Regex.Replace(NewText, @"^\s+$[\r\n]*", "", RegexOptions.Multiline); 
     richTextBox1.Text = NewText; 
     richTextBox1.Refresh(); 
    } 

希望它能幫助!

+0

德國空軍我試過你的代碼,因爲某些原因我不能工作,我會更新我的問題。 –

+0

@DanielLip如果您的目的是從包含特定關鍵字的RichTextBox中刪除行(如問題標題所示),那麼上面的代碼肯定會起作用。或者你的要求不同? – Luftwaffe

+0

德國空軍剛剛更新了我的問題與更新。 –