Windows Develop Bookmark and Share   
 index > Windows Forms General > set focus to a child form that is contained in a tab control
 

set focus to a child form that is contained in a tab control

I am developing a software application that has a tab control in its main window. Each tab page can host a maximum of 4 child forms. Once more child form is added to the tab control, a new tab page is created to host this new child form and so on.

childForm.TopLevel = false;
tabControl1.SelectedTab.Controls.Add(childForm);
childForm.Show();

Currently, the problem is that when I click on any of the child forms inside the tab page, the child form does not get the focus. It seems that the focus is set onto the tab page itself rather than passed to the child form. This prevents me from highlighting text inside the child forms.

I have looked at MSDN and it seems that the method Focus() does not help in this case. Is there a way to achieve what I want to do?

Thanks
xenoverse  Wednesday, October 07, 2009 9:39 PM
No, this cannot work.  As soon as you turn a toplevel windows like a Form into a child control, its caption bar stops functioning correctly.  You must use the FormBorderStyle = None setting.  This also prevents the user from arbitrarily move the child controls into non-functional positions.  Using a UserControl is the better mousetrap.

Hans Passant.
  • Marked As Answer byxenoverse 17 hours 50 minutes ago
  •  
nobugz  Wednesday, October 07, 2009 11:50 PM
A Form is not designed to receive the focus, its child controls are.  Add this to the constructor:

            this.SetStyle(ControlStyles.UserMouse, true);


Hans Passant.
nobugz  Wednesday, October 07, 2009 9:57 PM
I have added the SetStyle to the childForm constructor but it has no effect. I also tried to added a MouseClick event handler to the childForm class but it is not receiving the mouse click events. I could drag the child form window within the tab page but the window title bar stays gray instead of highlighted.
xenoverse  Wednesday, October 07, 2009 10:45 PM
No, this cannot work.  As soon as you turn a toplevel windows like a Form into a child control, its caption bar stops functioning correctly.  You must use the FormBorderStyle = None setting.  This also prevents the user from arbitrarily move the child controls into non-functional positions.  Using a UserControl is the better mousetrap.

Hans Passant.
  • Marked As Answer byxenoverse 17 hours 50 minutes ago
  •  
nobugz  Wednesday, October 07, 2009 11:50 PM

You can use google to search for other answers

Custom Search

More Threads

• System.ArgumentException: Parameter is not valid. at System.Drawing.Graphics.GetHdc()
• Detaching Winform app process from command line
• dataGridView Value
• An error on the script of this page.
• proerty grid display problem using ExpandableObjectConverter
• Getting Drive List
• Program Flow between forms
• open calculator
• how to pass value from treeview selectednode
• VS 2005 - MDI Child forms not displaying correctly