使用 C# 在 WinForm 中的 MDI 子级之间传递值

发布于 2024-08-24 21:23:24 字数 374 浏览 10 评论 0原文

我有一个 MDI 父级,其中包含一个 MenuStrip。当我单击其中一个菜单时,会同时显示两个子窗体。

我的一个 ChildForm 上有一个 TextBox 和一个 Send Button 。当我在该文本框中输入内容并单击发送按钮时,我需要在第二个子表单的文本框中显示该值。

我所做的是,我在第二个子表单中编写了一个公共函数,并尝试通过在“发送按钮”单击事件上创建第二个表单的对象来调用它。当我在该公共函数中放置断点时,我发现单击“发送”按钮时控制流经该公共函数。但不显示传递的值。而且,我知道这不是标准方法。

有任何示例脚本可以帮助吗?谢谢。

I have an MDI Parent, containing a MenuStrip. When I click on one of the Menu, two Child Forms are displayed simultaneoulsy.

I have a TextBox and a Send Button on one of my ChildForm. When I type-in something in that TextBox and Click the Send Button, I need to show that value in the TextBox of my Second Child Form.

What I had done is, I wrote a Public Function in the Second Child Form and tried to invoke it by creating an object of Second Form, on the Send Button click event. When I put break points, in that Public Function, I find that the control is flowing through that Public function on cliking the Send button. But the passed value is not displayed. And, I know that is not the standard way to do that.

Any sample script for help? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴情 2024-08-31 21:23:24

我可能误读了您的问题,但看来在您的“发送”按钮的 Click 事件中,您正在创建 SecondForm 的新实例并调用其函数。如果您在此处创建窗体的新实例,则它与 MDI 父窗体中已存在的窗体实例不同(这就是为什么似乎没有发生任何事情的原因)。

您需要做的是获取对 MDI 父窗体中已有的 SecondForm 实例的引用,并调用其公共方法。您可以通过父表单的 MdiChildren 集合获取对第二个表单的引用,如下所示:

SecondForm f2 = (SecondForm)this.MdiChildren[1]; // second form in collection
f2.PublicMethod();

I may be misreading your question, but it appears that in your Send button's Click event you're creating a new instance of SecondForm and calling its function. If you're creating a new instance of the form here, then it's not the same instance of the form that is already sitting in your MDI parent form (which is why nothing appears to be happening).

What you need to do is get a reference to the instance of SecondForm that is already in your MDI parent form, and call its public method. You can get a reference to the second form via the parent form's MdiChildren collection, like so:

SecondForm f2 = (SecondForm)this.MdiChildren[1]; // second form in collection
f2.PublicMethod();
我最亲爱的 2024-08-31 21:23:24

感谢您的回复。

我尝试了您的代码,但出现错误:索引超出了数组的范围。

我稍微更改了代码,使其正常工作,如下所示:

SecondForm f2= (SecondForm)this.MdiParent.MdiChildren[1];
        f2.PublicMethod(some_value_to_pass);

感谢您的帮助。
:-)

Thanks for the reply.

I tried your code, but was giving an error : Index was outside the bounds of the array.

I changed the code slightly, to make it working, as follows:

SecondForm f2= (SecondForm)this.MdiParent.MdiChildren[1];
        f2.PublicMethod(some_value_to_pass);

Thanks for the help.
:-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文