Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# winforms richtextbox中保留RTF格式的排序行_C#_Winforms_Sorting_Richtextbox_Rtf - Fatal编程技术网

C# winforms richtextbox中保留RTF格式的排序行

C# winforms richtextbox中保留RTF格式的排序行,c#,winforms,sorting,richtextbox,rtf,C#,Winforms,Sorting,Richtextbox,Rtf,有没有办法在winforms richtextbox中对保留RTF格式的行进行排序 var lines = edit.Lines.OrderBy(s => s); edit.Lines = lines.ToArray(); 这项工作做得很好,但显然,会丢失任何RTF格式 我稍微改变了TaW的片段: 1.添加unique可能会破坏第一行的格式设置 2.除了\par标签,还有\pard 这是一个片段,再次感谢TaW!: private void cmdSort_Click(object se

有没有办法在winforms richtextbox中对保留RTF格式的行进行排序

var lines = edit.Lines.OrderBy(s => s);
edit.Lines = lines.ToArray();
这项工作做得很好,但显然,会丢失任何RTF格式

我稍微改变了TaW的片段: 1.添加unique可能会破坏第一行的格式设置 2.除了\par标签,还有\pard

这是一个片段,再次感谢TaW!:

private void cmdSort_Click(object sender, EventArgs e)
    {
        const string PARD = "\\pard";
        var pard = Guid.NewGuid().ToString();

        var pos1 = edit.Rtf.IndexOf(PARD, StringComparison.Ordinal) + PARD.Length;
        if (pos1 < 0) return;
        var header = edit.Rtf.Substring(0, pos1);
        var body = edit.Rtf.Substring(pos1);
        body = body.Replace("\\pard", pard);
        var lines = body.Split(new[] { "\\par" }, StringSplitOptions.None);
        var lastFormat = "";
        var sb = new StringBuilder();
        var rtfLines = new SortedList<string, string>();
        foreach (var line in lines)
        {
            var ln = line.Replace(pard, "\\pard");
            var temp = ln.Replace("\r\n", "").Trim();
            if (temp.Length > 0 && temp[0] != '\\')
            {
                rtfLines.Add(temp.Trim(), lastFormat + " " + ln);
            }
            else
            {
                var pos2 = temp.IndexOf(' ');
                if (pos2 < 0)
                {
                    rtfLines.Add(temp.Trim(), ln);
                }
                else
                {
                    rtfLines.Add(temp.Substring(pos2).Trim(), ln);
                    lastFormat = temp.Substring(0, pos2);
                }
            }
        }
        foreach (var key in rtfLines.Keys.Where(key => key != "}"))
        {
            sb.Append(rtfLines[key] + "\\par");
        }
        edit.Rtf = header + sb;
    }

RichTextBox具有保留RTF格式的属性

[BrowsableAttribute(false)]
public string Rtf { get; set; }

如果文件既没有嵌入图像也没有嵌入表,那么下面是一个代码片段

它使用两个RTF盒。在我的测试中,他们分类正确,并保持所有格式不变

    private void button4_Click(object sender, EventArgs e)
    {
        string unique = Guid.NewGuid().ToString() ; 

        richTextBox1.SelectionStart = 0;
        richTextBox1.SelectionLength = 0;
        richTextBox1.SelectedText = unique;
        int pos1 = richTextBox1.Rtf.IndexOf(unique);

        if (pos1 >= 0)
        {
            string header = richTextBox1.Rtf.Substring(0, pos1);
            string header1 = "";
            string header2 = "";
            int pos0 = header.LastIndexOf('}') + 1;
            if (pos0 > 1) { header1 = header.Substring(0, pos0); header2 = header.Substring(pos0); }
            // after the header comes a string of formats to start the document
            string[] formats = header2.Split('\\');
            string firstFormat = "";
            string lastFormat = "";
            // we extract a few important character formats (bold, italic, underline, font, color)
            // to keep with the first line which will be sorted away
            // the lastFormat variable holds the last formatting encountered
            // so we can add it to all lines without formatting
            // (and of course we are really talking about paragraphs)
            foreach (string fmt in formats)  
                if (fmt[0]=='b' ||  ("cfiu".IndexOf(fmt[0]) >= 0 && fmt.Substring(0,2)!="uc") ) 
                      lastFormat += "\\" + fmt; else firstFormat += "\\" + fmt;
            // add the rest to the header
            header = header1 + firstFormat;
            // now we remove our marker from the body and split it into paragraphs
            string body = richTextBox1.Rtf.Substring(pos1);
            string[] lines = body.Replace(unique, "").Split(new string[] { "\\par" }, StringSplitOptions.None);

            StringBuilder sb = new StringBuilder();
            // the soteredlist will contain the unformatted text as key and the formatted one as valaue
            SortedList<string, string> rtfLines = new SortedList<string, string>();
            foreach (string line in lines)
                {
                    // cleanup
                    string line_ = line.Replace("\r\n", "").Trim();
                    if (line_[0] != '\\' ) rtfLines.Add(line_, lastFormat + " " + line);
                    else
                    {
                        int pos2 = line_.IndexOf(' ');
                        if (pos2 < 0) rtfLines.Add(line_, line);
                        else
                        {
                            rtfLines.Add(line_.Substring(pos2).Trim(), line);
                            lastFormat = line_.Substring(0, pos2);
                        }
                    }

                }
            foreach (string key in rtfLines.Keys) if (key != "}") sb.Append(rtfLines[key] + "\\par");
            richTextBox2.Rtf = header + sb.ToString();
        }
当然,这是真正的q&d,还没有准备好进行正式生产;但这似乎是一个开始


编辑2:我更改了代码以修复第一行格式的错误,并添加了一些注释。这应该可以更好地工作,但仍然是一种必须适应实际输入文件的黑客行为。

Rtf属性只是一个包含Rtf格式标记的字符串。我已经考虑过按\par标记拆分它,但看起来我真的是一团糟。一团糟的程度将取决于您的格式设置。如果没有嵌入图像,它实际上可能相当简单,请参见上面我的建议。它看起来真的像开始,谢谢!如果可能的话,我会检查这是否有用——声誉不够:检查一下,如果它真的对你有用,你可以“接受”它作为答案。要做到这一点,你不需要任何声誉;事实上,这样做会给你带来两点好处。。