- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在处理一个表单,该表单根据组合框中选定的索引显示多个用户控件之一。该表格用于添加和编辑包含不同数据字段的不同种类的项目(即服务、库存、非库存等)。用户控件需要访问表单上的一些控件,然后才能保留 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/
我有一个应用程序,我在其中的表单上添加了一个用户控件。当我在 userControl 构造函数中检查 this.parentForm 时,它给出了一个空引用 我的用户控制代码是这样的 public U
我正在将自定义的 MenuStrip 控件集成到另一个 .NET 应用程序中。除了焦点问题外,MenuStrip 工作正常。如果承载 MenuStrip 的窗体未获得焦点,则用户需要单击两次 - 一次
我正在处理一个表单,该表单根据组合框中选定的索引显示多个用户控件之一。该表格用于添加和编辑包含不同数据字段的不同种类的项目(即服务、库存、非库存等)。用户控件需要访问表单上的一些控件,然后才能保留 L
在 winforms 中我们有 objForm.Owner 和 objForm.Parent。这两者有什么区别。 我从表单 A 打开表单 B 作为对话框,并期望使用 ParentForm 属性从表单
简单的问题。我有一个 MainForm 和一个 settingsForm。设置表单初始化一次,然后在用户每次单击按钮时显示。单击此按钮时,我需要它执行某些操作。 m_inputSettings.Sho
第一个问题。如果需要,请帮忙格式化。 上下文 我的主窗体中有一个 TWebBrowser,它的行为就像打印机一样。 因此,当用户在真实打印机中执行一些命令时,我会在其中加载一些 HTML 文本...
我想知道 Control.FindForm 和 ContainerControl.ParentForm 之间是否存在任何功能(或其他)差异?我在创建自己的用户控件时一直在使用 .ParentForm
我有一个包含多个子表单的父表单(例如 http://jsfiddle.net/riemersebastian/9zr00ear/3/ ) 当用户更改任何字段时,相应的子表单和父表单都会获取类 ng-d
我让这段代码工作了! 我的 UserControl 内有一个按钮,在设计时掉落在我的表单中。一切正常,当我在按钮事件中调用 this.ParentForm 时,它正确地向我返回了(唯一的)父表单。 重
我是一名优秀的程序员,十分优秀!