b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

richtextbox显示文本?richtextbox光标在最后?RichTextBox(2)

电脑杂谈  发布时间:2016-06-11 07:02:00  来源:网络整理

操作Selection:

if (rbtMyRichTextBox != null && rbtMyRichTextBox.Selection.Text.Length > 0) { if (rbtMyRichTextBox.Selection.GetPropertyValue(Run.FontWeightProperty) is FontWeight && ((FontWeight)rbtMyRichTextBox.Selection.GetPropertyValue(Run.FontWeightProperty)) == FontWeights.Normal) { rbtMyRichTextBox.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Bold); } else { rbtMyRichTextBox.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Normal); } } if (rbtMyRichTextBox != null) { rbtMyrichtextbox.Focus(); } 上面的代码中,我们获取了选中文本的FontWeight属性,通过检查它是Normal还是Bold改变其值,这里的逻辑也可以用在FontSize等其它的属性上。 简单的复制,剪切,粘贴功能:

Sl4支持Clipboard功能,所以通过访问Clipboard我们可以很容易实现剪切,复制功能

private void Copy_Click(object sender, RoutedEventArgs e) { Clipboard.SetText(rbtMyRichTextBox.Selection.Text); rbtMyRichTextBox.Focus(); } private void Cut_Click(object sender, RoutedEventArgs e) { Clipboard.SetText(rbtMyRichTextBox.Selection.Text); rbtMyRichTextBox.Selection.Text = ""; rbtMyrichtextbox.Focus(); }

上面的代码分别演示如何对richtextbox中被选中的文本进行复制,剪切

private void Paste_Click(object sender, RoutedEventArgs e) { rbtMyRichTextBox.Selection.Text = Clipboard.GetText(); rbtMyrichtextbox.Focus(); }

复制的时候,如果richtextbox没有选择任何元素,那么剪切板上的文本将显示在鼠标的当前位置上。注意的是,Cilpboard只能保护简单

的文本,所以当粘贴,复制的时候会丢失之前格式化的信息。

保存内容为文件:

richtextbox提供了一个名为Xaml的属性,通过这个属性可以很方便的将内容保存为文件

private void Save_Click(object sender, RoutedEventArgs e) { var uiElements = from block in rbtMyRichTextBox.Blocks from inline in (block as Paragraph).Inlines where inline.GetType() == typeof(InlineUIContainer) select inline; if (uiElements.Count() != 0) { MessageBox.Show("UIElements cannot be saved"); return; } SaveFileDialog sfdSaveXaml = new SaveFileDialog(); sfdSaveXaml.DefaultExt = ".sav"; sfdSaveXaml.Filter = "Saved rtb files|*.rtb"; if (sfdSaveXaml.ShowDialog().Value) { using (FileStream fs = (FileStream)sfdSaveXaml.OpenFile()) { System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding(); byte[] buffer = enc.GetBytes(rbtMyrichtextbox.Xaml); fs.Write(buffer, 0, buffer.Length); fs.Close(); } } }

因为richtextbox不支持保存InlineUIContainer的元素,所以上面的代码中先将其排除,然后通过FileStream保存

打开文件:

这个与保存文件过程是相反的,只要将richtextbox的Xaml属性设置为读出的XAML

private void Open_Click(object sender, RoutedEventArgs e) { OpenFileDialog ofdOpenXaml = new OpenFileDialog(); ofdOpenXaml.Multiselect = false; ofdOpenXaml.Filter = "Saved rtb files|*.rtb"; if (ofdOpenXaml.ShowDialog().Value) { FileInfo fiXamlFile = ofdOpenXaml.File; StreamReader sr = fiXamlFile.OpenText(); rbtMyrichtextbox.Xaml = sr.ReadToEnd(); sr.Close(); } }

上面的保存,读取的文件只支持rtb格式的,其实可以使用其它的格式,当然肯定需要额外的编码,比如你打开一个文本文件,那么

首先需要你创建所必须的Run标记。总之,你需要将内容转换成Xaml格式。

对于保存文件,可以看看CodePlex上一个开眼项目:WordToXaml

这篇文章是参考的国外这篇richtextbox,文中的源码可以在其中找到。

快到年底了,也许这是2010年写下的最后一篇博文了,11年Silverlight5就出来了,作为一个专注于Silverlight技术的开发者,我对未来

越来越充满期待,。

posted @

以上就是关于richtextbox的全部内容,相信你一定会非常满意。


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/shenmilingyu/article-8612-2.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...