views:

294

answers:

3

Hello.

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?

+2  A: 

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?

skb
A: 

Sounds pretty wacky, but with a reference to the instance of Form B and the Button control being made public:

b.myButton.PerformClick();
frou
A: 

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.

TheSean