问题描述
我试图在新的win形式中使用第1页对象调用page1到page2的方法,但问题是该方法的功能是iam调用在page1中正常工作但是进入第2页它不起作用。任何人都可以帮我解决这个问题...... !!
I am trying to call a method of page1 to page2 using page 1 object in a new win form but the problem is the functionality of the method is which iam calling is working fine in page1 but coming into page 2 its not working.can anyone help me out this...!!
推荐答案
public void Foo(string text)
{
myLabel.Text = text;
}
如果你从Page1调用它然后它可以工作:
If you call it from Page1 then it works:
Foo("hello");
但要从Page2调用它,您需要访问正在运行的Page1的实际实例,这听起来像你这样做:
But to call it from Page2, you need to access the actual instance of Page1 that is running, and it sounds like you are doing this:
Page1 p1 = new Page1();
p1.Foo("Goodbye");
赢了不行,因为它正在创建一个新的Page1和changign而不是改变你可以看到的实例。它就像一辆汽车:如果你把你的手机放在我的汽车的手套箱里,你不要期望在打开汽车的手套箱时找到它,是吗? :笑:
看看这个: [] =它可能会让你更简单。
That won't work, because it is creating a new Page1 and changign that rather than changing the instance you can see. It's like a car: if you put your mobile phone in teh glove box of my car, You don;t expect to find it when you open teh glove box of your car, do you? :laugh:
Have a look at this: Transferring information between two forms, Part 2: Child to Parent[^] = it might make it simpler for you.
这篇关于我试图使用第1页对象调用page1到page2的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!