我如何剪切,复制,粘贴和在TextView控件选择呢?

问题描述:

我想剪切,复制,粘贴和TextView的基于GTK控件中选择所有。为什么TextView控件? !因为我似乎无法得到血腥文本编辑控制去多行

I'm trying to cut, copy, paste and select all with the TextView Gtk control. Why the TextView control? Cause I can't seem to get the bloody TextEditor control to go multiline!

反正...我如何:

剪切从TextView控件的文字?

Cut text from the TextView control?

复制从TextView控件的文字?

Copy text from the TextView control?

粘贴文本到TextView控件?

Paste text into the TextView control?

?在TextView控件文本

Select All text in the TextView control?



以下是我已经试过:
1小时谷歌搜索


Here's what I've tried: 1 hour of googling.

和这样的:

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;
}



来自:的 GTK#TextView的复制和粘贴 - ?。但是这显然是行不通的(因此我的问题)

from: GTK# textview copy and paste? - but that obviously doesn't work (hence my question).

我也发现了这一点: http://docs.go-mono.com/ ?链接= T%3aGtk.TextView Mono的GTK C#文档。有,只是似乎是不存在的这么多东西。

I've also found this: http://docs.go-mono.com/?link=T%3aGtk.TextView The Mono GTK C# docs. There is so much stuff that just seems to be non-existent.

基本上你应该有潜在 TextBuffer 的TextView

Basically you should work with Underlying TextBuffer from your TextView.

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

To Cut, Copy and Paste, First we should select the part we intended to Copy (or check and see if the buffer already has some selection or not), to select a part we should get an Iterator of type TextIter from buffer, here is how we can do it:

下面是对于全选的例子

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

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

textview.Buffer.SelectRange (start, end);

下面是一个例子,是从文本选择范围[2,4]:

Here is an example is for selecting range [2,4] from text:

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

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

textview.Buffer.SelectRange (start, end);



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

TextIter have extensive methods for range selection for example ForwardChars() has a twin method BackwardChars().

要检查我们的 TextBuffer 有我们应该使用 HasSelection 属性的选择:

To check if our TextBuffer has any selection we should use HasSelection Property:

var hasSelection = textview.Buffer.HasSelection;



剪贴板


$ B $工作b

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

Working with Clipboard

Now that we have a selected text we can simply use it with Clipboard actions.

下面是一个示例的切割选择的范围[2,4]:

Here is a sample for Cutting selected range [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);



复制非常相似的切割:我们应该只替换 CutClipboard CopyClipboard

Copying is very similar to Cutting we should only replace CutClipboard with CopyClipboard :

下面是对样品的复制选择的范围[2,4]:

Here is a sample for Copying selected range [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);

和最后的粘贴从剪贴板的东西非常相似的切割/复制

and finally Pasting something from clipboard is very similar to Cutting/Copying

下面是的粘贴一些文​​本从剪贴板位置0的例子:

Here is an example of Pasting some text from Clipboard to location 0:

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

textview.Buffer.PasteClipboard (clipboard);



最后一个例子:



作为最后一个例如,我们从它设置文本为 123456 ,然后切 34 ,并在一开始粘贴,最后文本应该是这样的 341256

Final Example:

As a final example, we set text to 123456 and then cut 34 from it and paste it at the beginning, the final text should be like 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);
}