C# richtextbox首次错误选择应用属性值
您好,我在C# richtextbox首次错误选择应用属性值,c#,wpf,visual-studio,select,richtextbox,C#,Wpf,Visual Studio,Select,Richtextbox,您好,我在richtextbox.selection.applypropertyvalue()函数中遇到了一个错误,在我第一次将它应用到richtextbox(单击按钮)时它就不起作用了(我在下面附加了一个.gif图像,它更深入地显示了问题) 下面是我单击按钮时的代码,对于选项卡栏上的每个按钮/组合框都是相同的 代码: private void Button_Click(object sender, RoutedEventArgs e) { System.Win
richtextbox.selection.applypropertyvalue()
函数中遇到了一个错误,在我第一次将它应用到richtextbox(单击按钮)时它就不起作用了(我在下面附加了一个.gif图像,它更深入地显示了问题)
下面是我单击按钮时的代码,对于选项卡栏上的每个按钮/组合框都是相同的
代码:
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);
}
}
}
图片:
图像描述:此图像显示我必须先输入文本,然后才能在文本末尾设置文本属性。但如果我试图通过点击按钮来实现,我必须再次输入文本,然后再次按下按钮(如图所示)
请注意,MessageBox只是一个检查选择长度(而不是错误)的测试。在按钮单击事件中使用Focus()方法
if (!yourRichTextBox.IsFocused)
yourRichTextBox.Focus();
请找到附件中的图片
使用调试器时会发生什么情况?调试器中没有问题,情况相同problem@JohnSaunders你认为这是VisualStudio/c中的一个bug吗?我的意思是,当你迈出一步时,你是否达到了你期望达到的所有目标?顺便说一句,大多数人在他们的有生之年都没有在VisualStudio中发现一个bug。他们在自己的代码中发现了更多的错误。是的,没有调试错误,并且达到了预期的行数。这些点分隔符不起作用,因为我使用的是WPF或richtextbox。e、 g..selectedtext和.SelectionFont这似乎不是一个解决方案,因为问题是在尝试修改文本FontWeight时没有输入以前的文本。您的问题是richtextbox selected text。我的意思是,我们可以解决这个问题,当我们把焦点放在你的richTextBox上点击。
if (!yourRichTextBox.IsFocused)
yourRichTextBox.Focus();