Control focus in enabled/disabled panels

  • Thread starter Thread starter emorgoch.public
  • Start date Start date
E

emorgoch.public

I'm having some trouble with Windows Forms in .NET 1.1 that I haven't
been able to find a soution for. Below is a sample form that shows my
problem. Basically, the form has two panels that switch their enabled
state with the button presses. But trying to navigate the form purely
with the keyboard (tab / arrow keys), results in the inability to
select any controls beside buttons (the text boxes / date time picker
will never recieve focus) when the one panel becomes disabled and the
second enabled (or vice-versa). Does anyone know a solution for this
phenomenon? Thanks.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace TestFormsApp
{
/// <summary>
/// Summary description for PanelTest.
/// </summary>
public class PanelTest : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.DateTimePicker dateTimePicker1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public PanelTest()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.panel2 = new System.Windows.Forms.Panel();
this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
this.button2 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.Controls.Add(this.textBox1);
this.panel1.Controls.Add(this.button1);
this.panel1.Location = new System.Drawing.Point(24, 16);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(136, 104);
this.panel1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 56);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "Switch";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(16, 16);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
//
// panel2
//
this.panel2.Controls.Add(this.textBox2);
this.panel2.Controls.Add(this.button2);
this.panel2.Controls.Add(this.dateTimePicker1);
this.panel2.Enabled = false;
this.panel2.Location = new System.Drawing.Point(16, 136);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(256, 136);
this.panel2.TabIndex = 1;
//
// dateTimePicker1
//
this.dateTimePicker1.Location = new System.Drawing.Point(32, 56);
this.dateTimePicker1.Name = "dateTimePicker1";
this.dateTimePicker1.TabIndex = 0;
//
// button2
//
this.button2.Location = new System.Drawing.Point(88, 96);
this.button2.Name = "button2";
this.button2.TabIndex = 1;
this.button2.Text = "Go Back";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(32, 16);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 2;
this.textBox2.Text = "textBox2";
//
// PanelTest
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(496, 526);
this.Controls.Add(this.panel2);
this.Controls.Add(this.panel1);
this.Name = "PanelTest";
this.Text = "PanelTest";
this.panel1.ResumeLayout(false);
this.panel2.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

private void button1_Click(object sender, System.EventArgs e) {
this.panel1.Enabled = false;
this.panel2.Enabled = true;
}

private void button2_Click(object sender, System.EventArgs e) {
this.panel1.Enabled = true;
this.panel2.Enabled = false;
}


}
}
 
I'm having some trouble with Windows Forms in .NET 1.1 that I haven't
been able to find a soution for. Below is a sample form that shows my
problem. Basically, the form has two panels that switch their enabled
state with the button presses. But trying to navigate the form purely
with the keyboard (tab / arrow keys), results in the inability to
select any controls beside buttons (the text boxes / date time picker
will never recieve focus) when the one panel becomes disabled and the
second enabled (or vice-versa). Does anyone know a solution for this
phenomenon? Thanks.

I don't know why it's happening, but a simple fix is to explicitly set the
focus when you enable a panel, i.e. change the two button click handlers to:

private void button1_Click(object sender, System.EventArgs e)
{
this.panel1.Enabled = false;
this.panel2.Enabled = true;
button2.Focus();
}
private void button2_Click(object sender, System.EventArgs e)
{
this.panel1.Enabled = true;
this.panel2.Enabled = false;
button1.Focus();
}

Chris Jobson
 
I'm having some trouble with Windows Forms in .NET 1.1 that I haven't
been able to find a soution for. Below is a sample form that shows my
problem. Basically, the form has two panels that switch their enabled
state with the button presses. But trying to navigate the form purely
with the keyboard (tab / arrow keys), results in the inability to
select any controls beside buttons (the text boxes / date time picker
will never recieve focus) when the one panel becomes disabled and the
second enabled (or vice-versa). Does anyone know a solution for this
phenomenon? Thanks.

Actually I think I've just found the cause and a simpler solution. Things go
wrong if EVERY control on the form is disabled, so just change the order so
that you always enable one panel before disabling the other one, i.e. change
button1_Click to:

private void button1_Click(object sender, System.EventArgs e)
{
this.panel2.Enabled = true;
this.panel1.Enabled = false;
}

Chris Jobson
 
Thanks Chris, that seems to have fixed it up.

Originally I had tried doing textbox1.focus() (Since that's where I
wanted the focus to be anyways), but that wasn't helping anything
either. Seems that making sure one panel is always enabled it the best
strategy.

Evan
 
Back
Top