gpt4 book ai didi

c# - UserControl.ParentForm 为空

转载 作者:太空宇宙 更新时间:2023-11-03 16:27:24 24 4
gpt4 key购买 nike

我正在处理一个表单,该表单根据组合框中选定的索引显示多个用户控件之一。该表格用于添加和编辑包含不同数据字段的不同种类的项目(即服务、库存、非库存等)。用户控件需要访问表单上的一些控件,然后才能保留 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 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12124844/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com