Reputation:
Hi I have had an error for a long time with the richtextbox.selection.applypropertyvalue()
function where it wont work the first time I apply it (click the button) to the richtextbox it just wont do so (I have attached a image .gif below which shows the problem much more in depth)
Here is the code for when I click the button, it is the same for each button / combobox that is on the tab bar
CODE:
private void Button_Click(object sender, RoutedEventArgs e)
{
System.Windows.MessageBox.Show(textselectrangea.Text.Length.ToString());
if (textselectrangea.Text.Length != 0)
{
if (textselectrangea.GetPropertyValue(TextElement.FontWeightProperty).ToString() == "Normal" || textselectrangea.GetPropertyValue(TextElement.FontStyleProperty).ToString() == "{DependencyProperty.UnsetValue}")
{
boldbutton.FontWeight = FontWeights.Bold;
textselectrangea.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
}
else if (textselectrangea.GetPropertyValue(TextElement.FontWeightProperty).ToString() == "Bold")
{
boldbutton.FontWeight = FontWeights.Normal;
textselectrangea.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Normal);
}
}
//I think error occurs below here
else if (textselectrangea.Text.Length == 0)
{
if (richtextboxfile.Selection.GetPropertyValue(TextElement.FontWeightProperty).Equals(FontWeights.Normal))
{
boldbutton.FontWeight = FontWeights.Bold;
richtextboxfile.Selection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
}
else if (richtextboxfile.Selection.GetPropertyValue(TextElement.FontWeightProperty).Equals(FontWeights.Bold))
{
boldbutton.FontWeight = FontWeights.Normal;
richtextboxfile.Selection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Normal);
}
}
}
image:
Description Of Image: this image is showing that I have to enter text first before I can set the text property on the end of the text. But if I try to do so by clicking the button I once again have to enter text and then again press the button (described in photo)
just a note the MessageBox is just a test that checks selection length (Not the error)
Upvotes: 3
Views: 1255
Reputation: 459
use Focus() method in your button click event.
if (!yourRichTextBox.IsFocused)
yourRichTextBox.Focus();
please find the attached image.
Upvotes: 1
Reputation: 153
if (richtextboxfile.SelectedText.Length > 0) // If: there's any text selected toggle bold on that text
{
// Toggle bold on/off for selected text
if (richtextboxfile.SelectionFont.Style != FontStyle.Bold) // If: Current selected text is not bold, then bold it
{
richtextboxfile.SelectionFont = new Font(richtextboxfile.Font, FontStyle.Bold);
}
else // Else: selected text is bold, set it to regular text (non-bold)
{
richtextboxfile.SelectionFont = new Font(richtextboxfile.Font, FontStyle.Regular);
}
}
else // Else: No text is selected, make the font bold from here on out regardless of position
{
// if bold is not already enabled
if (richtextboxfile.SelectionFont.Style == FontStyle.Regular) // Toggle bold ON at current position
{
richtextboxfile.SelectionFont = new Font(richtextboxfile.Font, FontStyle.Bold);
}
else if (richtextboxfile.SelectionFont.Style == FontStyle.Bold) // Toggle bold OFF at current position
{
richtextboxfile.SelectionFont = new Font(richtextboxfile.Font, FontStyle.Regular);
}
}
Upvotes: 1