简体   繁体   中英

How to dragdrop on a disabled TextBox in C#?

I want to make a DragDrop on a TextBox which has Enabled=false property, but DragDrop is disabled as the control is disabled. Is there a way to make it or it has to be Enabled=true

private void textBox1_DragEnter(object sender, DragEventArgs e)
{
   if (e.Data.GetDataPresent(DataFormats.FileDrop))
       e.Effect = DragDropEffects.Copy;
}
void textBox1_DragDrop(object sender, DragEventArgs e)
{
   string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
   if (files.Length < 1)
       return;
   if (new string[] { ".txt", ".csv" }.Contains(Path.GetExtension(files[0])))
   textBox1.Text = Path.GetFileNameWithoutExtension(files[0]);
   e.Effect = DragDropEffects.None;
}

Use the ReadOnly property instead of the Enabled property. A ReadOnly control accepts Drop events but does not allow the user to otherwise modify its content.

ReadOnly likely gives the effect and behavior you seek without having to implement complicated state tracking code and doing so will keep your user interface congruent with standard practices.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM