DragEnter、DragOver、DragDrop、leave、回车实现、事件的简单使用

来源:互联网 发布:mac os x 10.12.6 编辑:程序博客网 时间:2024/06/05 11:06

窗体的AllowDrop属性必须设置成true;

且必须有DragEnter事件(单独写DragDrop事件是不会具有拖拽功能的)

1:DragEnter是你拖动后首次在进入某个控件内发生。

2:DragOver发生在DragEnter之后,当你移动拖动对象(鼠标)时发生,类似于MouseMove。

3:DragDrop当你松开鼠标时发生。

        private void textBox2_DragEnter(object sender, DragEventArgs e)        {            if(e.Data.GetDataPresent (DataFormats.FileDrop))            {                e.Effect = DragDropEffects.Move;                MessageBox.Show("The TextBox2 DragEnter");            }        }        //当有文件拖拽到Form1上面时,触发事件        private void Form1_DragEnter(object sender, DragEventArgs e)        {            if (e.Data.GetDataPresent(DataFormats.FileDrop))            {                e.Effect = DragDropEffects.Move;                MessageBox.Show("The Form  DragEnter");            }        }

Leave事件

        private void textBox2_Leave(object sender, EventArgs e)//光标在textBox2上,离开后触发的事件:弹出MessageBox        {            MessageBox.Show("Leave the textBox2");        }

回车:按下Enter键触发事件

        //在这里添加一个回车事件,当按下回车键时,触发事件        private void textBox3_KeyDown(object sender, KeyEventArgs e)        {            if(e.KeyCode==Keys.Enter)//当点击Enter键时            {                MessageBox.Show("输入完成");            }        }

        //groupBox1中包含radioButton1-4,groupBox2包括5-7,下面保证groupBox1只选中一个选项        private void groupBox1_Enter(object sender, EventArgs e)        {            RadioButton[] radioButton = new RadioButton[7]            {                radioButton1,radioButton2,radioButton3,radioButton4,radioButton5,radioButton6,radioButton7            };            foreach (RadioButton r in radioButton)            {                if (r.Checked)                {                    r.Checked = false;                    break;                }            }        }