Determine order of controls on form

  • Thread starter Markus Wildgruber
  • Start date
M

Markus Wildgruber

Hi!

Is there a way to determine the (tab-)order of controls at runtime? My goal
is to get a unique number for each control on a form that could deal as a
priority value for that control.

The easiest approach is the TabIndex-property of all controls, but
unfortunately this does not work with several levels that exist when some
controls are placed on a panel.

I need a common approach to this problem because I don't know how the forms
will look like on whom that algorithm is run.

How can I get a real Z-order value that is unique for the whole form and not
just for the level?

TIA,

Markus
 
T

Tim Wilson

The code below will traverse controls, starting at the beginning (Form), and
assign a simple priority based on the actual tab order. These priorities are
stored in a hashtable using the control as the key.

private Hashtable priorities = new Hashtable();

/// <summary>
/// Get the controls on the form and assign a
/// priority based on the tab order.
/// </summary>
private void GetPriorities()
{
Control ctrl = this;
int priority = 0;
priorities.Clear();
while ((ctrl = this.GetNextControl(ctrl, true)) != null)
{
priority++;
this.priorities.Add(ctrl, priority);
}
}

Now if you wanted to get the priority for a given control...

object val = this.priorities[this.button1];
if (val is int)
{
// MessageBox.Show(val.ToString());
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top