Avatar of AbeHoffman
AbeHoffman
 asked on

Drag and Drop Tab Off a Form .Net

I have several tabs in an application which are dynamically generated. I would like to add the ability to drag-off a tab and have it become it's own window. I'm trying to mimic the drag-out behavior as seen in the Chrome browser or the new Safari beta browser. I have figured out how to drag and drop a tab from one form to another, but still haven't figured out how to drag and drop to where a form doesn't exist.

Thanks!
C#.NET ProgrammingVisual Basic.NET

Avatar of undefined
Last Comment
Nikkoli

8/22/2022 - Mon
Dmitry G

I'm afraid you can't get this functionality using C#.
ASKER CERTIFIED SOLUTION
Mike Tomlinson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
AbeHoffman

ASKER
That's more than enough to get me started! Thanks!
Nikkoli

Idle_Mind's code in C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
 
namespace TabPopBrowser
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            tabControl1.AllowDrop = true;
        }
 
        private void TabControl1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                for (int i = 0; i < tabControl1.TabCount; i++)
                {
                    Rectangle rc = tabControl1.GetTabRect(i);
                    if (rc.Contains(e.X, e.Y))
                    {
                        tabControl1.DoDragDrop(new DataObject("TabPage", tabControl1.TabPages[i]), DragDropEffects.All);
                        break;
                    }
                }
            }
        }
 
        private void TabControl1_DragLeave(object sender, EventArgs e)
        {
            DropForm frm = new DropForm();
            frm.Location = Cursor.Position;
            frm.Show();
        }
 
        private class DropForm : Form
        {
 
            private const int HTCAPTION = 2;
            private const int WM_NCLBUTTONDOWN = 161;
 
            [DllImport("User32.dll")]
            public static extern int ReleaseCapture();
            [DllImport("User32.dll")]
            private static extern int SendMessage(IntPtr handle, int wMsg, int wParam, int lParam);
 
            public DropForm()
            {
                this.AllowDrop = true;
                this.StartPosition = FormStartPosition.Manual;
                this.Shown += new EventHandler(DropForm_Shown);
                this.DragOver += new DragEventHandler(DropForm_DragOver);
                this.DragDrop += new DragEventHandler(DropForm_DragDrop);
                this.DragLeave += new EventHandler(DropForm_DragLeave);
            }
 
            private void DropForm_Shown(object sender, EventArgs e)
            {
                Cursor.Position = new Point(this.Location.X + this.Width / 2, this.Location.Y + this.Height / 2);
            }
 
            private void DropForm_DragOver(object sender, DragEventArgs e)
            {
                e.Effect = (e.Data.GetDataPresent("TabPage")) ? DragDropEffects.All : DragDropEffects.None;
                ReleaseCapture();
                SendMessage(this.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
                Cursor.Position = new Point(this.Location.X + this.Width / 2, this.Location.Y + this.Height / 2);
            }
 
            private void DropForm_DragDrop(object sender, DragEventArgs e)
            {
                this.AllowDrop = false;
 
                TabControl tc = new TabControl();
                tc.TabPages.Clear();
                tc.TabPages.Add((TabPage)e.Data.GetData("TabPage"));
                tc.Dock = DockStyle.Fill;
                this.Controls.Add(tc);
                this.BringToFront();
            }
 
            private void DropForm_DragLeave(object sender, EventArgs e)
            {
                this.Close();
            }
        }
    }
}

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23