I am designing a Windows form application wherein I need to call, from Form A, a click event for a command button on Form B. Form B needs to remain hidden at all times. It was simple to do this in Visual Basic, I'd just call it like this: "FormName.ControlName.Event/Method".
This isn't working for me now in C#, Visual Studio 8. Can anyone help?
All you need to do is give Form A a reference to Form B. Presumably there is some startup code in your application that is aware of both forms. If so, you could have code like this:
// starting up
FormB b = new FormB();
b.Visible = false;
FormA a = new FormA();
a.FormBInstance = b; // you would have to add the 'FormBInstance' property to Form A class yourself
a.Show();
// Now, if Form A needs to do something with FormB, it just needs to use the FormBInstance property.
Make sense?
You can't raise events from a different class directly, they have special access level that means you can only subscribe/unsubscribe. You would need method on formB that can raise the event.
听起来很古怪,但是引用了Form B的实例,并公开了Button控件:
b.myButton.PerformClick();
You should create a public method on FormB that does what you need. This way, you get rid of the need to directly call the click event handler, which is not a great thing to do.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.