2014-06-28 59 views
0

我的問題是類同這一https://stackoverflow.com/questions/18944437/replacing-text-between-b-and-b-with-bold-text-in-c-sharp如何在文本加粗串

我想< * STRONG>標籤

之間的粗弦我得到的字符串與

string pattern = @"<strong>(.*?)</strong>"; 
var matches = Regex.Matches(paragraf2.Range.Text, pattern) 
        .Cast<Match>() 
        .Select(m => m.Groups[1].Value) 
        .ToList(); 

我猜我需要像

foreach(string a in matches) 
{  
} 

但我無法fi指出裏面寫什麼。

+0

你嘗試了什麼?您的標記似乎表明您將生成的文本放入Word文檔中。是對的嗎? – KevinS

+0

@KevinS是的。我想要的是找到屬於的文本(匹配給出了字符串列表),並替換爲單詞文檔中的粗體文本。 – ruqo

+0

http://stackoverflow.com/questions/9840199/c-sharp-microsoft-office-interop-word – har07

回答

0

如har07在註釋中規定,你可以使用它像

 string pattern = @"<strong>(.*?)</strong>"; 
     var matches = Regex.Matches(paragraf2.Range.Text, pattern) 
       .Cast<Match>() 
       .Select(m => m.Groups[1].Value) 
       .ToList(); 
     Word.Find findObject = Application.Selection.Find; 
     foreach(string a in matches){ 
      findObject.ClearFormatting(); 
      findObject.Text = a; 
      findObject.Replacement.ClearFormatting(); 
      findObject.Replacement.Font.Bold = 1; 
      object replaceAll = Word.WdReplace.wdReplaceAll; 
      findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceAll, ref missing, ref missing, ref missing, ref missing); 
      } 
1

相關的你的情況(如果正確描述)時,可以通過修改字符串中使用在[https://stackoverflow.com/questions/18944437/replacing-text-between-b-and-b-with-bold-text-in-c-sharp]指定的溶液:

richTextBox1.SelectedText = richTextBox1.SelectedText.Replace("<b>", "").Replace("</b>", ""); 

如下所示:

richTextBox1.SelectedText = richTextBox1.SelectedText.Replace("<strong>", String.Empty).Replace("</strong>", String.Empty); 

或者,相應的解決方案(修改後的原件)如下:

private void richTextBox1_TextChanged(object sender, EventArgs e) 
{ 
    string regexString = "(?<=<strong>)(.*?)(?=</strong>)"; 
    Match matches = (Regex.Match(richTextBox1.Text, regexString)); 

    if (matches.Success) 
    { 
     int index1 = richTextBox1.Find("<strong>"); 
     int index2 = richTextBox1.Find("</strong>"); 
     richTextBox1.Select(index1 + 3, ((index2) - (index1 + 3))); 

     richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont.FontFamily, richTextBox1.SelectionFont.Size, FontStyle.Bold); 
    } 
} 

有關參考下面的代碼片段RichTextBox中的字體屬性的詳細信息(來自微軟在線文檔http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectionfont%28v=vs.100%29.aspx):

private void ToggleBold() 
{ 
    if (richTextBox1.SelectionFont != null) 
    { 
     System.Drawing.Font currentFont = richTextBox1.SelectionFont; 
     System.Drawing.FontStyle newFontStyle; 

     if (richTextBox1.SelectionFont.Bold == true) 
     { 
     newFontStyle = FontStyle.Regular; 
     } 
     else 
     { 
     newFontStyle = FontStyle.Bold; 
     } 

     richTextBox1.SelectionFont = new Font(
     currentFont.FontFamily, 
     currentFont.Size, 
     newFontStyle 
    ); 
    } 
} 

RGDS,

+0

我不想只是全部刪除它們,我需要它們以粗體顯示。 – ruqo

+0

可悲的是它不工作沒有range.SelectionFont或range.Text.SelectionFont – ruqo