Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I support drag and drop of TabPages between TabControls?

The following code snippet shows how you can drag a TabPage from TabControl1 and drop it into TabControl2 (whose AllowDrop property must be set to True):

[C#]

// Source TabControl
private void tabControl1_MouseMove( object sender, MouseEventArgs e )
{
  if ( e.Button == MouseButtons.Left )
    tabControl1.DoDragDrop( tabControl1.SelectedTab, DragDropEffects.All );
}

// Target TabControl
private void tabControl2_DragEnter( object sender, DragEventArgs e )
{
  e.Effect = e.Data.GetDataPresent( typeof( TabPage ) )
    ? DragDropEffects.Move
    : DragDropEffects.None;
}

private void tabControl2_DragDrop( object sender, DragEventArgs e )
{
  TabPage DropTab = (TabPage)( e.Data.GetData( typeof( TabPage ) ) );
  tabControl2.TabPages.Add( DropTab );
}

[Visual Basic]

' Source TabControl
Private Sub tabControl1_MouseMove(ByVal sender As Object, _
    ByVal e As MouseEventArgs)
  If e.Button = MouseButtons.Left Then
    tabControl1.DoDragDrop(tabControl1.SelectedTab, DragDropEffects.All)
  End If
End Sub

' Target TabControl
Private Sub tabControl2_DragEnter(ByVal sender As Object, _
    ByVal e As DragEventArgs)
  If e.Data.GetDataPresent(Type.GetType(TabPage)) Then
    e.Effect = DragDropEffects.Move
  Else
    e.Effect = DragDropEffects.None
  End If
End Sub

Private Sub tabControl2_DragDrop(ByVal sender As Object, _
    ByVal e As DragEventArgs)
  Dim DropTab As TabPage = _
    CType((e.Data.GetData(Type.GetType(TabPage))), TabPage)
  tabControl2.TabPages.Add(DropTab)
End Sub

Contributed from George Shepherd's Windows Forms FAQ



Page view counter