Click here to Skip to main content
15,900,390 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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);
          }
      }
 }
Posted
Updated 5-Jun-13 2:17am
v5

According to the names of the event handlers, shouldn't that be...

C#
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)
{
   // ...
}


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.
 
Share this answer
 
Comments
d.allen101 5-Jun-13 6:51am    
No it doesn't fire if the dragDrop operation is from the Form2 instance but it does fire if the dragDrop operation is from an outside application. That's what I'm trying to figure out. It doesn't make sense.
Place a breakpoint on
C#
dataGridView1.DoDragDrop(prod, DragDropEffects.Copy);
Does this call get called?

If not, check the grid_MouseDown() method for errors in the if-condition.
 
Share this answer
 
Comments
d.allen101 5-Jun-13 8:46am    
There aren't any errors and yes dataGridView1.DoDragDrop fires correctly. This is unbelievable! It works if I'm dragDrop operation is from another application or after a tabPage has been added.
I have to override default behavior of tabControl:

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);
        }
    }
}


here's the link: http://stackoverflow.com/questions/5071033/winforms-tabcontrol-drag-drop-problem[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900