3

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:

enter image description here

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)

  • What happens when you use the debugger? – John Saunders Jul 06 '14 at 18:46
  • no problem in the debugger its the same problem –  Jul 06 '14 at 18:47
  • @JohnSaunders do you reckon this is a bug in visual studio / c# –  Jul 06 '14 at 18:51
  • 3
    I mean, when you single-step, do you reach all the lines you expect to reach? BTW, most people don't find a single bug in Visual Studio in their lifetime. They find many more bugs in their own code. – John Saunders Jul 06 '14 at 18:57
  • yes there are no debug errors and lines expected are reached –  Jul 06 '14 at 18:59
  • I could send over the rest of the code but that just runs other peices of the app which don't effect text decoration. –  Jul 06 '14 at 19:00

2 Answers2

1
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);
    }
}
Gwenc37
  • 2,064
  • 7
  • 18
  • 22
Codezilla
  • 153
  • 1
  • 9
  • these dot separators do not work either because I am using WPF or a richtextbox. e.g. .selectedtext and .selectionfont –  Jul 14 '14 at 16:40
  • http://stackoverflow.com/questions/6403902/making-specific-text-bolded-in-a-textbox – Codezilla Jul 14 '14 at 18:45
1

use Focus() method in your button click event.

if (!yourRichTextBox.IsFocused) 
            yourRichTextBox.Focus();

please find the attached image.

enter image description here

  • that doesn't seem to be a fix since the problem is when there has been no prior text enter while trying to modify the text fontweight –  Oct 20 '14 at 16:45
  • your problem is with richtextbox selected text. i mean , we can solve the issue when we make a focus to your richTextBox on click. – Karthik Krishna Baiju Oct 20 '14 at 17:33