C#参考 [英] C# reference
问题描述
我有两种形式.
一个是主窗体form1,在其中我打开了另一个窗体form2.我在form1中有一个对象obj1,该对象在form1中的事件下已更改.我想在form2中查看obj1中的更改.
我已经通过obj1作为form2中的引用.没用能否请您提出我如何使用此概念的方法.
谢谢.
I have two forms.
one is the main form form1 ,in which I open another form form2. I have an object obj1 in form1 that is changed under an event in the form1. I want to see the change in obj1 in form2.
I have passed obj1 as a reference in form2. It don''t work. can you please suggest the way how I can use this concept.
Thank You.
推荐答案
您可以使用委托.
您将在您的form1中添加一个委托签名:
You can use delegates.
you will add a delegate signature to your form1:
public delegate void Passchangeobject(MyObject obj);
然后在您的事件下更改对象时,您将使用将添加到form2中的函数来调用委托.
and then when your object is changed under your event, you call your delegate with the function that you will add in your form2
Passchangeobject delchange = new Passchangeobject(f2.GetChanges);
//f2 is your form2
delchange(ms);
并且应该将GetChanges函数添加到form2
And you should add GetChanges function to your form2
public void GetChanges(MyObject ms2)
{
textBox1.Text = ms2.GetName();
// you do here whatever you want
}
它应该可以工作
it should work
这篇关于C#参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!