FIX: setSelectedTab() In TabControl Does Not Properly Paint Children


When making a call to TabPage.setSelectedTab() from within an OnSelectedIndexChanged event handler of the parent TabControl, the focus will first go to the user-selected index before returning to the index value set in the setSelectedTab() call. In addition, when returning to the index set in code by the setSelectedTab() call, the children components of the set TabPage will not be visible and Control.setVisible(true) must be used, causing a noticeable flicker.


To work around this problem, call Control.setVisible(true) on the TabPage after the setSelectedTab() call is made.


Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

This bug was corrected in Visual Studio 6.0 Service Pack 3.

For more information about Visual Studio service packs, please see the following articles in the Microsoft Knowledge Base:

194022 INFO: Visual Studio 6.0 Service Packs, What, Where, Why

194295 HOWTO: Tell That Visual Studio 6.0 Service Packs Are Installed


Steps to Reproduce Behavior

  1. Open a new Visual J++ Windows Application project.
  2. Add a TabControl and add some TabPages to the control.
  3. Add an Edit control and a Button control to each TabPage.
  4. Add an OnSelectedIndexChanged event handler to the TabControl.
  5. In the event handler, check the text value of the Edit control on the current TabPage; and, if it is not valid, use setSelectedTab() to keep the focus on the current TabPage until the value is valid (see the code sample below).

    Results:When setting the TabPage back to the one that had invalid data in its Edit box, the Edit box will not be visible. Call TabControl.setVisible(true) to work around this problem.
import*;import*;import*;import*;import*;public class Form1 extends Form{  TabPage lastPage;  public Form1()  {    super();    initForm();    lastPage = tabPage1;  }  public void dispose()  {    super.dispose();    components.dispose();  }  private void onSelectedIndexChanged(Object source, Event e)  {    Control[] ctrls = lastPage.getControls();    for (int i = 0; i < ctrls.length; i++)    {      if (ctrls[i] instanceof Edit)      {        Edit edit = (Edit)ctrls[i];        if (edit.getText().equalsIgnoreCase("Valid Data"))        {          lastPage = tabControl1.getSelectedTab();          return;        }        tabControl1.setSelectedTab(lastPage);        break;      }    }  }  /**   * NOTE: The following code is required by the Visual J++ form   * designer. It can be modified using the form editor. Do not   * modify it using the code editor.   */   Container components = new Container();  TabControl tabControl1 = new TabControl();  TabPage tabPage1 = new TabPage();  TabPage tabPage2 = new TabPage();  TabPage tabPage3 = new TabPage();  Edit edit1 = new Edit();  Button button1 = new Button();  Button button2 = new Button();  Edit edit2 = new Edit();  Button button3 = new Button();  Edit edit3 = new Edit();  private void initForm()  {    this.setText("Form1");    this.setAutoScaleBaseSize(new Point(5, 13));    this.setClientSize(new Point(546, 261));    tabControl1.setLocation(new Point(16, 8));    tabControl1.setSize(new Point(504, 232));    tabControl1.setTabIndex(0);    tabControl1.setText("tabControl1");    tabControl1.setSelectedIndex(2);    tabControl1.addOnSelectedIndexChanged(new EventHandler(this.onSelectedIndexChanged));    tabPage1.setLocation(new Point(4, 25));    tabPage1.setSize(new Point(496, 203));    tabPage1.setTabIndex(0);    tabPage1.setText("tabPage1");    tabPage2.setLocation(new Point(4, 25));    tabPage2.setSize(new Point(496, 203));    tabPage2.setTabIndex(1);    tabPage2.setText("tabPage2");    tabPage3.setLocation(new Point(4, 25));    tabPage3.setSize(new Point(496, 203));    tabPage3.setTabIndex(2);    tabPage3.setText("tabPage3");    edit1.setLocation(new Point(8, 8));    edit1.setSize(new Point(448, 20));    edit1.setTabIndex(0);    edit1.setText("edit1");    button1.setLocation(new Point(8, 168));    button1.setSize(new Point(75, 23));    button1.setTabIndex(1);    button1.setText("button1");    button2.setLocation(new Point(8, 168));    button2.setSize(new Point(75, 23));    button2.setTabIndex(0);    button2.setText("button2");    edit2.setLocation(new Point(8, 8));    edit2.setSize(new Point(424, 20));    edit2.setTabIndex(1);    edit2.setText("edit2");    button3.setLocation(new Point(8, 168));    button3.setSize(new Point(75, 23));    button3.setTabIndex(0);    button3.setText("button3");    edit3.setLocation(new Point(8, 8));    edit3.setSize(new Point(416, 20));    edit3.setTabIndex(1);    edit3.setText("edit3");    this.setNewControls(new Control[] {                        tabControl1});    tabControl1.setNewControls(new Control[] {                               tabPage1,                                tabPage2,                                tabPage3});    tabPage1.setNewControls(new Control[] {                            button1,                             edit1});    tabPage2.setNewControls(new Control[] {                            edit2,                             button2});    tabPage3.setNewControls(new Control[] {                            edit3,                             button3});  }  public static void main(String args[])  { Form1());  }}


For support information about Visual J++ and the SDK for Java, visit the following Microsoft Web site:

Article ID: 223351 - Last Review: Feb 14, 2017 - Revision: 1

Microsoft Visual J++ 6.0 Standard Edition, Microsoft Software Development Kit for Java 3.1