ruqo
ruqo

Reputation: 65

How to bold strings in a text

My question is similiar to this one https://stackoverflow.com/questions/18944437/replacing-text-between-b-and-b-with-bold-text-in-c-sharp

I want to bold strings between <*strong> tags

I get the strings with

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

I guess I need something like

foreach( string a in matches)
{     
}

But I couldn't figure out what to write inside.

Upvotes: 0

Views: 10197

Answers (2)

korrupt
korrupt

Reputation: 200

as har07 stated in comments you can use it like

       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);  
           }

Upvotes: 0

Alexander Bell
Alexander Bell

Reputation: 7918

Pertinent to your case (if described correctly), you can use the solution given in [ https://stackoverflow.com/questions/18944437/replacing-text-between-b-and-b-with-bold-text-in-c-sharp ] by modifying the string:

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

like shown below:

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

Or, the corresponding solution (modified original) like the following:

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);
    }
}

For more details on font properties of the RichTextBox refer to the following code snippet (from Microsoft documentation online 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,

Upvotes: 1

Related Questions