winform程序
我用串口调试助手给串口定时发送指令。
程序中用timer定时器监测,一收到指令就生成一个新的控件,然后把控件添加到panel1
private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            //收到呼叫信息
            if (PublicMethod.callFlag)//呼叫时callFlag变为true
            {
                PublicMethod.callFlag = false;
                     panel1.Controls.Clear();
                    //添加控件
                       Button b = new Button();
                    b.Name = "b" + (i + 1).ToString();
                    panel1.Controls.Add(b);
                    MessageBox.Show(DateTime.Now.ToString());
             }
            timer1.Enabled = true;
        }
添加完一个控件后就再也添加不了,PublicMethod.callFlag状态也在变,MessageBox.Show还是弹出的我不用串口,直接在定时器里这样写就没有问题
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
panel1.Controls.Clear();
//添加控件
 Button b = new Button();
 b.Name = "b" + (i + 1).ToString();
 panel1.Controls.Add(b);
timer1.Enabled = true;
} 为什么?谢谢 

解决方案 »

  1.   

    其实添加成功了,只不过后一个button被第一个button覆盖了,你能不能根据i改变一下各个button的位置,如
     Button b = new Button();
    b.Name = "b" + (i + 1).ToString();
    b.Location=new Point(10,i*30+10);
     panel1.Controls.Add(b);
      

  2.   


    我怀疑两点
    1、串口调试助手定时器频率为100毫秒,winform里的程序定时器频率也为100毫秒
    串口通讯能反应不过来吗?有阻塞这一说吗?
    2、panel1.Controls.Add(b);这个不灵光?为什么不用串口就行?
    对串口通讯不熟悉,大侠帮忙啊!!!
      

  3.   

    大家不要考虑b.name和b.Location了,我为了写的方便,没在贴子里写,这里我处理了
    1、控件名不重复
    2、控件位置不重复我第二种方法就行
      

  4.   

    你每次panel1.Controls.Clear();
    timer1里能添加多个?