我正在处理一个窗体,该窗体根据组合框中的所选索引显示多个用户控件之一。该表格用于添加和编辑包含不同数据字段的不同种类的项目(即服务,库存,非库存等)。用户控件需要访问表单上的一些控件,然后才能保留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());
}
最佳答案
您可以尝试使用Parent属性,在对象树中获取此FrameworkElement的父对象。
var result = this.Parent;
关于c# - UserControl.ParentForm为Null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12124844/