資源簡(jiǎn)介
C# 容器之間的控件拖動(dòng):
1. 從左邊容器(GroupBox)中的Lable,TextBox,拖到右邊的
GroupBox中,并可以在右邊GroupBox區(qū)域中進(jìn)行移動(dòng)該控件及變動(dòng)寬度和高度。
2.從左邊容器(GroupBox)中Button,拖到右邊的GroupBox區(qū)域后,重新創(chuàng)建一個(gè)Button,并不移動(dòng)之前的Button, 新創(chuàng)建的Button可在該區(qū)域移動(dòng)、改變寬度和高度。
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Text;
using?System.Windows.Forms;
namespace?GroupBoxMove
{
????public?partial?class?Form1?:?Form
????{
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????private?void?Form1_Load(object?sender?EventArgs?e)
????????{
????????????grbRight.AllowDrop?=?true;
????????????//CaptionHeight?=?40;
???????????
????????}
????????///?
????????///?拖動(dòng)的控件;?可在此處修改
????????///?
????????private?int?n_Type?=?0;
????????private?void?label1_MouseDown(object?sender?MouseEventArgs?e)
????????{
????????????n_Type?=?1;
????????????label1.DoDragDrop(sender?DragDropEffects.Move);
????????}
????????private?void?textBox1_MouseDown(object?sender?MouseEventArgs?e)
????????{
????????????n_Type?=?2;
????????????textBox1.DoDragDrop(sender?DragDropEffects.Move);
????????}
????????private?void?button1_MouseDown(object?sender?MouseEventArgs?e)
????????{
????????????n_Type?=?3;
????????????button1.DoDragDrop(sender?DragDropEffects.Move);
????????}
????????private?void?grbRight_DragDrop(object?sender?DragEventArgs?e)
????????{
????????????Control?col?=?(Control)sender;
????????????int?nW?=?(this.Width?-?this.ClientRectangle.Width)?/?2;
????????????Point?pC?=?new?Point(this.Location.X?+?nW?+?col.Location.X?this.Location.Y?+?col.Location.Y?+?this.Height?-?this.ClientRectangle.Height?-?nW);
????????????Point?p?=?new?Point(e.X?-?pC.X?e.Y?-?pC.Y);
????????????switch?(n_Type)
????????????{
????????????????case?1:
????????????????????grbLeft.Controls.Remove(label1);
????????????????????grbRight.Controls.Add(label1);
????????????????????label1.MouseDown?-=?new?MouseEventHandler(label1_MouseDown);
????????????????????label1.Location?=?p;
????????????????????MoveControl?label?=?new?MoveControl(label1);
????????????????????break;
????????????????case?2:
????????????????????grbLeft.Controls.Remove(textBox1);
????????????????????grbRight.Controls.Add(textBox1);
????????????????????textBox1.Location?=?p;
????????????????????textBox1.MouseDown?-=?new?MouseEventHandler(textBox1_MouseDown);
????????????????????MoveControl?textBox?=?new?MoveControl(textBox1);
????????????????????break;
????????????????case?3:
????????????????????Button?btn?=?new?Button();
????????????????????btn.Text?=?“New“;
????????????????????grbRight.Controls.Add(btn);
????????????????????btn.Location?=?p;
????????????????????MoveControl?button?=?new?MoveControl(btn);
????????????????????break;
????????????}
????????}
????????private?void?grbRight_DragEnter(object?sender?DragEventArgs?e)
????????{
????????????e.Effect?=?DragDropEffects.Move;
????????}
????}
}
評(píng)論
共有 條評(píng)論