none
Toggling Underline, Bold, Italic and Strikeout in Menu Strip of a Rich Text Box: (This code seems to work when converting to Visual Basic but in C#...) RRS feed

  • Question

  • I got an error displayed:
    "Operator '*' cannot be applied to operands of type 'bool' and System.Drawing.FontStyle"

    Here is an Example when clicking a Bold button, the error seems to be at the variable _with1 and *

    private void boldToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Font ExistingFont = richTextBox1.SelectionFont;
                FontStyle NewFontStyle = new FontStyle();
                var _with1 = ExistingFont;
                NewFontStyle = -((!_with1.Bold) * FontStyle.Bold);
                //toggle
                NewFontStyle += -(_with1.Italic * FontStyle.Italic);
                NewFontStyle += -(_with1.Underline * FontStyle.Underline);
                NewFontStyle += -(_with1.Strikeout * FontStyle.Strikeout);
                richTextBox1.SelectionFont = new Font(ExistingFont, NewFontStyle);
            }

    EDIT
    and found this different code for C#, he declared a function

     private void SetFontStyle(RichTextBox rtb, FontStyle style)
            {
                int selectionStart = rtb.SelectionStart;
                int selectionLength = rtb.SelectionLength;
                int selectionEnd = selectionStart + selectionLength;
                for( int x = selectionStart; x < selectionEnd; ++x )
                    rtb.Select(x, 1);// Toggle font style of the selection
                rtb.SelectionFont = new Font(rtb.SelectionFont, rtb.SelectionFont.Style ^ style);
                rtb.Select(selectionStart, selectionLength);
    
            }
    


    and called that function inside the bold tool strip menu
     private void boldToolStripMenuItem_Click(object sender, EventArgs e)
     {
     SetFontStyle(richTextBox1, FontStyle.Bold);
     }
    
    Doing the same for Italic, Underline and StrikeOut


    Works fine but there seems to be a problem: When Highlighting the typed Text of a richtextbox, and pressing the bold button, it doesnt seem to toggle the whole highlighted text but instead only toggles 1 letter to bold
    Anyone know how to code this??? urgent
    The problem seems to be inside the SetFontStyle function, maybe it has something to do with the StartSelection



    • Moved by Rudedog2 Sunday, January 9, 2011 3:24 PM . move to more appropriate forum . (From:Visual C# Language)
    Saturday, January 8, 2011 5:32 PM

Answers

  •  

    Hi  HelpMeinCsharp,

     

    In VB.NET, True will be auto converted to -1 and False will be auto converted to 0 when we use bool variable in a calculation.

    But in C#, we need use Convert.ToInt32 method to convert the bool variable to integer before we use it in a calculation, and the true's integer value is 1 and the false's integer value is 0.

     

    So we can convert the above VB.NET code to the following C# code:

    Font ExistingFont = this.richTextBox1.SelectionFont;
    FontStyle NewFontStyle = new FontStyle();
    int i = 0;
    i = ((Convert.ToInt32(!ExistingFont.Bold) * Convert.ToInt32(FontStyle.Bold)));
    i += ((Convert.ToInt32(ExistingFont.Italic) * Convert.ToInt32(FontStyle.Italic)));
    i += ((Convert.ToInt32(ExistingFont.Underline) * Convert.ToInt32(FontStyle.Underline)));
    i += ((Convert.ToInt32(ExistingFont.Strikeout) * Convert.ToInt32(FontStyle.Strikeout)));
    NewFontStyle = (FontStyle)i;
    this.richTextBox1.SelectionFont = new Font(ExistingFont, NewFontStyle);
    

     

    For the second question, you can use the following method body instead the original one:

    private void SetFontStyle(RichTextBox rtb, FontStyle style)
    {
      int selectionStart = rtb.SelectionStart;
      int selectionLength = rtb.SelectionLength;
    
      rtb.SelectionFont = new Font(rtb.SelectionFont, rtb.SelectionFont.Style ^ style);
      rtb.Select(selectionStart, selectionLength);
    }
    

     

    If there's any concern, please feel free to follow up.

     

    Have a nice day!


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 18, 2011 7:54 AM
    Moderator

All replies

  • ^bump^

    I am moving your thread to a more appropriate forum for issues with Windows Forms controls.


    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    Sunday, January 9, 2011 3:23 PM
  • Can't test it right now, but I think it's supposed to be :

        private void SetFontStyle(RichTextBox rtb, FontStyle style)
        {
          int selectionStart = rtb.SelectionStart;
          int selectionLength = rtb.SelectionLength;
          int selectionEnd = selectionStart + selectionLength;
    
          // select a letter by letter and change font
          for( int x = selectionStart; x < selectionEnd; ++x )
          {
            // select a single letter
            rtb.Select(x, 1);
            // Toggle font style of the selection
            rtb.SelectionFont = new Font(rtb.SelectionFont, rtb.SelectionFont.Style ^ style);
          }
          rtb.Select(selectionStart, selectionLength);
        }
    
    

    best regards,
    Vladimir

    Sunday, January 9, 2011 8:45 PM
  • And the first method, I'm not sure what it's supposed to do, but don't over complicate it if you don't have to:

    	private void button1_Click(object sender, EventArgs e)
    	{
    		Font existingFont = richTextBox1.SelectionFont;
    		FontStyle newFontStyle = new FontStyle();
    		//toggle bold
    		if (!existingFont.Bold)
    			newFontStyle = FontStyle.Bold;
    		//toggle italic
    		if (!existingFont.Italic)
    			newFontStyle |= FontStyle.Italic;
    		//toggle underline
    		if (!existingFont.Underline)
    			newFontStyle |= FontStyle.Underline;
    		//toggle strikeout
    		if (!existingFont.Strikeout)
    			newFontStyle |= FontStyle.Strikeout;
    		richTextBox1.SelectionFont = new Font(existingFont, newFontStyle);
    	}
    
    

    or just toggle the styles you want ...

    Best regards,
    Vladimir

    Sunday, January 9, 2011 9:00 PM
  •  

    Hi  HelpMeinCsharp,

     

    In VB.NET, True will be auto converted to -1 and False will be auto converted to 0 when we use bool variable in a calculation.

    But in C#, we need use Convert.ToInt32 method to convert the bool variable to integer before we use it in a calculation, and the true's integer value is 1 and the false's integer value is 0.

     

    So we can convert the above VB.NET code to the following C# code:

    Font ExistingFont = this.richTextBox1.SelectionFont;
    FontStyle NewFontStyle = new FontStyle();
    int i = 0;
    i = ((Convert.ToInt32(!ExistingFont.Bold) * Convert.ToInt32(FontStyle.Bold)));
    i += ((Convert.ToInt32(ExistingFont.Italic) * Convert.ToInt32(FontStyle.Italic)));
    i += ((Convert.ToInt32(ExistingFont.Underline) * Convert.ToInt32(FontStyle.Underline)));
    i += ((Convert.ToInt32(ExistingFont.Strikeout) * Convert.ToInt32(FontStyle.Strikeout)));
    NewFontStyle = (FontStyle)i;
    this.richTextBox1.SelectionFont = new Font(ExistingFont, NewFontStyle);
    

     

    For the second question, you can use the following method body instead the original one:

    private void SetFontStyle(RichTextBox rtb, FontStyle style)
    {
      int selectionStart = rtb.SelectionStart;
      int selectionLength = rtb.SelectionLength;
    
      rtb.SelectionFont = new Font(rtb.SelectionFont, rtb.SelectionFont.Style ^ style);
      rtb.Select(selectionStart, selectionLength);
    }
    

     

    If there's any concern, please feel free to follow up.

     

    Have a nice day!


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 18, 2011 7:54 AM
    Moderator