如何使只有RichTextBox的文本?文本、RichTextBox

2023-09-04 02:18:09 作者:便便

可能重复:   How以prevent的RichTextBox内粘贴的图片?

如果您使用的是的RichTextBox ,也有的RichTextBox 例如几大优势:

If you're using Richtextbox, there are several advantages in Richtextbox for example:

我们可以使用颜色的字体就可以了。

we can use color font on it

设置自定义字体的区域

附加在其上的文件。等等。

Attach files on it.. etc

先看看图片:

take a look at the picture:

下面是我的问题:

我可以让只有它的文字?

在我的项目,请将文件或类似不必要的。我甚至不想附着或粘贴在上面的图片,我只是想纯文本在的RichTextBox

In my project, attach file or the like is unnecessary at all. I even didn't want attach or paste an images on it, i just want "text only" on Richtextbox

我怎样才能做到这一点?

How can i do that?

推荐答案

由于RichTextBox中没有图像或对象集合,你必须去为RTF格式codeS。 RichTextBox中的所有数据存储为纯文本特殊格式codeS,这是通过其RTF属性控制曝光。如果你想读或改变它,学习资源容易获得整个网络,例如参见本概览学习这个code语言是必不可少的。 RichTextBox的使用更简单的RTF codeS不是多个全功能的编辑器,如微软Word等,所以它通常是有益的数据加载到RTB操纵它之前,这将消除很多冗余数据。

Since RichTextBox doesn't have a Images or Objects collection you have to go for the RTF formatting codes. All data of RichTextBox is stored as plain text with special formatting codes, this is exposed by the control through its RTF property. Learning this code language is essential if you want to read or change it, learning resources are easily available throughout the web, see for example this overview. RichTextBox uses more simplified rtf codes than several full-feature editors like MS Word etc, so it is usually beneficial to load data into a RTB before manipulating it, this will remove much redundant data.

使长话短说,我发现,这是必要的,以搜索开始任PICT或对象命令RTF组。明知组可以嵌套,你不能只找到在那里的第一端基字符,你必须焦炭,同时保持分组找到这些群体的结束数来分析字符串字符。现在,你有足够的信息以消除串的一部分。 RTF可以包含多个图片/对象组,所以你必须这样做,直到所有被删除。下面是消除这些群体后返回RTF字符串示例函数:

Making a long story short, I found that it is necessary to search for rtf groups that start with either "pict" or "object" command. Knowing that groups may be nested you can't just find the first end-group char from there, you have to parse the string char by char while keeping count of grouping to find the end of those groups. Now you have enough information to remove that part of the string. Rtf may contain multiple picture/object groups so you have to do this until all are removed. Here is a sample function that return rtf string after removing those groups:

private string removeRtfObjects(string rtf)
{
    //removing {\pict or {\object groups
    string pattern = "\\{\\\\pict|\\{\\\\object";
    Match m = Regex.Match(rtf, pattern);
    while (m.Success) {
        int count = 1;
        for (int i = m.Index + 2; i <= rtf.Length; i++) {
            //start group
            if (rtf(i) == '{') {
                count += 1;
            //end group
            } else if (rtf(i) == '}') {
                count -= 1;
            }
            //found end of pict/object group
            if (count == 0) {
                rtf = rtf.Remove(m.Index, i - m.Index + 1);
                break; // TODO: might not be correct. Was : Exit For
            }
        }
        m = Regex.Match(rtf, pattern);
        //go again
    }
    return rtf;
}

在应该这样做?你已经提到粘贴,还插入,这些都可以被困KeyDown事件,你得到的剪贴板信息,并采取相应措施。设置e.Handled = TRUE,当你已经处理了自己的操作信号,控制不应该做任何默认处理这个组合键。这也是你如何阻止粘贴图像,而不破坏用户剪贴板。例如:

When should this be done? You have already mention Paste, there is also Insert, these can be trapped with the KeyDown event where you get the clipboard info and handle it accordingly. Setting e.Handled=True when you have handled the operation yourself signals that the control should not do any default processing for this key combination. This is also how you block pasting images without destroying the users clipboard. Example:

private void RichTextBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    //aware of Paste or Insert
    if (e.Control && e.KeyCode == Keys.V || e.Shift && e.KeyCode == Keys.I) {
        if (Clipboard.ContainsImage || Clipboard.ContainsFileDropList) {
            //some images are transferred as filedrops
            e.Handled = true;
            //stops here
        } else if (Clipboard.ContainsData(DataFormats.Rtf)) {
            RichTextBox rtbox = new RichTextBox();
            //use a temp box to validate/simplify
            rtbox.Rtf = Clipboard.GetData(DataFormats.Rtf);
            this.RichTextBox1.SelectedRtf = this.removeRtfObjects(rtbox.Rtf);
            rtbox.Dispose();
            e.Handled = true;
        }
    }
}
 
精彩推荐