How to cut, copy, paste and select everything in a TextView control?

I am trying to cut, copy, paste and select everything using the TextView Gtk control. Why a TextView control? The reason I can't get the bloody TextEditor control to go multi-line!

Anyway ... Like me:

Cut text from TextView control?

Copy text from TextView control?

Paste text into a TextView control?

Select all text in a TextView control?

<h / "> Here's what I tried: 1 hour of searching the Internet.

and this:

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

from: https://stackoverflow.com/questions/26308501/gtk-textview-copy-and-paste - ( ).

: http://docs.go-mono.com/?link=T%3aGtk.TextView Mono GTK # docs. , .

+4
2

Underlying TextBuffer TextView.

, , , ( , ), , TextIter , :

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() BackwardChars().

, TextBuffer - , HasSelection :

var hasSelection = textview.Buffer.HasSelection;

, , Clipboard.

[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:

[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);
} 
+8

! 1 (), 2 () .. (TextField **. **), () / ( ), ( ) / ( ) ! #, , . ,

0

Source: https://habr.com/ru/post/1568144/


All Articles