none
Controlling pasting of data in RichTextBox

    Question

  • Hi,

    I would like to know if there is anyway to control data being pasted to the RichTextBox?  At present, pasted data is automatically handled and only on the OnTextChanged event can you make changes to pasted data.

    Accessing the DataObject is futile as you can only add data and not modify any existing data inside the object.

     

    Thank you,

    Jaco

    Wednesday, March 28, 2007 12:16 PM

Answers

  • use the dataovject pasteeventhandler

     

    you can now control pasting data into the richtextbox

     

    rtb.AddHandler(DataObject.PastingEvent, new DataObjectPastingEventHandler(PasteEvent), true);

    private void PasteEvent(object sender, DataObjectPastingEventArgs e)

    {

    }
    Wednesday, March 28, 2007 1:32 PM
    Moderator
  • create a new dataobject, copy all the passed formats + new formats and set it on the DataObjectPastingEventArgs 

     

    private void PasteEvent(object sender, DataObjectPastingEventArgs e)

    {

    DataObject d = new DataObject();

    d.SetData(DataFormats.Text, "HELLO");

    e.DataObject = d;

    }

    Wednesday, March 28, 2007 5:41 PM
    Moderator

All replies

  • use the dataovject pasteeventhandler

     

    you can now control pasting data into the richtextbox

     

    rtb.AddHandler(DataObject.PastingEvent, new DataObjectPastingEventHandler(PasteEvent), true);

    private void PasteEvent(object sender, DataObjectPastingEventArgs e)

    {

    }
    Wednesday, March 28, 2007 1:32 PM
    Moderator
  • I have tried that, but the object is still frozen.
    Wednesday, March 28, 2007 1:34 PM
  • create a new dataobject, copy all the passed formats + new formats and set it on the DataObjectPastingEventArgs 

     

    private void PasteEvent(object sender, DataObjectPastingEventArgs e)

    {

    DataObject d = new DataObject();

    d.SetData(DataFormats.Text, "HELLO");

    e.DataObject = d;

    }

    Wednesday, March 28, 2007 5:41 PM
    Moderator
  • Thank you very much.  It works perfectly.  Can't believe I didn't investigate the fact that the DataObject property itself

    contains a setter as well Stick out tongue

    Thursday, March 29, 2007 7:49 AM