【问题标题】:richtextbox first time error selection apply property valuerichtextbox第一次错误选择应用属性值
【发布时间】:2014-07-05 13:58:10
【问题描述】:

您好,我的 richtextbox.selection.applypropertyvalue() 函数很长时间以来一直出错,当我第一次将它(单击按钮)应用到 Richtextbox 时它无法正常工作(我附上了一张图片 .g​​if下面更深入地显示了问题)

这是我单击按钮时的代码,标签栏上的每个按钮/组合框都是相同的

代码:

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

图片:

图片说明:此图片显示我必须先输入文本,然后才能在文本末尾设置 text 属性。但是,如果我尝试通过单击按钮来执行此操作,我必须再次输入文本,然后再次按下按钮(如图所示)

请注意,MessageBox 只是一个检查选择长度的测试(不是错误)

【问题讨论】:

  • 使用调试器会发生什么?
  • 调试器没有问题,同样的问题
  • @JohnSaunders 你认为这是 Visual Studio / C# 中的一个错误
  • 我的意思是,当您单步执行时,您是否到达了您期望到达的所有行?顺便说一句,大多数人在他们的一生中都没有在 Visual Studio 中发现一个错误。他们在自己的代码中发现了更多错误。
  • 是的,没有调试错误并且达到预期的行数

标签: c# wpf visual-studio select richtextbox


【解决方案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);
    }
}

【讨论】:

【解决方案2】:

在您的按钮点击事件中使用 Focus() 方法。

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

请找到附件。

【讨论】:

  • 这似乎不是一个修复,因为问题是在尝试修改文本 fontweight 时没有输入任何先前的文本
  • 您的问题在于richtextbox 选择的文本。我的意思是,当我们在点击时关注你的richTextBox时,我们可以解决这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-29
  • 1970-01-01
  • 2015-02-06
  • 2017-01-29
  • 1970-01-01
  • 2016-04-10
相关资源
最近更新 更多