C# winform教程(二)----GroupBox

发布于:2025-06-20 ⋅ 阅读:(15) ⋅ 点赞:(0)

一、作用

带有标题的容器,一般只做个容器作用。

二、属性

控件没啥属性,一般就是个容器,用来规范和显示组的控件。

属性
名称 内容 含义
Text 名称 显示的顶部名称

三、事件

事件有很多,但是基本都是不使用的,谁会没事点击容器来响应事件呢;

但是容器对于拖曳等动作还是比较重要的,当拖入到容器内变化鼠标,或者设置内容等;

事件
名称 内容

含义

DragDrop 拖曳完成触发 粘贴事件
DragEnter 拖曳进入控件触发 粘贴选项,是否粘贴
DragLeave 拖曳离开触发 执行复制

四、示例

这个例子包含拖曳功能,比较经典的文本拖曳事件,代码有点多

 public partial class Form1 : Form
 {
     
     public Form1()
     {
         InitializeComponent();

     }

     private void Form1_Load(object sender, EventArgs e)
     {
         this.label1.AllowDrop = true;//允许拖放到label上
         this.groupBox1.AllowDrop = true;

     }

     private void textBox1_MouseDown(object sender, MouseEventArgs e)
     {
         if (e.Button == MouseButtons.Left && !string.IsNullOrEmpty(textBox1.Text))
         {
             textBox1.DoDragDrop(textBox1.Text, DragDropEffects.Copy);//复制源数据
         }
     }

     private void groupBox1_DragEnter(object sender, DragEventArgs e)
     {
         if (e.Data.GetDataPresent(DataFormats.Text))
         {
             e.Effect = DragDropEffects.Copy;
         }
         else
         {
             e.Effect = DragDropEffects.None;
         }
     }

     private void groupBox1_DragDrop(object sender, DragEventArgs e)
     {
         if (e.Data.GetDataPresent(DataFormats.Text))
         {
             string text = e.Data.GetData(DataFormats.Text) as string;
             label1.Text = text;
         }
     }

     private void textBox1_TextChanged(object sender, EventArgs e)
     {

     }

     private void label1_DragDrop(object sender, DragEventArgs e)
     {
         if (e.Data.GetDataPresent(DataFormats.Text))
         {
             string text = e.Data.GetData(DataFormats.Text) as string;
             label1.Text = text;
         }
     }

     private void label1_DragEnter(object sender, DragEventArgs e)
     {
         if (e.Data.GetDataPresent(DataFormats.Text))
         {
             e.Effect = DragDropEffects.Copy;
         }
         else
         {
             e.Effect = DragDropEffects.None;
         }
     }
 }

五、结尾

以上为本人使用开发总结,如有帮助,不胜感激。

继续努力,完成整个总结,哪里需要改进,请留言说明。

如果错误,留言改正,哪有做软件没有bug的。


网站公告

今日签到

点亮在社区的每一天
去签到