Problem with focus of an UserControl in a TabControl

K

Karsten Schlender

Hello,

i have a litte problem with an UserControl as a child of a TabControl.
My UserControl consists of several TextBoxes. This UserControl is placed
on a TabPage, but not the first one. When my program starts i use the
Form Load Event to select the TabPage containing my UserControl by
calling tabControl1.SelectTab(1). The strange thing now is, that my
first TextBox in the UserControl gets the focus and shows the cursor
although the selected TabPage itself should have it. When i place the
TextBoxes directly in a TabPage and select the page as described above,
it works fine, my page gets the focus and i can navigate with the cursor
keys through the TabPages.

It's not a problem specific to the UserControl. It must lie deeper,
maybe in ContainerControl as baseclass, because ToolStripContainer shows
the same strange behaviour.

I know that ContainerControl has a different focus behaviour and selects
the first child control when it's focused. That's maybe the reason of my
focused TextBox. But why gets the UserControl focus in the first place?

When you manually select the different TabPages with a mouse click, this
behaviour of "focus stealing" of the UserControls does not take place.

Do i miss something in my thoughts?

Kind regards,
Karsten
 
M

Michel Walsh

From my experimentation, that is not the behavior I got.


I built a form with two tabs controls, each with two pages. In the first tab
control, I put two edit box on its second page, while on the second tab
control, I place a UC having, itself, three edit box. I added two command
button, their click event selecting the tab of the second page:

private void button1_Click(object sender, EventArgs e)
{
tabControl1.SelectTab(1);
}

private void button2_Click(object sender, EventArgs e)
{
tabControl2.SelectTab(1);
}


Running the Windows application, what I got is : when I click the button AND
the selected page was not the one selected, then the first control of the
said page get the control. Whhtever it is an UC or another control. If I
click on the button a second time, while the page to be selected is already
the one 'displayed', the focus stays on the button rather than moving to
the first control of the page (and next, I can navigate through the control
of the form with the keyboard, as usual, in such situation).

So, in *this* scenario, to get the same effect, anytime, I can use:


private void button1_Click(object sender, EventArgs e)
{
tabControl1.SelectTab(1);
button1.Select();
}

private void button2_Click(object sender, EventArgs e)
{
tabControl2.SelectTab(1);
button2.Select();
}



Hoping it may help,
Vanderghast, Access MVP
 

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