我正在开发一个表单,该表单根据组合框中的选定索引显示几个用户控件之一。该表单用于添加和编辑包含不同数据字段的不同类型的项目(即服务、库存、非库存等)。用户控件需要访问表单上的一些控件才能持久化 Linq-to-SQL 对象。
我遇到的问题是值UserControl.ParentForm
总是返回null。我意识到理想情况下控件不应该与表单紧密耦合,但是,在考虑我的重构选项之前,我想了解为什么会发生这种情况。
下面,我在表单中包含了添加用户控件的方法和尝试获取 ParentForm 的控件中的方法。有人可以给我建议为什么 ParentForm 可能是 NULL 吗?
表格代码:
void comboBoxEditType_SelectedIndexChanged(object sender, EventArgs e)
{
string value = comboBoxEditType.SelectedItem.ToString();
if (value == "DiscountItem")
{
if (itemControl != null)
this.Controls.Remove(itemControl);
labelControlDescriptionOfType.Text = "Use to subtract a percentage or fixed amount from a total or subtotal. "
+ "Do not use this item type for an early payment discount.";
DiscountItemControl control = new DiscountItemControl();
control.Location = new Point(13, 110);
this.Controls.Add(control);
itemControl = control;
}
else if (value == "InventoryItem")
{
if (itemControl != null)
this.Controls.Remove(itemControl);
labelControlDescriptionOfType.Text = "Use for goods you purchase, track as inventory, and resell.";
InventoryItemControl control = new InventoryItemControl();
control.Location = new Point(13, 110);
this.Controls.Add(control);
itemControl = control;
}
else if (value == "NonInventoryItem")
{
if (itemControl != null)
this.Controls.Remove(itemControl);
labelControlDescriptionOfType.Text = "Use for goods you buy but don't track, like office supplies, "
+ "or materials for a specific job that you charge back to the customer.";
NonInventoryPartItemControl control = new NonInventoryPartItemControl();
control.Location = new Point(13, 110);
this.Controls.Add(control);
itemControl = control;
}
else if (value == "OtherChargeItem")
{
if (itemControl != null)
this.Controls.Remove(itemControl);
labelControlDescriptionOfType.Text = "Use for miscellaneous labor, material, or part charges, such as delivery charges, "
+ "setup fees, and service charges.";
OtherChargeItemControl control = new OtherChargeItemControl();
control.Location = new Point(13, 110);
this.Controls.Add(control);
itemControl = control;
}
else if (value == "ServiceItem")
{
if (itemControl != null)
this.Controls.Remove(itemControl);
labelControlDescriptionOfType.Text = "Use for services you charge for or purchase, like specialized labor, consulting hours, or "
+ "professional fees.";
ServiceItemControl control = new ServiceItemControl();
control.Location = new Point(13, 110);
this.Controls.Add(control);
itemControl = control;
}
// Move the location of buttons
int btnX = 12 + itemControl.Width + 10;
btnSaveAndClose.Location = new Point(btnX, btnSaveAndClose.Location.Y);
btnSaveAndNew.Location = new Point(btnX, btnSaveAndNew.Location.Y);
btnCancel.Location = new Point(btnX, btnCancel.Location.Y);
// Center the Make Inactive CheckBox on side of UserControl
Point point = new Point();
point.X = btnCancel.Location.X;
point.Y = itemControl.Location.Y + itemControl.Size.Height / 2;
checkEditMakeInactive.Location = point;
// Resize the window to fit fit the control
int windowWidth = 140 + itemControl.Width;
int windowHeight = 150 + itemControl.Height;
this.Size = new Size(windowWidth,windowHeight);
// Resize the group containing information on the item type
int groupWidth = itemControl.Width;
int groupHeight = groupControlType.Height;
groupControlType.Size = new Size(groupWidth, groupHeight);
//this.groupControlType.Size
// Resize the label
int labelWidth = itemControl.Width - 27 - comboBoxEditType.Size.Width;
labelControlDescriptionOfType.Size = new Size(labelWidth, labelControlDescriptionOfType.Height);
}
尝试获取 ParentForm 的用户控制代码(始终返回 NULL)
private void CopyItemDataFromForm()
{
currentDiscountItem.Name = textEditItemName_Number.Text;
if (checkEditSubItemOf.Checked)
{
currentDiscountItem.fkParentItem = ComboBoxHelper.getItemFromComboBox(comboBoxEditParentItem);
}
currentDiscountItem.SalesDescription = memoEditDescription.Text;
currentDiscountItem.SalesPrice = Convert.ToDecimal(textEditAmountOrPercent.Text);
currentDiscountItem.fkIncomeAccount = ComboBoxHelper.getQBLookUpFromComboBox(comboBoxEditAccount);
currentDiscountItem.fkTaxCode = ComboBoxHelper.getQBLookUpFromComboBox(comboBoxEditTaxCode);
AddEditItemForm form = this.ParentForm as AddEditItemForm;
currentDiscountItem.IsActive = !(form.GetMakeInactiveCheckboxValue());
}