jiangyuxuan

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理


private void button1_Click(object sender, EventArgs e)
{

int num=0;

string strchar;
string str = "";
num = textBox1.Text.Length;

str = textBox1.Text;

textBox2.Text = Convert.ToString(num);


tableLayoutPanel1.GetType().GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).SetValue(tableLayoutPanel1, true, null);

// tableLayoutPanel1.AutoScroll = true;
// tableLayoutPanel1.BackColor = Color.White;
// tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
// tableLayoutPanel1.ColumnCount = 6;


tableLayoutPanel1.Controls.Clear();

for (int i = 0; i < num ; i++)
{
// ——行和列的 样式 默认为auto
//tableLayoutPanel4.ColumnStyles.Add(new ColumnStyle());
//tableLayoutPanel4.RowStyles.Add(new RowStyle());



strchar = str.Substring(i, 1);

 

 

 

Label lab = new Label();
// lab.Name = "Label_" + i;
// lab.Text = "Label_" + i;

lab.Text = Convert.ToString( i+1);

lab.Font = new Font(label1.Font.FontFamily, 15,label1.Font.Style);

switch (strchar)
{
case "0":
//Console.WriteLine("很棒!");

lab.BackColor = Color.White;
break;

case "1":
lab.BackColor = Color.Red;
break;
case "2":
lab.BackColor = Color.Yellow;
break;
default:
lab.BackColor = Color.White;
break;
}

// lab.Text = strchar;

lab.AutoSize = true;
//通过Anchor 设置Label 列中居中
lab.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));


tableLayoutPanel1.Controls.Add(lab);

 

 


// TextBox txtObj = new TextBox();
// txtObj.Text = "TextBox_" + i;
// txtObj.Width = 70;
// tableLayoutPanel1.Controls.Add(txtObj);
}
}

posted on 2016-01-24 18:58  江宇旋  阅读(231)  评论(0编辑  收藏  举报