[英]c# CheckBox is not responding in TableLayoutPanel
我創建了一個擴展TableLayoutPanel的類,該類包含一個CheckBox的3列和兩個RichTextBoxes,我可以在文本框中進行編輯和編寫,但是我CheckBox沒有響應,這里的代碼是:
public class StepPanel : TableLayoutPanel
{
public RichTextBox result;
public CheckBox checkBox;
public RichTextBox step;
public StepPanel()
{
this.result = new RichTextBox();
this.label = new CheckBox();
this.step = new RichTextBox();
this.ColumnCount = 3;
this.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 50F));
this.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.Controls.Add(this.checkBox, 0, 0);
this.Controls.Add(this.step, 1, 0);
this.Controls.Add(this.result, 2, 0); }
然后,我在運行時將一些StepPanel添加到另一個面板中,當我單擊該復選框時,該復選框不會更改其狀態,這是怎么回事?
以下代碼有效, 請參見Gif Here 。 為什么要嘗試使用復選框啟動標簽?
public partial class UserControl1 : TableLayoutPanel
{
public RichTextBox res;
public CheckBox checkBox;
public RichTextBox steps;
public UserControl1()
{
InitializeComponent();
res = new RichTextBox();
checkBox = new CheckBox();
steps = new RichTextBox();
this.ColumnCount = 3;
this.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 50F));
this.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.Controls.Add(this.checkBox, 0, 0);
this.Controls.Add(this.steps, 1, 0);
this.Controls.Add(this.res, 2, 0);
}
}
向面板添加控件的表單
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var uc = new UserControl1();
panel1.Controls.Add(uc);
}
}
UserControl1局部類
partial class UserControl1
{
/// <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 Component 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.label = new System.Windows.Forms.Label();
this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
this.SuspendLayout();
//
// label
//
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(81, 74);
this.label.Name = "label";
this.label.Size = new System.Drawing.Size(35, 13);
this.label.TabIndex = 0;
this.label.Text = "label1";
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.ColumnCount = 2;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.Location = new System.Drawing.Point(84, 141);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 2;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.Size = new System.Drawing.Size(200, 100);
this.tableLayoutPanel1.TabIndex = 1;
//
// UserControl1
//
this.Controls.Add(this.tableLayoutPanel1);
this.Controls.Add(this.label);
this.Name = "UserControl1";
this.Size = new System.Drawing.Size(602, 329);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label;
private System.Windows.Forms.RichTextBox result;
private System.Windows.Forms.RichTextBox step;
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.