| | | tabbing across multiple forms | | |
hi, I was wondering how to tab across multiple controls on a windows form. I have a form with multiple controls and I need to set the tab order, but the controls are coming from different .cs files and I can't figure out how to set them so they all work together.
chuckdawit- Moved byTaylorMichaelLMVPTuesday, September 22, 2009 1:03 AMWinforms related (From:Visual C# General)
-
| | chuckdawit Monday, September 21, 2009 9:25 PM | Hi chuckdawit,
I have developed my own custom class , for sitting the Tab order dynamically on a container that contains control. The class contains an enumeration called 'TabScheme' that you can set the way in which fashion you want to set the Tab order like Up down scheme and across scheme.
The class that i am using it may helpful for you , after loading the control you call the TabManger to set the Tab dynamically.
////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics;
namespace BDSUtilityService
{
/// <summary>
/// Dynamically determine and set a tab order for a container and children according to a given strategy.
/// </summary>
public class TabOrderManager
{
/// <summary>
/// Compare two controls in the selected tab scheme.
/// </summary>
private class TabSchemeComparer : IComparer
{
private TabScheme comparisonScheme;
#region IComparer Members
public int Compare(object x, object y)
{
Control control1 = x as Control;
Control control2 = y as Control;
if( control1 == null || control2 == null )
{
Debug.Assert( false, "Attempting to compare a non-control" );
return 0;
}
if( comparisonScheme == TabScheme.None )
{
// Nothing to do.
return 0;
}
if( comparisonScheme == TabScheme.AcrossFirst )
{
// The primary direction to sort is the y direction (using the Top property).
// If two controls have the same y coordination, then we sort them by their x's.
if( control1.Top < control2.Top )
{
return -1;
}
else if( control1.Top > control2.Top )
{
return 1;
}
else
{
return ( control1.Left.CompareTo( control2.Left) );
}
}
else // comparisonScheme = TabScheme.DownFirst
{
// The primary direction to sort is the x direction (using the Left property).
// If two controls have the same x coordination, then we sort them by their y's.
if( control1.Left < control2.Left )
{
return -1;
}
else if( control1.Left > control2.Left )
{
return 1;
}
else
{
return (control1.Top.CompareTo( control2.Top ));
}
}
}
#endregion
/// <summary>
/// Create a tab scheme comparer that compares using the given scheme.
/// </summary>
/// <param name="scheme"></param>
public TabSchemeComparer( TabScheme scheme )
{
comparisonScheme = scheme;
}
}
/// <summary>
/// The container whose tab order we manage.
/// </summary>
private Control container;
/// <summary>
/// Hash of controls to schemes so that individual containers can have different ordering
/// strategies than their parents.
/// </summary>
private Hashtable schemeOverrides;
/// <summary>
/// The tab index we start numbering from when the tab order is applied.
/// </summary>
private int curTabIndex = 0;
/// <summary>
/// The general tab-ordering strategy (i.e. whether we tab across rows first, or down columns).
/// </summary>
public enum TabScheme
{
None,
AcrossFirst,
DownFirst
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="container">The container whose tab order we manage.</param>
public TabOrderManager(Control container)
{
this.container = container;
this.curTabIndex = 0;
this.schemeOverrides = new Hashtable();
}
/// <summary>
/// Construct a tab order manager that starts numbering at the given tab index.
/// </summary>
/// <param name="container">The container whose tab order we manage.</param>
/// <param name="curTabIndex">Where to start numbering.</param>
/// <param name="schemeOverrides">List of controls with explicitly defined schemes.</param>
private TabOrderManager(Control container, int curTabIndex, Hashtable schemeOverrides )
{
this.container = container;
this.curTabIndex = curTabIndex;
this.schemeOverrides = schemeOverrides;
}
/// <summary>
/// Explicitly set a tab order scheme for a given (presumably container) control.
/// </summary>
/// <param name="c">The control to set the scheme for.</param>
/// <param name="scheme">The requested scheme.</param>
public void SetSchemeForControl( Control c, TabScheme scheme )
{
schemeOverrides[c] = scheme;
}
/// <summary>
/// Recursively set the tab order on this container and all of its children.
/// </summary>
/// <param name="scheme">The tab ordering strategy to apply.</param>
/// <returns>The next tab index to be used.</returns>
public int SetTabOrder( TabScheme scheme )
{
// Tab order isn't important enough to ever cause a crash, so replace any exceptions
// with assertions.
try
{
ArrayList controlArraySorted = new ArrayList();
controlArraySorted.AddRange( container.Controls );
controlArraySorted.Sort( new TabSchemeComparer( scheme ) );
foreach( Control c in controlArraySorted )
{
Debug.WriteLine( "TabOrderManager: Changing tab index for " + c.Name );
c.TabIndex = curTabIndex++;
if( c.Controls.Count > 0 )
{
// Control has children -- recurse.
TabScheme childScheme = scheme;
if( schemeOverrides.Contains( c ) )
{
childScheme = (TabScheme) schemeOverrides[c];
}
curTabIndex = (new TabOrderManager( c, curTabIndex, schemeOverrides )).SetTabOrder( childScheme );
}
}
return curTabIndex;
}
catch( Exception e )
{
Debug.Assert( false, "Exception in TabOrderManager.SetTabOrder: " + e.Message );
return 0;
}
}
}
}
After Loading the controls use following code to set the Tab order for e.g on load you can use the code.
TabOrderManager.TabScheme scheme = TabOrderManager.TabScheme.AcrossFirst;
TabOrderManager tom = new TabOrderManager(this); // Pass the container object
tom.SetTabOrder(scheme);
- Marked As Answer bychuckdawit Wednesday, October 07, 2009 6:01 PM
-
| | Malik M.Shahid Wednesday, October 07, 2009 8:26 AM | Hello, Set the TabIndex property of each control on a form. Make sure each control has a unique integer value. If you are passing controls around as parameters, cease and desist. Stop doing it. As your code grows, maintaining it will become a nightmare. One small change here will ripple across to several .cs files. You should use events to pass data between forms. Walkthrough: Passing Data Between Windows Forms You can set the TabIndex property of each control on each form to set the Tab order. If you pass around references to the controls, and change the TabIndex property for each form when it gets focus is begging for trouble. Hope this helps. Rudedog =8^D
Mark the best replies as answers. "Fooling computers since 1971." | | Rudedog2 Tuesday, September 22, 2009 12:11 AM | Hi chuckdawit,
> I have a form with multiple controls and I need to set the tab order, but the controls are coming from different .cs files and I can't figure out how to set them so they all work together.
It doesn’t matter where these controls are coming from. Even if they are defined in another assembly, you can also set their order in your form. Select each control on the form one by one and set its TabIndex property can build their order.
TabIndex property is for ordering controls on a form. You can set it in the PropertyGrid of Visual Studio. Some controls such as Panel, PictureBox etc. cannot have focus. But they also have TabIndex property. Even if you have set its TabIndex, it will be ignored when you press TAB key.
If you have any doubt, please feel free to tell me.
Sincerely,
Kira Qian
Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! | | Kira Qian Tuesday, September 22, 2009 6:18 AM | Hi chuckdawit, I am writing to check the status of the issue on your side. Could you please let me know if the suggestion works for you? If you have any questions or concerns, please feel free to let me know. I will be more than happy to be of assistance. Sincerely, Kira Qian Send us any feedback you have about the help from MSFT at fbmsdn[At]microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! | | Kira Qian Wednesday, October 07, 2009 7:14 AM | Hi chuckdawit,
I have developed my own custom class , for sitting the Tab order dynamically on a container that contains control. The class contains an enumeration called 'TabScheme' that you can set the way in which fashion you want to set the Tab order like Up down scheme and across scheme.
The class that i am using it may helpful for you , after loading the control you call the TabManger to set the Tab dynamically.
////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics;
namespace BDSUtilityService
{
/// <summary>
/// Dynamically determine and set a tab order for a container and children according to a given strategy.
/// </summary>
public class TabOrderManager
{
/// <summary>
/// Compare two controls in the selected tab scheme.
/// </summary>
private class TabSchemeComparer : IComparer
{
private TabScheme comparisonScheme;
#region IComparer Members
public int Compare(object x, object y)
{
Control control1 = x as Control;
Control control2 = y as Control;
if( control1 == null || control2 == null )
{
Debug.Assert( false, "Attempting to compare a non-control" );
return 0;
}
if( comparisonScheme == TabScheme.None )
{
// Nothing to do.
return 0;
}
if( comparisonScheme == TabScheme.AcrossFirst )
{
// The primary direction to sort is the y direction (using the Top property).
// If two controls have the same y coordination, then we sort them by their x's.
if( control1.Top < control2.Top )
{
return -1;
}
else if( control1.Top > control2.Top )
{
return 1;
}
else
{
return ( control1.Left.CompareTo( control2.Left) );
}
}
else // comparisonScheme = TabScheme.DownFirst
{
// The primary direction to sort is the x direction (using the Left property).
// If two controls have the same x coordination, then we sort them by their y's.
if( control1.Left < control2.Left )
{
return -1;
}
else if( control1.Left > control2.Left )
{
return 1;
}
else
{
return (control1.Top.CompareTo( control2.Top ));
}
}
}
#endregion
/// <summary>
/// Create a tab scheme comparer that compares using the given scheme.
/// </summary>
/// <param name="scheme"></param>
public TabSchemeComparer( TabScheme scheme )
{
comparisonScheme = scheme;
}
}
/// <summary>
/// The container whose tab order we manage.
/// </summary>
private Control container;
/// <summary>
/// Hash of controls to schemes so that individual containers can have different ordering
/// strategies than their parents.
/// </summary>
private Hashtable schemeOverrides;
/// <summary>
/// The tab index we start numbering from when the tab order is applied.
/// </summary>
private int curTabIndex = 0;
/// <summary>
/// The general tab-ordering strategy (i.e. whether we tab across rows first, or down columns).
/// </summary>
public enum TabScheme
{
None,
AcrossFirst,
DownFirst
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="container">The container whose tab order we manage.</param>
public TabOrderManager(Control container)
{
this.container = container;
this.curTabIndex = 0;
this.schemeOverrides = new Hashtable();
}
/// <summary>
/// Construct a tab order manager that starts numbering at the given tab index.
/// </summary>
/// <param name="container">The container whose tab order we manage.</param>
/// <param name="curTabIndex">Where to start numbering.</param>
/// <param name="schemeOverrides">List of controls with explicitly defined schemes.</param>
private TabOrderManager(Control container, int curTabIndex, Hashtable schemeOverrides )
{
this.container = container;
this.curTabIndex = curTabIndex;
this.schemeOverrides = schemeOverrides;
}
/// <summary>
/// Explicitly set a tab order scheme for a given (presumably container) control.
/// </summary>
/// <param name="c">The control to set the scheme for.</param>
/// <param name="scheme">The requested scheme.</param>
public void SetSchemeForControl( Control c, TabScheme scheme )
{
schemeOverrides[c] = scheme;
}
/// <summary>
/// Recursively set the tab order on this container and all of its children.
/// </summary>
/// <param name="scheme">The tab ordering strategy to apply.</param>
/// <returns>The next tab index to be used.</returns>
public int SetTabOrder( TabScheme scheme )
{
// Tab order isn't important enough to ever cause a crash, so replace any exceptions
// with assertions.
try
{
ArrayList controlArraySorted = new ArrayList();
controlArraySorted.AddRange( container.Controls );
controlArraySorted.Sort( new TabSchemeComparer( scheme ) );
foreach( Control c in controlArraySorted )
{
Debug.WriteLine( "TabOrderManager: Changing tab index for " + c.Name );
c.TabIndex = curTabIndex++;
if( c.Controls.Count > 0 )
{
// Control has children -- recurse.
TabScheme childScheme = scheme;
if( schemeOverrides.Contains( c ) )
{
childScheme = (TabScheme) schemeOverrides[c];
}
curTabIndex = (new TabOrderManager( c, curTabIndex, schemeOverrides )).SetTabOrder( childScheme );
}
}
return curTabIndex;
}
catch( Exception e )
{
Debug.Assert( false, "Exception in TabOrderManager.SetTabOrder: " + e.Message );
return 0;
}
}
}
}
After Loading the controls use following code to set the Tab order for e.g on load you can use the code.
TabOrderManager.TabScheme scheme = TabOrderManager.TabScheme.AcrossFirst;
TabOrderManager tom = new TabOrderManager(this); // Pass the container object
tom.SetTabOrder(scheme);
- Marked As Answer bychuckdawit Wednesday, October 07, 2009 6:01 PM
-
| | Malik M.Shahid Wednesday, October 07, 2009 8:26 AM |
| |
You can use google to search for other answers |
|
|
|
| | More Threads | | | Get a certain text from a window | | PrintPreviewDialog.showDialog | | edit xml configuration file | | Hi to set widths and Heights of PropertyGrid parts | | BLOB to Listview | | setup and deployment/one click deployment problem | | tablelayoutpanel, combobox, array | | instantiating form from combobox list of objects | | Official Time Webservice | | Compiler for java in Visual Studio 2005 Standard Edition | |
|