如何将按钮从一个面板拖放到另一个面板?

时间:2012-07-10 05:22:22

标签: c# winforms

我有一个表格中的5个面板和两个不同面板中的两个按钮,现在要求将按钮从一个面板移动到另一个面板。

请给我一个代码。

2 个答案:

答案 0 :(得分:23)

为了演示,将两个面板放在一个面板上的一个窗体和一个按钮上:

public Form1() {
  InitializeComponent();

  panel1.AllowDrop = true;
  panel2.AllowDrop = true;

  panel1.DragEnter += panel_DragEnter;
  panel2.DragEnter += panel_DragEnter;

  panel1.DragDrop += panel_DragDrop;
  panel2.DragDrop += panel_DragDrop;

  button1.MouseDown += button1_MouseDown;
}

void button1_MouseDown(object sender, MouseEventArgs e) {
  button1.DoDragDrop(button1, DragDropEffects.Move);
}

void panel_DragEnter(object sender, DragEventArgs e) {
  e.Effect = DragDropEffects.Move;
}

void panel_DragDrop(object sender, DragEventArgs e) {
  ((Button)e.Data.GetData(typeof(Button))).Parent = (Panel)sender;
}

答案 1 :(得分:0)

此方法也适用于Group Box,但必须手动添加MouseEnter,MouseLeave和MouseUp事件:

data have;
input Product $ DMA Home_sales;
cards;
A       1      5
A       2      12
A       3      13
B       1      5
B       2      12
B       3      13
;;;

proc sql noprint;
select count(distinct product) into :num_groups from have;
quit;

%put &num_groups;

data want;
set have;
array homes(&num_groups.) ;
by product;
retain index 0;

if first.product then index+1;


*set remaining to zero;
do i=1 to dim(homes);
if i ne index then homes(i) = 0;
else homes(i) = home_sales;
end;

drop index i;
run;