我有一个自TableLayoutPanel派生的自定义控件。我想使其具有焦点,但TableLayoutPanel不是。我怎样才能做到这一点?如果我无法使从TableLayoutPanel派生的控件本身具有可聚焦性,该如何解决呢?
在下面的示例中,我直接添加了一个TableLayoutPanel,以显示在表单中进行制表可以将两个ComboBox集中在一起,而不是基于TableLayoutPanel的控件。在这种情况下,它具有按钮功能,这当然是微不足道的,宁愿被现实应用程序中的现有Button控件取代,但这仅用于演示目的。
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");
}
}
}