Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
890
Rename a Tab?
posted

Hi All,

Is it possible for a user to rename a tab?  I know I can do it programmatically using Tab.text, but I'm looking for a way of the existing tab text getting selected/highlighted, and then the user being able to type a new name over the top. 

Just like in MS Excel, when the user clicks on a sheet tab and chooses "Rename".

Is this acheivable, and if not, what is a good alternative for handling user tab renaming?

Any info appreciated,

Scott Pearce

Parents
  • 410
    posted

    Here is how I roll this smoke...Merry Christmas....  

    Here is the Tab MouseUp Event that gets the ball rolling...dont forget to use the text editors lost focus to hide itself when its all said and done...

    It goes like this: Extract the tab, map the tab, create some geometric info, assign that info to the tab, pull up the context menu, assign the tab to the tools in the pop menu, use the geometric info to pop up the menu, then respond to the tool click and extract the prior assignments first from the tool, and then its tag (the tab)...to place your edit box

    This works like a charm...Cheers

    1.           
    2. private void tabAddress_MouseUp(object sender, MouseEventArgs e)  
    3. {  
    4.     if (e.Button != MouseButtons.Right) return;  
    5.       
    6.     // a couple of points to work with  
    7.     Point p1 = e.Location;  
    8.     Point p2 = e.Location;  
    9.       
    10.     // get your tab object  
    11.     UltraTab tab = tabAddress.TabFromPoint(p1);              
    12.       
    13.     if (tab != null && tab.Key != "Add")  
    14.     {                  
    15.           
    16.         // These while loops will define the   
    17.         // footprint of the active tab that   
    18.         // sent the mouse up event......  
    19.         // we are basically manually hit testing  
    20.         // using the TabFromPoint() method  
    21.         // the offset after each loop does two things
    22.         // 1) scoots the point back onto the tab so the next loop hits
    23.         // 2) gives you a place to tweak the position and size of the text editor
    24.         // think of it like cell interior padding
    25.         // ======================================  
    26.           
    27.         // find left X  
    28.         while (tabAddress.TabFromPoint(p1) == tab)  
    29.         {  
    30.             p1.Offset(-1, 0);   
    31.         }  
    32.         p1.Offset(2, 0); // pad back a few pixels  
    33.           
    34.         // find top Y  
    35.         while (tabAddress.TabFromPoint(p1) == tab)  
    36.         {  
    37.             p1.Offset(0, -1);  
    38.         }  
    39.         p1.Offset(0, 1); // pad back a few pixels  
    40.   
    41.         // find right X  
    42.         while (tabAddress.TabFromPoint(p2) == tab)  
    43.         {  
    44.             p2.Offset(1, 0);  
    45.         }  
    46.         p2.Offset(-2, 0); // pad back a few pixels  
    47.   
    48.         // find bottom Y  
    49.         while (tabAddress.TabFromPoint(p2) == tab)  
    50.         {  
    51.             p2.Offset(0, 1);  
    52.         }  
    53.         p2.Offset(0, -3); // pad back a few pixels                  
    54.         // ========================================  
    55.         // end the while loops       
    56.           
    57.         // offset p2 by negative p1 to create a size point  
    58.         p2.Offset(-p1.X, -p1.Y);                  
    59.           
    60.         // translate p1 into a usable location  
    61.         // for the context menu  
    62.         p1 = tabAddress.PointToScreen(p1);  
    63.           
    64.         // create a new size with p2  
    65.         Size size = new Size(p2);  
    66.           
    67.         // we are done with p2 as a size point  
    68.         // but we will reuse it pinpoint where  
    69.         // we will position or tab's text editor  
    70.         // that we have hidden....  
    71.   
    72.         // call the texbox parent and translate p1  
    73.         // into a new p2 that the textbox can be   
    74.         // positioned with  
    75.         p2 = txtTabRename.Parent.PointToClient(p1);  
    76.           
    77.         // create a new rectangle object using p2 and size  
    78.         // we will attach this to the tab's   
    79.         // tag property so we can pull it out later  
    80.         tab.Tag = new Rectangle(p2, size);  
    81.   
    82.         // get the pop up menu tool that is the tab context menu  
    83.         PopupMenuTool popup = tbm.Tools["ContextTab"]as PopupMenuTool;  
    84.               
    85.         // loop through the popup tools collection  
    86.         foreach (ToolBase tool in popup.Tools)  
    87.         {  
    88.             // assign the tool Tag the Tab object  
    89.                 tool.Tag = tab;  
    90.         }  
    91.         // show the context menu just below the tab  
    92.         tbm.ShowPopup("ContextTab",new Point(p1.X, p1.Y + size.Height));  
    93.               
    94.         // Everything we need is now available in ToolClick event tool object  
    95.         // No class wide variable required....  
    96.     }  
    97. }  
    98.         

    Here is the Tool Click Event...notice how we query the Tool.Tag to see if is packing a Tab, then check to see if the Tab is packing a Rectangle...we just burrow into the tool and query whats it packing to decide what to do...and in this case we display the Tab Rename edit box on target, on time....in fact parsing the Tool.Tag for its type is a slick way to handle click events...it lets you sift out the ordinary from the extraordinary in one step...

    I constantly leverage the Tag property like this to extend functionality without bogging a GUI code set down with class wide variables...and other such nonsense...such as unecessary inheritence...

    There is no end to how sophisticated you can get with this, as you can create custom objects, and type check against custom interfaces and pack it into a single Tag property if required..there are tons of solutions to common problems using this method...

    Now thats what I call a Tag Team... 

    1. private void tbm_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)  
    2. {              
    3.     switch (e.Tool.Key)  
    4.     {  
    5.         case "Rename":  
    6.   
    7.             // is this tool packing an UltraTab with it?  
    8.             if (e.Tool.Tag is UltraTab)  
    9.             {  
    10.                 // get our tab object  
    11.                 UltraTab tab = (UltraTab)e.Tool.Tag;  
    12.   
    13.                 // is this tab packing a Rectangle with it?  
    14.                 if (tab.Tag is Rectangle)  
    15.                 {  
    16.                     // set the tag of the text editor  
    17.                     // to the tab so we can update  
    18.                     // the tab text from the text  
    19.                     // editor events ....if the text editor is pack the tab as a tag...easy tab text update...
    20.                     txtTabRename.Tag = tab;  
    21.   
    22.                     // get the Rectangle  
    23.                     Rectangle rect = (Rectangle)tab.Tag;  
    24.   
    25.                     // set the location and size  
    26.                     txtTabRename.Location = rect.Location;  
    27.                     txtTabRename.Size = rect.Size;  
    28.   
    29.                     // get the tab text  
    30.                     txtTabRename.Text = tab.Text;  
    31.   
    32.                     // show it, focus it, select all text   
    33.                     txtTabRename.Visible = true;  
    34.                     txtTabRename.Focus();  
    35.                     txtTabRename.SelectAll();  
    36.                 }  
    37.             }  
    38.             break;  
    39.     }  
    40. }  
Reply Children
No Data