在两个不同的表单实例之间拖动n

在两个不同的表单实例之间拖动n

本文介绍了在两个不同的表单实例之间拖动n Drop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个C#winForm项目,该项目有一个带有空tabControl的winForm,它可以用于拖放&下降。我还有一个带有dataGridView的winForm实例,我想将数据从我的winForm tabControl拖动到生成tabPages ...由于某种原因,我似乎无法弄清楚这是不能使用我的winForm dataGridView但我可以拖动从另一个应用程序中删除。



似乎无法在此问题上找到任何内容。有人可以帮忙吗?提前致谢。以下是我的代码示例:



I'm working on a C# winForm project that has a winForm with an empty tabControl that is enabled for drag & drop. I also have another winForm instance with a dataGridView that I want to drag data from onto my winForm tabControl and generate tabPages...for some reason that I can't seem to figure out this is not working with my winForm dataGridView BUT I can drag and drop from another application.

Can't seem to find anything on this issue. Can someone plz help? Thanks in advance. Here's an example of my code:

// Form 1
class Form1 : Form
{
   void Form1()
   {
       InitializeComponent();

       tabControl1.TabPages.Clear();
       tabControl1.AllowDrop = true;
       tabControl1.DrageEnter += tabCtrl_DrageEnter;
       tabControl1.DragDrop += tabCtrl_DropDrop;
   }

   void tabCtrl_DragEnter(object sender, EventArgs e)
   {
      e.Effect = DragDropEffects.Copy;
   }

   void tabCtrl_DragDrop(object sender, DragEventArgs e)
   {
      string data = (string)e.Data.GetData(typeof(string));

      if (tabControl1.Controls.ContainsKey(data) == false)
      {
         TabPage tp = new TabPage();
         tp.Name = data;
         tp.Text = data;
         DataGridView dgv = new DataGridView();
         dgv.Dock = DockStyle.Fill;
         tp.Controls.Add(dgv);
         tabControl1.Controls.Add(tp);
       }
     }

     private void button1_Click_1(object sender, EventArgs e)
     {
         Form2 frm2 = new Form2();
         frm2.Show();
     }

}
// Form 2
class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();

         dataGridView1.Columns.Add("colID", "ID");
         dataGridView1.Columns.Add("colProd", "Prod");
         dataGridView1.Rows.Add("0", "prod0");
         dataGridView1.Rows.Add("1", "prod1");
         dataGridView1.Rows.Add("2", "prod2")
         dataGridView1.Rows.Add("3", "prod3");

         dataGridView1.MouseDown += new MouseEventHandler(grid_MouseDown);
     }

     private void grid_MouseDown(object sender, MouseEventArgs e)
     {
         DataGridView.HitTestInfo info = grid.HitTest(e.X, e.Y);

         if (e.Button == MouseButtons.Left && info.RowIndex >= 0 && info.ColumnIndex == 1)
         {
              string prod = (string)dataGridView1.Rows[info.RowIndex].Cells["colProd"].Value;

              dataGridView1.DoDragDrop(prod, DragDropEffects.Copy);
          }
      }
 }

推荐答案

void Form1()
{
    InitializeComponent();

    tabControl1.TabPages.Clear();
    tabControl1.AllowDrop = true;
    tablControl1.DragEnter += tabCtrl_DragEnter;
    tab1Control1.DragDrop += tabCtrl_DragDrop;
}

void tabCtrl_DragEnter(object sender, EventArgs e)
{
   // ...
}

void tabCtrl_DragDrop(object sender, DragEventArgs e)
{
   // ...
}





也许您应该检查事件处理程序(例如,输入 tabCtrl_DragEnter 时,在控制台中写入)这样你就可以看到事件是否在需要时被触发。这可以帮助您更准确地找到问题。



Maybe you should check the event handlers (for example, when entering tabCtrl_DragEnter, write it in the console) so that you'll see if events are fired when needed. This could help you to find more precisely the problem.


dataGridView1.DoDragDrop(prod, DragDropEffects.Copy);

这次调用是否被调用?



如果没有,请检查 grid_MouseDown()方法中的错误 if -condition。

Does this call get called?

If not, check the grid_MouseDown() method for errors in the if-condition.


public partial class CustTabControl : TabControl
{
    const int WM_NCHITTEST = 0x0084;
    const int HTTRANSPARENT = -1;
    const int HTCLIENT = 1;

    public CustTabControl()
        :base()
    {
        InitializeComponent();
    }

    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        if (m.Msg == WM_NCHITTEST)
        {
            if (m.Result.ToInt32() == HTTRANSPARENT)
                m.Result = new IntPtr(HTCLIENT);
        }
    }
}





这里是链接: [ ]


这篇关于在两个不同的表单实例之间拖动n Drop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 01:08