Change paste contents in Textbox

后端 未结 4 1645
逝去的感伤
逝去的感伤 2020-12-05 14:44

How can I dynamically change the contents of what will be pasted in the TextBox.

Here is how I subscribe to the event:

DataObject.AddPastingHandler (         


        
4条回答
  •  时光说笑
    2020-12-05 15:34

    You cannot call args.DataObject.SetData("some data") since the DataObject is frozen. What you can do is replace the DataObject altogether:

    private void TextBoxPaste(object sender, DataObjectPastingEventArgs e) {
            string text = (String)e.DataObject.GetData(typeof(String));
            DataObject d = new DataObject();
            d.SetData(DataFormats.Text, text.Replace(Environment.NewLine, " "));
            e.DataObject = d;
     }
    

提交回复
热议问题