简体   繁体   English

如何在FastColoredTextBox中复制,粘贴和剪切?

[英]How to copy, paste, cut in FastColoredTextBox?

I need three functions: Copy, Paste, Cut , 我需要三个功能:复制,粘贴,剪切,

For a FastColoredTextBox .. so far with my methods, the job is done but afterwards, 到目前为止,对于使用我的方法的FastColoredTextBox ..来说,工作已经完成,但是之后,

the cursor's position get changed and I got no clue on how to keep it where it 光标的位置改变了,我不知道如何将其保持在原处

was before. 以前。

Here's my methods: 这是我的方法:

    private void OnMouseMenuCut(object sender, EventArgs e)
    {
        var sPoint = rtbScript.SelectionStart;
        var ePoint = rtbScript.SelectionLength;

        var text = rtbScript.SelectedText;
        rtbScript.Text = rtbScript.Text.Remove(sPoint, ePoint);

        Clipboard.SetText(text.Replace("\n", "\r\n"));
        rtbScript.Text = rtbScript.Text.Insert(sPoint, text);
    }

    private void OnMouseMenuCopy(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(rtbScript.SelectedText)) return;
        Clipboard.SetText(rtbScript.SelectedText.Replace("\n", "\r\n"));

    }

    private void OnMouseMenuPaste(object sender, EventArgs e)
    {
        if (!Clipboard.ContainsText()) return;
        var index = rtbScript.SelectionStart;

        rtbScript.Text = rtbScript.Text.Insert(index, Clipboard.GetText());
    }

Also, If there's a better way to do those functions, please post.. 另外,如果有更好的方法来执行这些功能,请发布..

Thanks! 谢谢!

For a RichTextBox your code has more issues than loosing the Cursor position, It also looses all formatting! 对于RichTextBox,您的代码比失去Cursor的位置有更多的问题,它还会丢失所有格式! Here are versions that should work better: 以下是一些效果更好的版本:

    private void OnMouseMenuCut(object sender, EventArgs e)
    {
        var sPoint = rtbScript.SelectionStart;
        var text = rtbScript.SelectedText;

        rtbScript.Cut();

        Clipboard.SetText(text.Replace("\n", "\r\n"));
        rtbScript.SelectionStart = sPoint;
    }

    private void OnMouseMenuCopy(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(rtbScript.SelectedText)) return;
        Clipboard.SetText(rtbScript.SelectedText.Replace("\n", "\r\n"));
    }

    private void OnMouseMenuPaste(object sender, EventArgs e)
    {
        if (!Clipboard.ContainsText()) return;
        var index = rtbScript.SelectionStart;

        rtbScript.SelectedText = Clipboard.GetText();
        rtbScript.SelectionStart = index + Clipboard.GetText().Length;
    }

Note: You must never change the Text property of a RTB or else you will mess up the formating! 注意:您绝不能更改RTB的Text属性,否则会弄乱格式!

Since you wrote that this also works with your FastColoredTextBox I have undeleted the solution.. 自从您写到这也可以用于您的FastColoredTextBox我就FastColoredTextBox了解决方案。

In the current version of FCTB, these methods already exist inside the FCTB.cs file. 在当前版本的FCTB中,这些方法已经存在于FCTB.cs文件中。 They just need to be linked up. 他们只需要链接起来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM