C#WinForms TableLayoutPanel在添加我的行和如何停止它时闪烁

时间:2015-07-28 21:29:49

标签: c# winforms

此代码可以正常工作,例如添加行和动态删除行。但是,当我从表中删除某些行,然后单击“添加”按钮向面板中添加一些行时,闪烁会在添加更多行时开始增加。

公共全局变量

 public TableLayoutPanel table;
 public int tablecol, tablerow;

在主窗口中创建表格的功能。 此函数创建一个包含一些标题标签的表和名为" ADD"的按钮。

public void createtable()
            {
                Label title = new Label(); title.Text = "Table Name";
                this.myw.right_panel.Controls.Add(title);
                TextBox tname = new TextBox(); tname.Width = 300;
                this.myw.right_panel.Controls.Add(tname);
                Button addrowbtn = new Button(); addrowbtn.Text = "ADD ROW";
                addrowbtn.Click += addrowbtn_Click;
                this.myw.right_panel.Controls.Add(addrowbtn);

                table = new TableLayoutPanel();
                table.Width = this.myw.right_panel.Width;
                table.Height = 400;
                table.AutoScroll = true;
                table.ColumnCount = 9;

                int col = 0;
                Label[] titlelbl = new Label[9];
                foreach(Label l in titlelbl)
                {
                    titlelbl[col++] = new Label();
                }
                titlelbl[0].Text = "NAME";
                titlelbl[0].Width = 200;
                titlelbl[1].Text = "TYPE";
                titlelbl[2].Text = "NOT NULL";
                titlelbl[3].Text = "PK";
                titlelbl[4].Text = "AI";
                titlelbl[5].Text = "U";
                titlelbl[6].Text = "DEFAULT";
                titlelbl[7].Text = "CHECK";
                titlelbl[8].Text = "DELETE";
                col = 0;
                FlowLayoutPanel headerpanel = new FlowLayoutPanel();
                headerpanel.Width = this.myw.right_panel.Width;
                headerpanel.AutoSize = true;
                foreach(Label l in titlelbl)
                {
                    l.BackColor = Color.LightGray;
                    l.TextAlign = ContentAlignment.MiddleCenter;
                    headerpanel.Controls.Add(l);
                }
                this.myw.right_panel.Controls.Add(headerpanel);
                this.myw.right_panel.Controls.Add(table);

            }

此函数用于在表中创建动态行。每行包含一个名为" DEL"

的按钮
void addrowbtn_Click(object sender, EventArgs e)
        {
            TextBox name = new TextBox(); name.Width = 200;
            ComboBox type = new ComboBox();
            type.Items.AddRange(new string[] {"INTEGER","TEXT","BLOB","NUMERIC","REAL"});
            CheckBox notnull = new CheckBox();
            CheckBox pk = new CheckBox();
            CheckBox ai = new CheckBox();
            CheckBox u = new CheckBox();
            TextBox def = new TextBox(); def.Width = 100;
            TextBox check = new TextBox(); check.Width = 100;
            Button delbtn = new Button(); delbtn.Text = "DEL";delbtn.AutoSize = true;
            delbtn.Click += Delbtn_Click;
            table.Controls.Add(name, 0, tablerow);
            table.Controls.Add(type, 1, tablerow);
            table.Controls.Add(notnull, 2, tablerow);
            table.Controls.Add(pk, 3, tablerow);
            table.Controls.Add(ai,4, tablerow);
            table.Controls.Add(u, 5, tablerow);
            table.Controls.Add(def, 6, tablerow);
            table.Controls.Add(check,7, tablerow);
            table.Controls.Add(delbtn, 8, tablerow);

            tablerow++;

        }

删除行的功能, 只要表格行上的删除按钮单击

,就会调用此函数
 private void Delbtn_Click(object sender, EventArgs e)
        {


            TableLayoutPanelCellPosition tlpcp = new TableLayoutPanelCellPosition();
            tlpcp = table.GetPositionFromControl((Control)sender);
            for (int i = 0; i < table.ColumnCount; i++)
            {
                var control = table.GetControlFromPosition(i, tlpcp.Row);
                table.Controls.Remove(control);
            }

        }

欢迎任何代码改进建议。

0 个答案:

没有答案