在C#中从隐藏表单调用控件

时间:2021-08-05 15:50:39

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".

我正在设计一个Windows表单应用程序,其中我需要从表单A调用表单B上的命令按钮的单击事件。表单B需要始终保持隐藏状态。在Visual Basic中执行此操作非常简单,我只需将其命名为:“FormName.ControlName.Event / Method”。

This isn't working for me now in C#, Visual Studio 8. Can anyone help?

现在这在C#,Visual Studio 8中对我不起作用。任何人都可以帮忙吗?

4 个解决方案

#1


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:

您需要做的就是给表单A一个表单B的引用。据推测,您的应用程序中有一些启动代码可以识别这两种形式。如果是这样,您可以使用以下代码:

// 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?

#2


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

听起来很古怪,但是引用了Form B的实例并且公开了Button控件:

b.myButton.PerformClick();

#3


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.

您应该在FormB上创建一个满足您需要的公共方法。这样,您就不必直接调用click事件处理程序,这不是一件好事。

#4


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.

您不能直接从其他类引发事件,它们具有特殊的访问级别,这意味着您只能订阅/取消订阅。你需要formB上可以引发事件的方法。

#1


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:

您需要做的就是给表单A一个表单B的引用。据推测,您的应用程序中有一些启动代码可以识别这两种形式。如果是这样,您可以使用以下代码:

// 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?

#2


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

听起来很古怪,但是引用了Form B的实例并且公开了Button控件:

b.myButton.PerformClick();

#3


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.

您应该在FormB上创建一个满足您需要的公共方法。这样,您就不必直接调用click事件处理程序,这不是一件好事。

#4


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.

您不能直接从其他类引发事件,它们具有特殊的访问级别,这意味着您只能订阅/取消订阅。你需要formB上可以引发事件的方法。