Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

Problem with focus of an UserControl in a TabControl

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Karsten Schlender - 26 Feb 2008 13:18 GMT
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
Michel Walsh - 27 Feb 2008 15:24 GMT
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

> Hello,
>
[quoted text clipped - 24 lines]
> Kind regards,
> Karsten

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.