Answered Controlling pasting of data in RichTextBox

  • Wednesday, March 28, 2007 12:16 PM
     
     

    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

All Replies

  • Wednesday, March 28, 2007 1:32 PM
    Moderator
     
     Answered

    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:34 PM
     
     
    I have tried that, but the object is still frozen.
  • Wednesday, March 28, 2007 5:41 PM
    Moderator
     
     Answered

    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;

    }

  • Thursday, March 29, 2007 7:49 AM
     
     

    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