[英]How do I cut, copy, paste and select all in a TextView control?
我正在尝试使用TextView Gtk控件剪切,复制,粘贴和选择所有内容。 为什么TextView控件? 因为我似乎无法让血腥的TextEditor控件进入多线!
无论如何......我该怎么做:
从TextView控件剪切文本?
从TextView控件复制文本?
将文本粘贴到TextView控件中?
在TextView控件中选择所有文本?
和这个:
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; }
来自: https : //stackoverflow.com/questions/26308501/gtk-textview-copy-and-paste - 但这显然不起作用(因此我的问题)。
我也发现了这个: http : //docs.go-mono.com/ ?link = T%3aGtk.TextView Mono GTK C#docs。 有很多东西似乎根本不存在。
基本上你应该使用TextView
底层TextBuffer
。
要剪切,复制和粘贴,首先我们应该选择我们想要复制的部分(或检查并查看缓冲区是否已经有一些选择),从缓冲区中选择一个我们应该得到TextIter
类型的Iterator的部分,这里是我们该怎么做:
以下是SelectAll的示例:
var start = textview.Buffer.GetIterAtOffset (0);
var end = textview.Buffer.GetIterAtOffset (0);
end.ForwardToEnd ();
textview.Buffer.SelectRange (start, end);
以下是从文本中选择范围[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);
TextIter
有广泛的范围选择方法,例如ForwardChars()
有一个ForwardChars()
法BackwardChars()
。
要检查我们的TextBuffer
了我们应该用任何选择HasSelection
属性:
var hasSelection = textview.Buffer.HasSelection;
现在我们有了一个选定的文本,我们可以简单地将它与剪贴板操作一起使用。
以下是切割选定范围[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);
复制非常类似于Cutting我们应该只用CutClipboard
替换CopyClipboard
:
以下是复制所选范围[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);
最后从剪贴板粘贴一些东西与剪切/复制非常相似
下面是粘贴从剪贴板中一些文字位置0的例子:
var pasteLocation=textview.Buffer.GetIterAtOffset (0);
textview.Buffer.SelectRange (pasteLocation, pasteLocation);
textview.Buffer.PasteClipboard (clipboard);
作为最后一个例子,我们将文本设置为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);
}
你可以使用一些按钮来做到这一点!实际上是button1(复制),button2(粘贴)等等你可以读取用户输入(TextField **。文本**),然后把它放在一个数组之后你可以显示你的数组对于输出(粘贴)以及复制/粘贴输入的选定部分(不是全部),您可以存储索引(从索引到选择结束索引)和在索引之间读取/写入数据(选定索引)并放置用户输入在阵列中享受吧! 我不是#程序员,但我试着让你明白你应该做什么。我希望如此好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.