简体   繁体   中英

c# CheckBox is not responding in TableLayoutPanel

I created a class extending TableLayoutPanel containing 3 columns of a CheckBox and two RichTextBoxes , I can edit and write in the textboxes but I CheckBox is not responding here's the code :

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); }

I then add some StepPanel's to another panel in runtime the checkbox does not change it's state when I click on it , what is wrong there ?

The following code works See Gif Here . why are you trying initiate a label with a checkbox?

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);
    }
}

Form that adds control to panel

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        var uc = new UserControl1();
        panel1.Controls.Add(uc);
    }
}

UserControl1 partial Class

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM