怎样在c#中判断richtextbox是否为纯文本或是否包含图片

来源:百度知道 编辑:UC知道 时间:2024/06/07 06:25:21
如题!
感谢 无毒欧阳锋 的提示 richtextbox.Rtf.IndexOf(@"{\pict\") > -1 测试成功

包含图片的时候,里面存在<img>标签,
如果richtextbox的内容
indexOOf("<img")>-1
或者
indexOOf("<IMG")>-1
就有图片了

可以用一种比较巧妙的方式来达到你的目的,
原理:先将所有内容全选,然后获取选取的内容的类型即使用属性SelectionType来实现,
然后判断SelectionType来达到目的。我们在一个button里面判断,代码:
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.SelectionStart = 0;
richTextBox1.SelectionLength = richTextBox1.Text.Length;

//==================可以被下面那段代替===================
RichTextBoxSelectionTypes rtfSelType = richTextBox1.SelectionType;
richTextBox1.SelectionLength = 0;
if ((rtfSelType & RichTextBoxSelectionTypes.Object) == RichTextBoxSelectionTypes.Object)
MessageBox.Show("不是纯文本!");
else if (rtfSelType != RichTextBoxSelectionTypes.Empty)
MessageBox.Show("是纯文本!");