c#中的TableLayoutPanel

时间:2016-03-07 14:54:46

标签: c# winforms visual-studio tablelayoutpanel

我有生成textBox的代码,因此无法逐个提供属性。 TextBox需要你只能输入数字,尝试KeyPress,但我无法分配它:

我设法定义了一些属性如下:

  tableLayoutPanel1.Controls [a * size+ b].enabled = false;
  tableLayoutPanel1.Controls [a * size + b]= Color.FromArgb.BackColor (255, 255, 153);
  tableLayoutPanel1.Controls [a + b * size].font = new Font(tableLayoutPanel1.Controls [a + b * size].font, FontStyle.Bold);

但是对于我生成textBox的“全局”事件是不可能的。 除了我需要textAlign,我尝试如下:

tableLayoutPanel1.TextAlign = ContentAlignment.MiddleCenter

但我不承认“TextAlign”。

所以他们是相对简单的问题,但我花了很多时间尝试一切:(

PD:我的事件KeyPress是:

    private void keyPress(object sender, KeyPressEventArgs e)
    {
        if (((e.KeyChar) < 48 && e.KeyChar != 8) || e.KeyChar > 57)
        {
            e.Handled = true;
        }
    }

但是我不能将其归为生成的textBox,谢谢!

1 个答案:

答案 0 :(得分:0)

    private void Txt_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (((e.KeyChar) < 48 && e.KeyChar != 8) || e.KeyChar > 57)
        {
            e.Handled = true;
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        TextBox txt = new TextBox();
        txt.KeyPress += Txt_KeyPress; // attach event handler to textbox created at runtime.
        tableLayoutPanel1.Controls.Add(txt);
    }

如果上述代码无法解决您的问题,请注释解释。干杯!