Focus on First tab index programatically

Hi, I am developing C# windows application.

Is there a way to set focus programatically based on first tab order. Currently am doing control.focus.

PS: I do know that on opening the form focus remains on first control in tab order.
GRChandrashekarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carl TawnSystems and Integration DeveloperCommented:
You could try doing it with a LINQ query like:
Control c = this.Controls.Cast<Control>().Where(p => p.TabIndex == 0).First();
c.Focus();

Open in new window

0
Éric MoreauSenior .Net ConsultantCommented:
do you know the name of the control? in this case you could use: tabControl1.SelectTab(NameOfTheFirstTabPage);
0
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
Programatically you can use this method too:-

tabControl1.SelectedTab = tabName;

Open in new window


Details here
0
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

GRChandrashekarAuthor Commented:
@carl_tawn

This is what I am looking for. But it is not working. Is it because the control is within group box ?
0
Carl TawnSystems and Integration DeveloperCommented:
Probably. Controls inside a container control have their own tab index order separate from the main form. You can either replace "this.Controls" with "this.YourGroupBox.Controls" in the Linq query, or you would have to extend the code to handle nested containers.
0
GRChandrashekarAuthor Commented:
How to  extend the code to handle nested containers.
0
Carl TawnSystems and Integration DeveloperCommented:
Try it as a recursive function:
        private void TabToFirst(Control container)
        {
            Control c = container.Controls.Cast<Control>().Where(exp => exp.TabIndex == 0).First();
            if (c.HasChildren)
                TabToFirst(c);
            else
                c.Focus();
        }

Open in new window

Called like:
TabToFirst(this);     // kick off by passing the form

Open in new window

The code doesn't cater for not finding a control with a 0 tab index at the moment, which needs tidying up. But the mechanics are there to get you started.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.