Hi, i'm trying to focus the cursor on a textbox inside one of my tab controls (3 tabs) but my method doesn't seem to work...Any ideas?
Here is my code:

private void Maintenance_Load(object sender, EventArgs e)
        {
            tabControl1.Focus();
            tabControl1.SelectedIndex = 0;
            txt_CLName.Focus();
        }

Recommended Answers

All 4 Replies

Hi,

I ran the same code and it focuses fine.
You dont need to call

tabControl1.Focus();

since you then move focus to the textbox.
When you set the SelectedIndex for the tabControl are you selecting the tab page which contains the textbox?

The other thing to do is check that nothing is calling focus away from the textbox after you set focus to it.

Hello, phoenix_dwarf.

While playing with your code found interesting thingy: it works fine on every tab .. except the 1st one. Anyway, while trying to explain such behaviour, found this:

Note:
Focus is a low-level method intended primarily for custom control authors. Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms.

So, this should work fine:

tabControl1.SelectedIndex = 0;
txt_CLName.Select();
commented: nice catch, i learnt something too here :p +1

Tnx Antenka...it works. I'm just puzzled by why my original code doesn't want to work...? l0ol™ Ahw well, thnx again!

You're welcome :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.