Setting default selection on tab page

  • Thread starter Thread starter Rachel Suddeth
  • Start date Start date
R

Rachel Suddeth

I have an form where the whole display is a tab control (well, that plus a
status bar.)

I want to set the focus to the first TextBox on the first TabPage when it
loads. I tried to put that into the FormLoad event, but the form still comes
up with the focus on the tab for the first tab page. Anyone know how to do
this?

Here is what I tried (but simplified)...
----------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace TabTest
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.TextBox textBox1;

private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}


protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}


#region Windows Form Designer generated code

private void InitializeComponent()
{
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.textBox1 = new System.Windows.Forms.TextBox();
this.tabControl1.SuspendLayout();
this.tabPage1.SuspendLayout();
this.SuspendLayout();
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Location = new System.Drawing.Point(0, 0);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.TabIndex = 0;
//
// tabPage1
//
this.tabPage1.Controls.Add(this.textBox1);
this.tabPage1.Location = new System.Drawing.Point(4, 22);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Size = new System.Drawing.Size(192, 74);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "tabPage1";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(16, 8);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(384, 261);
this.Controls.Add(this.tabControl1);
this.Name = "Form1";
this.Text = "Form1";
this.tabControl1.ResumeLayout(false);
this.tabPage1.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void tabControl1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
textBox1.Focus();
}

}
}
______________________________________________________________
Roydan Enterprises Ltd
602 North 9th Street
Manitowoc, WI
54220-3924
 
Oops, mistake in the sample program. I had also tested with setting the
focus to a TextBox after moving off a different tab, and that worked fine,
it did get selected (but then I deleted the wrong event.)

I ran another version identical except it said:
private void Form1_Load(object sender, System.EventArgs e)

{

textBox1.Focus();

}

instead of
private void tabControl1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
textBox1.Focus();
}



and it was exactly the same. With it in the FormLoad, it doesn't set the
focus.

-Rachel



Rachel Suddeth said:
I have an form where the whole display is a tab control (well, that plus a
status bar.)

I want to set the focus to the first TextBox on the first TabPage when it
loads. I tried to put that into the FormLoad event, but the form still comes
up with the focus on the tab for the first tab page. Anyone know how to do
this?

Here is what I tried (but simplified)...
----------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace TabTest
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.TextBox textBox1;

private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}


protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}


#region Windows Form Designer generated code

private void InitializeComponent()
{
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.textBox1 = new System.Windows.Forms.TextBox();
this.tabControl1.SuspendLayout();
this.tabPage1.SuspendLayout();
this.SuspendLayout();
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Location = new System.Drawing.Point(0, 0);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.TabIndex = 0;
//
// tabPage1
//
this.tabPage1.Controls.Add(this.textBox1);
this.tabPage1.Location = new System.Drawing.Point(4, 22);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Size = new System.Drawing.Size(192, 74);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "tabPage1";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(16, 8);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(384, 261);
this.Controls.Add(this.tabControl1);
this.Name = "Form1";
this.Text = "Form1";
this.tabControl1.ResumeLayout(false);
this.tabPage1.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void tabControl1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
textBox1.Focus();
}

}
}
______________________________________________________________
Roydan Enterprises Ltd
602 North 9th Street
Manitowoc, WI
54220-3924
 
Back
Top