检测父母的当前形式 [英] detecting current form parent

查看:76
本文介绍了检测父母的当前形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好b $ b我有一个主窗口形式用于销售和购买发票,用户可以一次打开2个这样的实例。这个表格中有一个按钮,我showdialog()是一种商品形式。当用户打开2个实例(一个用于销售,一个用于购买)和商品形式是showdialog(),商品插入一个关闭形式(之前打开)。

如何检测哪个实例应该是否已经升级了?

谢谢

解决方案

首先,让我们区分一个有'父母的表格'和一个有'所有者'的表格。 br />


在Windows窗体中,将一个Form作为另一个Form的Parent是一个非常糟糕的事情(WinForms的旧MDI架构有它的自己处理子的方式MDIParentForm的形式)。但是,Form的'Owner属性可以设置没有设置Form''Parent'的副作用,如下面的代码所示。



我'我会在这里回答您的具体问题,但我想告诉您,我认为您需要评估您当前的计划。我建议你有一个主表格...也许用户设置应用程序首选项? ...然后有第二个表格,主表格创建用于购买和销售的实例。



创建主表单的第二个实例将导致设计时体验,您将看到主表单上的所有控件(当然)重复并锁定在第二个表单设计视图上。并且,如果您的主窗体在其加载事件中创建新对象,那么您将在第二个实例中复制创建这些对象,这可能导致混乱。



如果使用'ShowDialog以模态方式显示Form,可以将可选参数添加到'ShowDialog,设置模态显示Form的'Owner属性:

  //  当您从'currentBaseForm  
中显示模态表格时myModalForm.ShowModal( this );

// 'myModalForm现在提供对'currentBaseForm'实例的引用
// 通过'所有者属性

< span class =code-comment> // in'myModalForm
currentBaseForm = this .Owner;

// 哪个表格是所有者?
if (currentBaseForm.Name == myBuyForm
{

}
else
{

}


Hi I have a main windows form for sell and buy invoices and user can open 2 instance of this in one time. A button is available in this form that i showdialog() a commodity form. when user open 2 instance (one for sell And one for buy) and commodity form is showdialog(), commodity inserted in one off forms(that opened earlier).
How i can detect which of instances should be updeted?
thanks

解决方案

First, let's distinguish between a Form having a 'Parent, and a Form having an 'Owner.

In Windows Forms it is always a very bad thing to make one Form the Parent of another Form (the old MDI architecture for WinForms has its own way of handling "child" Forms of an MDIParentForm). But, a Form's 'Owner property can be set without the side-effects of setting the Form's 'Parent, as you'll see in the code below.

I'll answer your specific question here, but I want to tell you I think you need to evaluate your current plan. I'd suggest you have one Main Form ... perhaps where the user sets application preferences ? ... then have a second Form that the Main Form creates instances of to use for buy, and sell.

Creating a second instance of your Main Form will result in a design-time experience where you will see all Controls that are on the Main Form duplicated (of course) and locked on the second Form design-view. And, if your Main Form creates new objects in its Load Event, then you are going to duplicate creating those objects in the second instance which can lead to a mess.

If you show a Form modally using 'ShowDialog, you can add the optional argument to 'ShowDialog that sets the 'Owner property of the modal-shown Form:

// when you show the modal Form from inside 'currentBaseForm 
myModalForm.ShowModal(this);

// a reference to the instance of 'currentBaseForm is now available in 'myModalForm
// via the 'Owner property

// in 'myModalForm
currentBaseForm = this.Owner; 

// which form is the Owner ?
if(currentBaseForm.Name == "myBuyForm")
{

}
else
{

}


这篇关于检测父母的当前形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆