Getting ToolStrip button's CheckState to reflect RichTextBox content
-
Dear Sirs: I have a C# application with a ToolStrip control and a RichTextBox control. Three of the buttons are for aligning the text, i.e., Align Left, Center, Align Right. And two of the buttons are for increasing and decreasing Indentation. I have the CheckOnClick property set to true for these buttons. My application also has formatting buttons, i.e., Bold, Italic, Underlined and one button for setting a bulleted style. It was previously suggested that I use the RichTextBox_SelectionChanged event, which in fact works for the formatting buttons and Bullets button. However, I am running into a dead-end when attempting to write code for the alignment buttons or the 2-Indentation buttons. For example, when I try something like the following code:
tsBtnAlignLeft.Checked = richTextBoxBody.SelectionAlignment;
tsBtnAlignCtr.Checked = richTextBoxBody.SelectionAlignment;
tsBtnAlignRight.Checked = richTextBoxBody.SelectionAlignment;it throws this exception: "Cannot implicitly convert type System.Windows.Forms.HorizontalAlignment' to 'bool'" And when I try to add further code in the RTB's SelectionChanged event for the IncreaseIndentation or DecreaseIndentation button's, i.e.,
tsbtnIncreaseIndent.Checked = richTextBoxBody.SelectionIndent;
it throws the following exception: "Cannot implicitly convert type 'int' to 'bool'" Can anyone provide a suggestion for getting the Checked property of the alignment buttons and the indentation buttons to reflect the contents of the RichTextBox as the cursor moves over text that is aligned and\or indented? Thank you in advance. Richard -
Dear Sirs: I have a C# application with a ToolStrip control and a RichTextBox control. Three of the buttons are for aligning the text, i.e., Align Left, Center, Align Right. And two of the buttons are for increasing and decreasing Indentation. I have the CheckOnClick property set to true for these buttons. My application also has formatting buttons, i.e., Bold, Italic, Underlined and one button for setting a bulleted style. It was previously suggested that I use the RichTextBox_SelectionChanged event, which in fact works for the formatting buttons and Bullets button. However, I am running into a dead-end when attempting to write code for the alignment buttons or the 2-Indentation buttons. For example, when I try something like the following code:
tsBtnAlignLeft.Checked = richTextBoxBody.SelectionAlignment;
tsBtnAlignCtr.Checked = richTextBoxBody.SelectionAlignment;
tsBtnAlignRight.Checked = richTextBoxBody.SelectionAlignment;it throws this exception: "Cannot implicitly convert type System.Windows.Forms.HorizontalAlignment' to 'bool'" And when I try to add further code in the RTB's SelectionChanged event for the IncreaseIndentation or DecreaseIndentation button's, i.e.,
tsbtnIncreaseIndent.Checked = richTextBoxBody.SelectionIndent;
it throws the following exception: "Cannot implicitly convert type 'int' to 'bool'" Can anyone provide a suggestion for getting the Checked property of the alignment buttons and the indentation buttons to reflect the contents of the RichTextBox as the cursor moves over text that is aligned and\or indented? Thank you in advance. RichardIn the SelectionChanged event you will need to analyze the selection alignment and set the Checked property of the left/right/centered buttons to suit. This is because the SelectionAlignment property can be more than just true or false (boolean), where the checked property is a boolean.
-
In the SelectionChanged event you will need to analyze the selection alignment and set the Checked property of the left/right/centered buttons to suit. This is because the SelectionAlignment property can be more than just true or false (boolean), where the checked property is a boolean.
Thanks Cassandra: I previously wasn't able to get the syntax to do what I needed to do. I just kept dancing around the issue in code, but couldn't quite get it right. That took care of it. Thanks for responding. Richard