Catch the text before inserting it into the text box

I have a text view, and when something is pasted into it from the clipboard, I need to intercept this text and do some preprocessing before it appears in text form.

I tried listening to the "PasteClipboard" event, which does not give me a way to change the incoming text. and the textview.Buffer.Changed event, which fires after inserting the text, brings it to the text view.

Thanks in advance.

+3
source share
3 answers

AFAIK is your best option - the post-process of the text after it is inserted - the InsertText event in the TextBuffer contains arguments that indicate the location and size of the inserted text, so you can delete, process and paste it again. Of course, you would like to avoid catching 1-char inserts (keystrokes) and your own repeated inserts, but this is trivial.

The only other option I can think of is to re-implement folder support by catching the paste key command, middle-click, etc. - but note that command keys can be overridden in users gtkrc files, so implementing this correctly can get hairy.

It may also be interesting to request on the # gtk + IRC channel on irc.gnome.org.

+3
source

, Gtk.TextBuffer GTK .net WndProc Mono. , [GLib.ConnectBefore] GTK WndProc. Beaner , , GTK.

+1

, WM_PASTE , TextBox. GetText , , , .Text , . , , base.WndProc(ref m).

:

protected override void WndProc(ref Message m)
{
    if (m.Msg == WM_PASTE)
    {
        string clipboardVin = Clipboard.GetText();
        string newVin = "";
        if (SelectionLength > 0)
        {
            newVin = Text.Replace(SelectedText, "");
        }
        else
        {
            newVin = Text;
        }
        newVin = newVin.Insert(SelectionStart, clipboardVin);
        if (!vinRegEx.IsMatch(newVin))
        {
            m.Result = new IntPtr(Convert.ToInt32(true));
            MessageBox.Show("The resulting text is not a valid VIN.", "Can Not Paste", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else
        {
            base.WndProc(ref m);
        }
    }
    else
    {
        base.WndProc(ref m);
    }
}
0

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


All Articles