Category / Section
How to select the tab programmatically in WinForms TabbedMDIManager?
2 mins read
Select the tab programmatically
Since the TabControlAdv has been used internally to display the items in Tabbed structure, we can use the properties of TabControlAdv to change the selected tab programmatically in TabbedMDIManager. The following code snippet demonstrates how to change the selected tab programmatically through SelectedIndex and SelectedTab property of TabControlAdv,
C#
public partial class Form1 : Form { public Form1() { InitializeComponent(); this.textBox1.KeyDown += TextBox1_KeyDown; this.tabbedMDIManager1.TabControlAdded += TabbedMDIManager1_TabControlAdded; } TabControlAdv tabcontrol; private void TabbedMDIManager1_TabControlAdded(object sender, TabbedMDITabControlEventArgs args) { tabcontrol = args.TabControl; } private void TextBox1_KeyDown(object sender, KeyEventArgs e) { if(e.KeyCode == Keys.Enter && !string.IsNullOrEmpty(textBox1.Text)) { int index = Convert.ToInt32(textBox1.Text); //We can also change selected index using SelectedIndex property //tabcontrol.SelectedIndex = index; tabcontrol.SelectedTab = tabcontrol.TabPages[index]; } } private void AddItems() { for (int i = 0; i <= 10; i++) { Form2 form = new Form2(); form.MdiParent = this; form.Text = "Form" +i; form.Show(); } } }
VB
Partial Public Class Form1 Inherits Form Public Sub New() InitializeComponent() AddHandler Me.textBox1.KeyDown, AddressOf TextBox1_KeyDown AddHandler Me.tabbedMDIManager1.TabControlAdded, AddressOf TabbedMDIManager1_TabControlAdded AddItems() End Sub Private tabcontrol As TabControlAdv Private Sub TabbedMDIManager1_TabControlAdded(ByVal sender As Object, ByVal args As TabbedMDITabControlEventArgs) tabcontrol = args.TabControl End Sub Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) If e.KeyCode = Keys.Enter AndAlso Not String.IsNullOrEmpty(textBox1.Text) Then Dim index As Integer = Convert.ToInt32(textBox1.Text) 'We can also change selected index using SelectedIndex property 'tabcontrol.SelectedIndex = index; tabcontrol.SelectedTab = tabcontrol.TabPages(index) End If End Sub Private Sub AddItems() For i As Integer = 0 To 10 Dim form As New Form2() form.MdiParent = Me form.Text = "Form" & i form.Show() Next i End Sub End Class
Screenshot:
Form7 has been selected based on the input
Samples