如何在C#中删除禁用的TextBox?

时间:2017-06-04 13:29:01

标签: c# drag-and-drop

我想在具有DragDrop属性的TextBox上创建Enabled=false,但在禁用控件时禁用DragDrop。有没有办法实现它,或者它必须是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;
}

1 个答案:

答案 0 :(得分:0)

使用ReadOnly属性而不是Enabled属性。 ReadOnly控件接受Drop事件,但不允许用户以其他方式修改其内容。

ReadOnly可能会提供您所寻求的效果和行为,而无需实施复杂的状态跟踪代码,这样做可以使您的用户界面与标准实践保持一致。