Change SelectedTab when Child control gets focus
-
I am using VB .NET 2005. I have a tab control in my form and the tab control contains 4 tab pages. Each tab page has different controls in it. If my selected (current) tab is 3rd tab and I set the focus to a child control in first tab page by using ControlName.Focus() The control does not fire any Enter/GotFocus event Is there anyway to set the SelectedTab or SelectedIndex of the TabControl to the TabPage in which the Control to be focused exists. Waiting for a quick reply. Thank You. -- modified at 12:14 Thursday 26th January, 2006
-
I am using VB .NET 2005. I have a tab control in my form and the tab control contains 4 tab pages. Each tab page has different controls in it. If my selected (current) tab is 3rd tab and I set the focus to a child control in first tab page by using ControlName.Focus() The control does not fire any Enter/GotFocus event Is there anyway to set the SelectedTab or SelectedIndex of the TabControl to the TabPage in which the Control to be focused exists. Waiting for a quick reply. Thank You. -- modified at 12:14 Thursday 26th January, 2006
Here's a few quick tips... You can change the tab selection like this.
myTabControl.SelectedTab = myTabPage
You can find out which TabPage a control belong's to by searching the TabPage.Controls collection. Also, the control's Parent property "might" return a reference to the TabPage that owns it. You'll have to test that one out to be sure. -
Here's a few quick tips... You can change the tab selection like this.
myTabControl.SelectedTab = myTabPage
You can find out which TabPage a control belong's to by searching the TabPage.Controls collection. Also, the control's Parent property "might" return a reference to the TabPage that owns it. You'll have to test that one out to be sure.Thank you for your reply. I have tried by setting the tag property of each control on the tab page to the index of its tab page (i.e. 0 for controls on first tab, 1 for second etc.) at design time. Then I have dynamically associated the event handler for "Enter" event of each control on the tab page. Event handler looks like TabControl1.SelectedIndex = DirectCast(sender, Control).Tag But the control does not get focus when I call the .Focus() method I don't want to explicitly call the TabControl1.SelectedIndex before setting Focus to any control. Is there anyway?
-
Thank you for your reply. I have tried by setting the tag property of each control on the tab page to the index of its tab page (i.e. 0 for controls on first tab, 1 for second etc.) at design time. Then I have dynamically associated the event handler for "Enter" event of each control on the tab page. Event handler looks like TabControl1.SelectedIndex = DirectCast(sender, Control).Tag But the control does not get focus when I call the .Focus() method I don't want to explicitly call the TabControl1.SelectedIndex before setting Focus to any control. Is there anyway?
Calling Focus() won't change the tab selection. Plus a control won't gain Focus if it's not visible. You're going to have to show the tab page yourself. Now, if you want to keep it generic, I suppose you can do something like this. Just pass the control you want to give focus to in this function. It's just that easy.
Private Sub SetControlFocus(ByVal ctrl As Control)
' Validate argument.
If (ctrl Is Nothing) Then Return' If control belongs to a tab page, then show it.
If (TypeOf ctrl.Parent Is TabPage) Then
myTabControl.SelectedTab = DirectCast(ctrl.Parent, TabPage)
End If' Give control focus.
ctrl.Focus()
End Sub