Run Event Textbox c# WPF

Asked

Viewed 60 times

0

I have a Textbox and need that after pressing the right mouse button and clicking paste, it do a check, I tried to use various methods like

TxtTexto.PreviewMouseLeftButtonDown+= new MouseButtonEventHandler(OnMouseClick);
TxtTexto.MouseUp += new MouseButtonEventHandler(OnMouseClick);
TxtTexto.PreviewMouseDown += new MouseButtonEventHandler(OnMouseClick);
TxtTexto.MouseDown += new MouseButtonEventHandler(OnMouseClick);

Among other codes, it is possible to capitulate this event? Imagem do click no textbox

1 answer

1

Try something like this.

public Window1()
{
    InitializeComponent();

    // "tb" é o seu TextBox
    DataObject.AddPastingHandler(tb, OnPaste);
}

private void OnPaste(object sender, DataObjectPastingEventArgs e)
{
    //faça suas verificações aqui use o 'e' para obter os dados presente no past.
    var isText = e.SourceDataObject.GetDataPresent(DataFormats.UnicodeText, true);
    if (!isText) return;

    var text = e.SourceDataObject.GetData(DataFormats.UnicodeText) as string;

}

References https://stackoverflow.com/questions/3061475/paste-event-in-a-wpf-textbox

  • I took the test here, and it didn’t work I switched the tb for my textobox and did nothing...

  • In this case, it is pasting the text and making 2 checks

  • But the event OnPaste is evoked? What checks are you making?

  • yes, it is evoked, in case I take the text, do the checks and at the end I serve the formatted text according to what I need

Browser other questions tagged

You are not signed in. Login or sign up in order to post.