如何在TextView控件中剪切,复制,粘贴和选择所有内容?

jay*_*t55 4 c# linux gtk gtk# monodevelop

我正在尝试使用TextView Gtk控件剪切,复制,粘贴和选择所有内容.为什么TextView控件?因为我似乎无法让血腥的TextEditor控件进入多线!

无论如何......我该怎么做:

从TextView控件剪切文本?

从TextView控件复制文本?

文本粘贴到TextView控件中?

在TextView控件中选择所有文本?


这是我尝试过的:1小时的谷歌搜索.

还有这个:

TextView tv = ...;
TextIter start, end;
if (tv.Buffer.GetSelectionBounds(start, end)) {
  String selected = tv.Buffer.GetText(start, end);
  Clipboard clipboard = tv.GetClipboard(Gdk.Selection.Clipboard);
  clipboard.Text = selected;
}
Run Code Online (Sandbox Code Playgroud)

来自:https://stackoverflow.com/questions/26308501/gtk-textview-copy-and-paste - 但这显然不起作用(因此我的问题).

我也发现了这个:http://docs.go-mono.com/?link = T%3aGtk.TextView Mono GTK C#docs.有很多东西似乎根本不存在.

use*_*830 8

基本上你应该与TextBuffer你的Underlying合作TextView.

选择

进行剪切,复制和粘贴,首先,我们应该选择我们打算复制的部分(或检查,看看如果缓冲区已经有一些选择与否),选择我们应该得到类型的Iterator的一部分TextIter从缓冲,这里是如何我们能做到:

以下是SelectAll的示例:

var start = textview.Buffer.GetIterAtOffset (0);

var end = textview.Buffer.GetIterAtOffset (0);
end.ForwardToEnd ();

textview.Buffer.SelectRange (start, end);
Run Code Online (Sandbox Code Playgroud)

以下是从文本中选择范围[2,4]的示例:

var start = textview.Buffer.GetIterAtOffset (0);
start.ForwardChars (2);

var end = textview.Buffer.GetIterAtOffset (0);
end.ForwardChars (4);

textview.Buffer.SelectRange (start, end);
Run Code Online (Sandbox Code Playgroud)

TextIter有广泛的范围选择方法,例如ForwardChars()有双胞胎法BackwardChars().

要检查我们TextBuffer是否有任何选择,我们应该使用HasSelectionProperty:

var hasSelection = textview.Buffer.HasSelection;
Run Code Online (Sandbox Code Playgroud)

使用剪贴板

现在我们有了一个选定的文本,我们可以简单地将它与剪贴板操作一起使用.

以下是切割选定范围[2,4] 的示例:

 var clipboard = textview.GetClipboard (Gdk.Selection.Clipboard);

var start = textview.Buffer.GetIterAtOffset (0);
start.ForwardChars (2);

var end = textview.Buffer.GetIterAtOffset (0);
end.ForwardChars (4);

textview.Buffer.SelectRange (start, end);

textview.Buffer.CutClipboard (clipboard, true);
Run Code Online (Sandbox Code Playgroud)

复制非常类似于切割我们应该只替换CutClipboardCopyClipboard:

以下是复制所选范围[2,4] 的示例:

 var clipboard = textview.GetClipboard (Gdk.Selection.Clipboard);

var start = textview.Buffer.GetIterAtOffset (0);
start.ForwardChars (2);

var end = textview.Buffer.GetIterAtOffset (0);
end.ForwardChars (4);

textview.Buffer.SelectRange (start, end);

textview.Buffer.CopyClipboard (clipboard, true);
Run Code Online (Sandbox Code Playgroud)

最后从剪贴板粘贴一些东西与剪切/复制非常相似

下面是一个例子粘贴从剪贴板中一些文字位置0:

var pasteLocation=textview.Buffer.GetIterAtOffset (0);
textview.Buffer.SelectRange (pasteLocation, pasteLocation);

textview.Buffer.PasteClipboard (clipboard);
Run Code Online (Sandbox Code Playgroud)

最后的例子:

作为最后一个例子,我们将文本设置为123456,然后从中剪切34并将其粘贴在开头,最终文本应该像341256:

void TextViewSample ()
{
    textview.Buffer.Text = "123456";
    var clipboard = textview.GetClipboard (Gdk.Selection.Clipboard);
    var start = textview.Buffer.GetIterAtOffset (0);
    start.ForwardChars (2);
    var end = textview.Buffer.GetIterAtOffset (0);
    end.ForwardChars (4);
    textview.Buffer.SelectRange (start, end);
    var hasSelection = textview.Buffer.HasSelection;
    textview.Buffer.CutClipboard (clipboard, true);
    var pasteLocation = textview.Buffer.GetIterAtOffset (0);
    textview.Buffer.SelectRange (pasteLocation, pasteLocation);
    textview.Buffer.PasteClipboard (clipboard);
} 
Run Code Online (Sandbox Code Playgroud)