代码之家  ›  专栏  ›  技术社区  ›  oliver

如何使TableLayoutPanel可聚焦

  •  0
  • oliver  · 技术社区  · 5 年前

    我有一个从TableLayoutPanel派生的自定义控件。我想使其可聚焦,但TableLayoutPanel不是。我该怎么做?如果我不能使从TableLayoutPanel派生的控件本身具有可聚焦性,则如何解决它?

    在下面的示例中,我刚刚直接添加了一个TableLayoutPanel,以显示通过窗体的制表符将焦点放在两个组合框上,而不是我的基于TableLayoutPanel的控件上。在本例中,它具有按钮功能,这当然是微不足道的,并且宁愿被现实应用程序中的现有按钮控件替换,但它仅用于演示目的。

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace TestCustomComboBox
    {
        /// <summary>
        /// Description of MainForm.
        /// </summary>
        public class MainForm : Form
        {
            ComboBox comboBox1;
            ComboBox comboBox2;
            TableLayoutPanel tableLayoutPanel1;
            Label label1;
    
            public MainForm()
            {
                comboBox1 = new ComboBox();
                comboBox2 = new ComboBox();
                tableLayoutPanel1 = new TableLayoutPanel();
                label1 = new Label();
                tableLayoutPanel1.SuspendLayout();
                SuspendLayout();
    
                comboBox1.FormattingEnabled = true;
                comboBox1.Location = new Point(82, 45);
                comboBox1.Name = "comboBox1";
                comboBox1.Size = new Size(121, 21);
                comboBox1.TabIndex = 0;
    
                comboBox2.FormattingEnabled = true;
                comboBox2.Location = new Point(96, 99);
                comboBox2.Name = "comboBox2";
                comboBox2.Size = new Size(121, 21);
                comboBox2.TabIndex = 1;
    
                tableLayoutPanel1.ColumnCount = 1;
                tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
                tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 20F));
                tableLayoutPanel1.Controls.Add(label1, 0, 0);
                tableLayoutPanel1.Location = new Point(45, 149);
                tableLayoutPanel1.Name = "tableLayoutPanel1";
                tableLayoutPanel1.RowCount = 1;
                tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
                tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 20F));
                tableLayoutPanel1.Size = new Size(200, 25);
                tableLayoutPanel1.TabIndex = 2;
    
                label1.AutoEllipsis = true;
                label1.BackColor = SystemColors.Window;
                label1.BorderStyle = BorderStyle.FixedSingle;
                label1.Dock = DockStyle.Fill;
                label1.Location = new Point(0, 0);
                label1.Margin = new Padding(0);
                label1.Name = "label1";
                label1.Size = new Size(200, 100);
                label1.TabIndex = 0;
                label1.Text = "This is a placeholder for my own control";
                label1.TextAlign = ContentAlignment.MiddleLeft;
                label1.Click += justToShowThatThisIsAControl;
    
                StartPosition = FormStartPosition.CenterScreen;
                AutoScaleDimensions = new SizeF(6F, 13F);
                AutoScaleMode = AutoScaleMode.Font;
                ClientSize = new Size(284, 261);
                Controls.Add(tableLayoutPanel1);
                Controls.Add(comboBox2);
                Controls.Add(comboBox1);
                Name = "MainForm";
                Text = "TestCustomComboBox";
                tableLayoutPanel1.ResumeLayout(false);
                ResumeLayout(false);
            }
    
            void justToShowThatThisIsAControl(object sender, EventArgs e)
            {
                MessageBox.Show("Boo");
            }
        }
    }
    
    0 回复  |  直到 5 年前