1

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

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

两个 ComboBox 都同时显示 SelectedText

这是 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;
}
4

0 回答 0