【问题标题】:How to Drag and Drop(copy) from one panel to another panel如何从一个面板拖放(复制)到另一个面板
【发布时间】:2013-04-23 09:10:31
【问题描述】:

我在一个表单上有两个面板。
一个面板有一些控件,如按钮或图像,第二个面板是空的。我想将控件从面板 1 拖放到面板 2,但它应该创建控件的副本,并且在拖动矩形时应该显示与控件相同的大小,并且当拖放到面板 2 中时,拖动的形状应该出现在那里在鼠标位置
实际上我想创建一个类似模拟器的东西。面板 1 中有一些工具,当有人在面板 2 上拖放工具时,它应该出现在鼠标位置。

语言无关紧要可能是C#VB.NET

【问题讨论】:

  • 请显示一些你到目前为止所做的代码
  • 告诉我们你做了什么,什么没用。 Internet上有很多用于拖放的资源。你搜索了什么?

标签: c# vb.net drag-and-drop


【解决方案1】:

你尝试过这样的事情吗?

private void Form5_Load(object sender, EventArgs e)  
{  
    this.panel1.AllowDrop = true;  
    foreach (Control c in this.panel1.Controls)  
    {  
        c.MouseDown += new MouseEventHandler(c_MouseDown);  
    }  
    this.panel1.DragOver += new DragEventHandler(panel1_DragOver);  
    this.panel1.DragDrop += new DragEventHandler(panel1_DragDrop);  
}  

void c_MouseDown(object sender, MouseEventArgs e)  
{  
    Control c = sender as Control;  
    c.DoDragDrop(c, DragDropEffects.Move);  
}  

void panel1_DragDrop(object sender, DragEventArgs e)  
{  
    Control c = e.Data.GetData(e.Data.GetFormats()[0]) as Control;  
    if (c != null)  
    {  
        c.Location = this.panel1.PointToClient(new Point(e.X, e.Y));  
        this.panel1.Controls.Add(c);  
    }  
}  

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

VB.NET

Private Sub Form5_Load(sender As Object, e As EventArgs)
    Me.panel1.AllowDrop = True
    For Each c As Control In Me.panel1.Controls
        c.MouseDown += New MouseEventHandler(AddressOf c_MouseDown)
    Next
    Me.panel1.DragOver += New DragEventHandler(AddressOf panel1_DragOver)
    Me.panel1.DragDrop += New DragEventHandler(AddressOf panel1_DragDrop)
End Sub

Private Sub c_MouseDown(sender As Object, e As MouseEventArgs)
    Dim c As Control = TryCast(sender, Control)
    c.DoDragDrop(c, DragDropEffects.Move)
End Sub

Private Sub panel1_DragDrop(sender As Object, e As DragEventArgs)
    Dim c As Control = TryCast(e.Data.GetData(e.Data.GetFormats()(0)), Control)
    If c IsNot Nothing Then
        c.Location = Me.panel1.PointToClient(New Point(e.X, e.Y))
        Me.panel1.Controls.Add(c)
    End If
End Sub

Private Sub panel1_DragOver(sender As Object, e As DragEventArgs)
    e.Effect = DragDropEffects.Move
End Sub

Source

【讨论】:

    【解决方案2】:

    我正在更改@Shim 的一些代码。 这是更新后的代码,您的控件副本将放置在另一个面板中

    Random rnd = new Random();
    
    private void Form5_Load(object sender, EventArgs e)  
    {  
        this.panel1.AllowDrop = true;  
        foreach (Control c in this.panel1.Controls)  
        {  
            c.MouseDown += new MouseEventHandler(c_MouseDown);  
        }  
        this.panel1.DragOver += new DragEventHandler(panel1_DragOver);  
        this.panel1.DragDrop += new DragEventHandler(panel1_DragDrop);  
    }  
    
    void c_MouseDown(object sender, MouseEventArgs e)  
    {  
        Control c = sender as Control;  
        c.DoDragDrop(c, DragDropEffects.Move);  
    }  
    
    void panel1_DragDrop(object sender, DragEventArgs e)  
    {  
        Control c = e.Data.GetData(e.Data.GetFormats()[0]) as Control;
        // Here, you get a copy of your drag drop button and dynamically new button is created  
        Button btn = new Button();
        btn.Name = "Button" + rnd.Next();
        btn.Size = c.Size;
        if (c != null)  
        {  
            // Add the newly created button to you Panel
            btn.Location = this.panel1.PointToClient(new Point(e.X, e.Y));  
            this.panel1.Controls.Add(btn);  
        }  
    }  
    
    void panel1_DragOver(object sender, DragEventArgs e)  
    {  
        e.Effect = DragDropEffects.Move;  
    }
    

    【讨论】:

      【解决方案3】:

      此解决方案将在移动鼠标时拖动按钮(或选择的任何其他组件)并将其放置在您放置它的位置

      private SimpleButton selectedButton;
      
          public Form1()
          {
              InitializeComponent();
          }
      
          private void Form1_Load(object sender, EventArgs e)
          {
              xtraScrollableControl2.AllowDrop = true;
              xtraScrollableControl2.DragEnter += XtraScrollableControl_DragEnter;
              xtraScrollableControl2.DragDrop += XtraScrollableControl_DragDrop;
              xtraScrollableControl2.DragOver += XtraScrollableControl_DragOver;
          }
      
          private void XtraScrollableControl_DragEnter(object sender, DragEventArgs e)
          {
              e.Effect = e.Data.GetDataPresent(typeof(Bitmap)) ? DragDropEffects.Copy : DragDropEffects.None;
          }
      
          private void XtraScrollableControl_DragDrop(object sender, DragEventArgs e)
          {
      
              var simpleButton = e.Data.GetData(e.Data.GetFormats()[0]) as SimpleButton;
              if (simpleButton == null) return;
              if (simpleButton.Parent != sender) 
              {
                  var btn = new SimpleButton
                  {
                      Dock = DockStyle.None,
                      Size = new Size(125, 50),
                      Text = simpleButton.Text,
                      Location = ((XtraScrollableControl) sender).PointToClient(new Point(e.X, e.Y)),
                      ImageList = simpleButton.ImageList, 
                      ImageIndex = simpleButton.ImageIndex,
                      ImageLocation = simpleButton.ImageLocation,
                      Parent = ((XtraScrollableControl)sender)
                  };
                  btn.MouseDown += simpleButton_MouseDown;
      
                  ((XtraScrollableControl)sender).Controls.Add(btn);
              }
              else
              {
                  ((XtraScrollableControl)sender).Controls.Remove(simpleButton);
                  simpleButton.Location = ((XtraScrollableControl)sender).PointToClient(new Point(e.X, e.Y));
                  ((XtraScrollableControl)sender).Controls.Add(simpleButton);
              }
      
              selectedButton = null;
          }
      
          private void XtraScrollableControl_DragOver(object sender, DragEventArgs e)
          {
              e.Effect = DragDropEffects.Copy;
              selectedButton.Location = ((XtraScrollableControl)sender).PointToClient(new Point(e.X, e.Y));
          }
      
          private void simpleButton_MouseDown(object sender, MouseEventArgs e)
          {
              var btn = sender as SimpleButton;
              if (btn == null) return;
              selectedButton = btn;
              btn.DoDragDrop(btn, DragDropEffects.Copy);
          }
      

      希望对大家有所帮助

      我使用了 DevExpress 组件 但对于标准

      开发快递 XtraScrollableControl 简单按钮 微软 控制板 按钮

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-06-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多