Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.aspx" rel="noreferrer"><code>TabControl</code></a> and its <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.selectedindexchanged.aspx" rel="noreferrer"><code>SelectedIndexChanged</code></a> event will do what you need.</p> <p>For example, you have a Customer file with a <code>TabControl</code> in its details portion of the form. You want to load lazy-load this customer's transactions when the user clicks the Transactions <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.tabpage.aspx" rel="noreferrer"><code>TabPage</code></a>. Your code should look like this pseudo-code:</p> <pre><code>public partial class CustomerMgmtForm : Form { // Assuming the design already done, so the TabControl control exists on your form. public CustomerMgmtForm() { tabControl1.SelectedIndexChanged += new EventHandler(tabControl1_SelectedIndexChanged); } private void tabControl1_SelectedIndexchanged(object sender, EventArgs e) { switch((sender as TabControl).SelectedIndex) { case 0: // Do nothing here (let's suppose that TabPage index 0 is the address information which is already loaded. break; case 1: // Let's suppose TabPage index 1 is the one for the transactions. // Assuming you have put a DataGridView control so that the transactions can be listed. // currentCustomer.Id can be obtained through the CurrencyManager of your BindingSource object used to data bind your data to your Windows form controls. dataGridview1.DataSource = GetTransactions(currentCustomer.Id); break; } } } </code></pre> <p>The following are also useful while playing with the <code>TabControl</code>.</p> <ol> <li>TabControl.TabPages.Add();</li> <li>TabControl.TabPages.Contains();</li> <li>TabControl.TabPages.ContainsKey();</li> <li>TabControl.TabPages.Insert();</li> <li>TabControl.TabPages.Remove();</li> <li>TabControl.TabPages.RemoveAt();</li> <li>TabControl.TabPages.RemoveByKey().</li> </ol> <p>Using the <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.tabpagecollection_members.aspx" rel="noreferrer"><code>TabControl.TabPageCollection Members</code></a>.</p> <p><strong>EDIT #1</strong></p> <blockquote> <p>For selecting a specific tab, it can only be identified by 0, 1, 2, and not the tab name?</p> </blockquote> <p>Yes, you might as well increment or decrement the <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.selectedindex.aspx" rel="noreferrer"><code>TabControl.SelectedIndex</code></a> property to make a specific <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.tabpage.aspx" rel="noreferrer"><code>TabPage</code></a> selected/active.</p> <p>One thing though, make sure you don't index a <code>TabPage</code> out of the <code>TabPages.Count - 1</code>, since the start index is 0, otherwise you'll get an <a href="http://msdn.microsoft.com/en-us/library/system.indexoutofrangeexception.aspx" rel="noreferrer"><code>IndexOutOfRangeException</code></a> thrown.</p> <p>To continue with our example where we have two pages, the Address information and the Transactions:</p> <pre><code>// Will automatically change the selected tab to the Transactions TabPage. tabControl1.SelectedIndex = 1; // Considering there a count of two TabPages, the first is indexed at 0, and the second at 1. // Setting the SelectedIndex property to 2 will throw. tabControl1.SelectedIndex = 2; </code></pre> <p><em>Note: Any change to <code>TabControl.SelectedIndex</code> property will trigger the <code>TabControl.SelectedIndexChanged</code> event.</em></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload