[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 458
  • Last Modified:

Tabcontainer - user ordered tabs

I would like to be able to allow my users to re-order the sequence of the tabs in a tab container.

How do I do that?
0
rmmarsh
Asked:
rmmarsh
  • 9
  • 7
1 Solution
 
Miguel OzSoftware EngineerCommented:
You need to build a dialog (listbox and up/down/OK/Cancel buttons) showing the current tab order and then allow the user to change the order.  When user clicks OK you take the current order in the list box and change it in the tabcontrol.tabpages property
For code:
http://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.tabpages.aspx
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_20984358.html
0
 
rmmarshAuthor Commented:
This is for a 'tabcontainer', not a tab-control... just like you can do in Visual Studio... you can move the tabs  within the tabcontainer so you control the order of the tabs)

I hope that's clearer... kind of hard to explain...
0
 
rmmarshAuthor Commented:
Ignore my last post... I see tab-control is listed under 'containers'.. Duh!


0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
rmmarshAuthor Commented:
How do I allow the user to 'drag' the tab to where he want's it?  Just like in Visual Studio, you can re-organize the sequence of the tabs depending on your preference.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I'm using Visual Studio 2008 and I can't reorder my tabs in a TabControl via DRAGGING.

What version are you using?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here's some code that allows you to drag the TabPages to reorder them:
(hopefully I haven't missed some easy to set Property of the TabControl!)
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;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            tabControl1.MouseDown +=new MouseEventHandler(tabControl1_MouseDown);
            tabControl1.MouseMove +=new MouseEventHandler(tabControl1_MouseMove);
        }

        private TabPage SourceTab = null;

        private void tabControl1_MouseDown(object sender, MouseEventArgs e)
        {
 	        if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                int DragIndex = TabFromPoint();
                if (DragIndex != -1)
                {
                    SourceTab = tabControl1.TabPages[DragIndex];
                }
                else
                {
                    SourceTab = null;
                }
            }
        }

        private void  tabControl1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                int OverIndex = TabFromPoint();
                if (OverIndex != -1)
                {
                    TabPage overTab = tabControl1.TabPages[OverIndex];
                    if (overTab != SourceTab)
                    {
                        if (OverIndex < tabControl1.TabPages.IndexOf(SourceTab))
                        {
                            tabControl1.TabPages.Remove(SourceTab);
                            tabControl1.TabPages.Insert(tabControl1.TabPages.IndexOf(overTab), SourceTab);
                            tabControl1.SelectedTab = SourceTab;
                        }
                        else
                        {
                            tabControl1.TabPages.Remove(SourceTab);
                            if (OverIndex < tabControl1.TabCount - 1)
                            {
                                tabControl1.TabPages.Insert(OverIndex + 1, SourceTab);
                            }
                            else
                            {
                                tabControl1.TabPages.Add(SourceTab);
                            }
                            tabControl1.SelectedTab = SourceTab;
                        }
                    }
                }
            }
        }
    
        private int TabFromPoint()
        {
            Point pt = tabControl1.PointToClient(Cursor.Position);
            for (int i = 0; i < tabControl1.TabCount; i++)
            {
                if (tabControl1.GetTabRect(i).Contains(pt))
                {
                    return i;
                }
            }
            return -1;
        }

    }
}

Open in new window

0
 
rmmarshAuthor Commented:
Idle_Mind:  I'm using VS 2008 Professional; and even with VS Express editions, I can move the tabs by dragging to the left or the right...

Thanks for the code; I'll give it a try and get back to you...
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I'm using Visual Studio 2008 Standard...still can't get the Tabs to drag without the code above.

How are you dragging them?  Did you change any properties?  Do you have to hold a special key combination?

I just dropped a TabControl onto the form and added two more TabPages.  Then I left click on the Tabs and try to drag but nothing happens.

???
0
 
rmmarshAuthor Commented:
I think we're talking about two different things... I'm talking about the tabs in Visual Studio itself... when you open several .cs files, you can drag the tabs for each of the files and change the order...  let me know if you need an image of what I'm talking about...
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Ok...gotcha.  Yes, my code will allow the user to move the TabPages in a TabControl just like you can move the Tabs for the Code Files in Visual Studio.
0
 
rmmarshAuthor Commented:
Idle_Mind:  doesn't work!  When I move the mouse over the tab, it fires the "mouseMove" event, and since the left mouse button is not pressed at that time, nothing happens...
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Well...yeah...LEFT CLICK the Tab and DRAG it to move it.

Don't think you want the Tabs just re-ordering themselves whenever the user moves the mouse over them...  =\
0
 
rmmarshAuthor Commented:
I do LEFT-CLICK... nothing happens!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Ok...  =\

I assume you have a TabControl (tabControl1) on the Form with Tabs in it?

Did you see that I wired up the TabControl's MouseDown() and MouseMove() events in the Load() event of the Form?

        public Form1()
        {
            InitializeComponent();

            tabControl1.MouseDown +=new MouseEventHandler(tabControl1_MouseDown);
            tabControl1.MouseMove +=new MouseEventHandler(tabControl1_MouseMove);
        }
0
 
rmmarshAuthor Commented:
I think I see the problem... different names for the tab control... let me change it and I'll let you know...
0
 
rmmarshAuthor Commented:
Wheeeeeeeeee!  that was it!

Thank you so much for the code... I really appreciate it!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Lol...glad we finally got it all sorted out there...  =)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 9
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now