我有一个tablelayout面板,我在其中以编程方式添加行。在第一次打开表单时向该容器添加行(使用带有新ValidationForm()的语句)表格显示正常。第二次,由于某种原因,我在顶部添加了行?见这里的图片:
这不会影响我使用的行数,只显示额外的行。每次关闭并打开表单时,行数都会增加。这是我用来添加行的代码:
private void insertRow(Label label, dynamic control)
{
// Create Panel
var panel = new Panel();
// Set Object Properties
label.TextAlign = ContentAlignment.MiddleCenter;
label.Dock = DockStyle.Fill;
control.Dock = DockStyle.Fill;
panel.Dock = DockStyle.Fill;
//generate delete button
var delete = new Button();
delete.Text = "X";
delete.ForeColor = System.Drawing.Color.Red;
delete.MaximumSize.Height.Equals(40);
delete.Name = rowCount.ToString();
delete.Dock = DockStyle.Right;
delete.Click += new EventHandler(deleteRow);
// Add Controls to the panel
panel.Controls.Add(control);
panel.Controls.Add(delete);
// add controls
//tableLayoutPanel_Validations.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 48F));
tableLayoutPanel_Validations.RowCount = rowCount + 1;
tableLayoutPanel_Validations.Controls.Add(label, 0, rowCount);
tableLayoutPanel_Validations.Controls.Add(panel, 1, rowCount);
tableLayoutPanel_Validations.RowStyles.Add(new RowStyle(SizeType.AutoSize));
rowCount++;
}
rowCount是一个私有int,在运行时设置为0.
我的第一直觉是整个表格将重置"在使用声明之后。表单声明是公共部分类ValidationForm:Form {}
使用声明打开表单:
using (AVBuilder.ValidationForm validationBuilder = new AVBuilder.ValidationForm())
{
if (validationBuilder.ShowDialog() == DialogResult.OK)
{
// ADD ITEM TO Validations LIST
ListViewItem result = new ListViewItem(validationBuilder.resultObject + " : " + validationBuilder.resultName);
result.SubItems.Add(validationBuilder.resultJson);
result.ToolTipText = result.Text;
listView_Validations.Items.Add(result);
//MessageBox.Show(validationBuilder.resultJson);
}
}
答案 0 :(得分:2)
确保rowCount变量未声明为static,因为它不会在新表单实例上重置为0.