private void LoadItems() { if (items != null && items.Count > 0) { panel2.SuspendLayout(); panel2.Controls.Clear(); for (int i = 0; i < items.Count; i++) { FormItem item = items[i]; FormEditControl fec = new FormEditControl(); fec.Field = item; fec.Location = new Point(width, height + (height + fec.Height) * i); fec.Width = panel2.Width - width * 2; fec.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right; fec.AddItemHandler += new EventHandler(fec_AddItemHandler); fec.RemoveItemHandler += new EventHandler(fec_RemoveItemHandler); fec.ChangeItemNameHandler += new FieldNameChangedHandler(fec_ItemNameChangedHandler); fec.ChangeItemValueHandler += new FieldValueChangedHandler(fec_ChangeItemValueHandler); if (items.Count == 1) { fec.CanRemove = false;//最后一个必须留着,免得无法再添加 } panel2.Controls.Add(fec); } panel2.ResumeLayout(true); } }
void fec_ItemNameChangedHandler(FormEditControl fec, string newName) { int index = items.GetIndex(fec.Field); if (index > -1) { items[index].ItemName = newName; } }
void fec_ChangeItemValueHandler(FormEditControl fec, ItemValueControl value) { int index = items.GetIndex(fec.Field); if (index > -1) { items[index].ItemType = Commons.GetType(value.ItemType); items[index].ItemValue = value.ItemValue; } }
void fec_RemoveItemHandler(object sender, EventArgs e) { FormEditControl thisItem = sender as FormEditControl; int index = items.GetIndex(thisItem.Field); if (index > -1) { items.RemoveAt(index); LoadItems(); } }
void fec_AddItemHandler(object sender, EventArgs e) { FormEditControl thisItem = sender as FormEditControl; string newName; int index = items.GetIndex(thisItem.Field, out newName); if (index > -1) { items.Insert(index + 1, FormItem.Empty(newName)); LoadItems(); } }