public static void SetRichTextBoxDropString(System.Windows.Forms.TextBoxBase yourCtr, Action action = null)
{
if (yourCtr == null)
{
return;
}
if (yourCtr is RichTextBox)
{
((RichTextBox)yourCtr).AllowDrop = true;
}
else if (yourCtr is TextBox)
{
((TextBox)yourCtr).AllowDrop = true;
}
else
{
yourCtr.AllowDrop = true;
}
yourCtr.DragDrop += (sender, e) =>
{
System.Windows.Forms.TextBoxBase tempTextBoxBase = sender as System.Windows.Forms.TextBoxBase;
string tempText = (string)e.Data.GetData(typeof(string));
if (tempText == null || tempTextBoxBase == null)
{
return;
}
int selectionStart = tempTextBoxBase.SelectionStart;
tempTextBoxBase.Text = tempTextBoxBase.Text.Insert(selectionStart, tempText);
tempTextBoxBase.Select(selectionStart, tempText.Length);
action?.Invoke();
};
yourCtr.DragEnter += (sender, e) =>
{
if (e.Data.GetData(typeof(string)) == null)
{
e.Effect = System.Windows.Forms.DragDropEffects.None;
}
else
{
e.Effect = System.Windows.Forms.DragDropEffects.Move;
}
};
}