相關的你的情況(如果正確描述)時,可以通過修改字符串中使用在[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,
你嘗試了什麼?您的標記似乎表明您將生成的文本放入Word文檔中。是對的嗎? – KevinS
@KevinS是的。我想要的是找到屬於的文本(匹配給出了字符串列表),並替換爲單詞文檔中的粗體文本。 – ruqo
http://stackoverflow.com/questions/9840199/c-sharp-microsoft-office-interop-word – har07