表单加载后更改控件上的TabStop

时间:2013-04-05 14:22:56

标签: .net winforms tabstop

我有一个包含一组下拉列表的表单,在某些情况下我希望不要使用Tab键顺序。我已将TabStop属性设置为false,并且当我希望它们处于Tab键顺序时,我将其设置为true。当用户点击该组中的某个下拉列表时,我总是希望将它们全部包含在Tab键顺序中。为此,我在每个控件的Enter事件处理程序中将所有下拉列表的TabStop属性设置为true。这会在Enter上产生奇怪的行为。每个控件似乎都选中了(突出显示的)所有文本。我希望只有具有焦点的控件才能表现得那样。有谁能解释一下?谢谢!

我创建了一个具有相同奇怪行为的简单表单。当表单出现时,只需用鼠标点击其中一个下拉菜单,这就是您将看到的内容:

Two ComboBoxes both display SelectedText at the same time

以下是C#中表单的代码:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void comboBox1_Enter(object sender, EventArgs e)
    {
        comboBox1.TabStop = comboBox2.TabStop = true;
    }

    private void comboBox2_Enter(object sender, EventArgs e)
    {
        comboBox1.TabStop = comboBox2.TabStop = true;
    }

    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (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.comboBox1 = new System.Windows.Forms.ComboBox();
        this.comboBox2 = new System.Windows.Forms.ComboBox();
        this.SuspendLayout();
        // 
        // comboBox1
        // 
        this.comboBox1.FormattingEnabled = true;
        this.comboBox1.Items.AddRange(new object[] {
        "comboBox1",
        "comboBox2"});
        this.comboBox1.Location = new System.Drawing.Point(12, 12);
        this.comboBox1.Name = "comboBox1";
        this.comboBox1.Size = new System.Drawing.Size(121, 21);
        this.comboBox1.TabIndex = 1;
        this.comboBox1.TabStop = false;
        this.comboBox1.Text = "comboBox1";
        this.comboBox1.Enter += new System.EventHandler(this.comboBox1_Enter);
        // 
        // comboBox2
        // 
        this.comboBox2.FormattingEnabled = true;
        this.comboBox2.Items.AddRange(new object[] {
        "comboBox1",
        "comboBox2"});
        this.comboBox2.Location = new System.Drawing.Point(12, 39);
        this.comboBox2.Name = "comboBox2";
        this.comboBox2.Size = new System.Drawing.Size(121, 21);
        this.comboBox2.TabIndex = 2;
        this.comboBox2.TabStop = false;
        this.comboBox2.Text = "comboBox2";
        this.comboBox2.Enter += new System.EventHandler(this.comboBox2_Enter);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(284, 77);
        this.Controls.Add(this.comboBox2);
        this.Controls.Add(this.comboBox1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.ComboBox comboBox1;
    private System.Windows.Forms.ComboBox comboBox2;
}

0 个答案:

没有答案