如何从另一种形式获得价值 [英] How to get value from another form

查看:64
本文介绍了如何从另一种形式获得价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。



我有一个C#应用程序,其中有两种形式。 ParentForm包含几个控件,如TextBoxes,DataGridView等。现在我想在ChildForm上获取这些控件的值。



在ParentForm中,我将此控件声明为Public和In ChildForm,我创建ParentForm的对象作为ParentForm uxParent = new ParentForm()。



在ParentForm中有一个有5行的DataGridView控件。现在我想在ChildForm中获取这些行的值,但rowcount显示为0.



需要建议

解决方案

真的,不要这样做。

第一个问题是

 ParentForm uxParent =  new  ParentForm(); 

不允许您访问实际的父级 - new 关键字是一个线索这里 - 它生成父表单的新 实例 。如果您的父表单在其构造函数中自动创建子表单的实例,您的程序将立即崩溃,因为它将自行循环。要做这样的事情,yoiu需要访问父表单的实际实例。



虽然可以使用(在他的解决方案中作为Kishor syas),但它是一个将它用于这样的事情是非常糟糕的,因为它将两种形式的设计联系在一起,这样没有另一种形式就不能存在,并且你不能在不考虑任何可能的影响的情况下对其中一种进行更改。这是非常糟糕的做法,违背了OOP的原则。这就是为什么默认情况下所有表单控件都被声明为私有的!



正确地完成工作并不困难 - 您所要做的就是创建属性子表单提供数据,在子表单中创建一个表示需要数据的事件,并在父表中处理事件:

子表单:

< pre lang =c#> public string MyProperty
{
get { return myTextBox.Text; }
set {myTextBox.Text = value ; }
}
/// < 摘要 >
/// 指示DataRequired
/// < / summary >
public event EventHandler ReadyForData;
/// < 摘要 >
/// 调用以向订阅者发出DataRequired
/// < / summary >
/// < param name =e > < / param >
受保护 虚拟 void OnReadyForData(EventArgs e)
{
EventHandler eh = ReadyForData;
if (eh!= null
{
eh( ,e);
}
}

要发出需要数据的信号,只需调用方法:

 OnReadyForData ( null ); 



在父表单中,在构造子实例时添加处理程序:

 frmChild myChildForm =  new  frmChild(); 
...你的另一个孩子初始化
myChildForm.ReadyForData + = new EventHandler(myChildForm_ReadyForData);
}

void myChildForm_ReadyForData( object sender,EventArgs e)
{
frmChild child = sender as frmChild;
if (child!= null
{
child。 MyProperty = 这是新数据;
}
}


Hello There

I have a C# application in which there are two forms. ParentForm contains several controls like TextBoxes,DataGridView etc. Now I want to fetch the values of these controls on ChildForm.

In ParentForm, I declared this controls as Public and In ChildForm, I create object of ParentForm as ParentForm uxParent = new ParentForm().

There is a DataGridView Control in ParentForm which has 5 rows. Now I want to get the value of these rows in ChildForm but the rowcount display as 0.

Need Suggestion

解决方案

Really, don''t do that.
The first problem is that

ParentForm uxParent = new ParentForm();

Does not give you access to the actual parent - the new keyword is a clue here - it generates a new instance of the parent form. If your parent form automatically creates an instance of the child form in it''s constructor, your program will immediately crash because it will loop itself. To do anything like that, yoiu need access to teh actual instance of the parent form.

While that is available (as Kishor syas in his solution), it is a very bad idea to use it for something like this, because it ties the design of the two forms together so that neither one can exist without the other, and you cannot make changes to one without considering any possible effects on the other. This is very bad practice, and against the principles of OOP. This is why all form controls are declared private by default!

It''s not difficult to do the job properly - all you have to do is create properties in the child form to supply the data to, create an event in the child form which says it needs the data, and handle the event in the parent:
Child form:

public string MyProperty
    {
    get { return myTextBox.Text; }
    set { myTextBox.Text = value; }
    }
/// <summary>
/// Event to indicate DataRequired
/// </summary>
public event EventHandler ReadyForData;
/// <summary>
/// Called to signal to subscribers that DataRequired
/// </summary>
/// <param name="e"></param>
protected virtual void OnReadyForData(EventArgs e)
    {
    EventHandler eh = ReadyForData;
    if (eh != null)
        {
        eh(this, e);
        }
    }

To signal you need the data, just call the method:

OnReadyForData(null);


In the parent form, add a handler when you construct the child instance:

    frmChild myChildForm = new frmChild();
    ... Do your other child initialization
    myChildForm.ReadyForData += new EventHandler(myChildForm_ReadyForData);
    }

void myChildForm_ReadyForData(object sender, EventArgs e)
    {
    frmChild child = sender as frmChild;
    if (child != null)
        {
        child.MyProperty = "Here is the new data";
        }
    }


这篇关于如何从另一种形式获得价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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