使用 C# 在 WinForm 中的 MDI 子级之间传递值
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可能误读了您的问题,但看来在您的“发送”按钮的 Click 事件中,您正在创建
SecondForm
的新实例并调用其函数。如果您在此处创建窗体的新实例,则它与 MDI 父窗体中已存在的窗体实例不同(这就是为什么似乎没有发生任何事情的原因)。您需要做的是获取对 MDI 父窗体中已有的
SecondForm
实例的引用,并调用其公共方法。您可以通过父表单的MdiChildren
集合获取对第二个表单的引用,如下所示: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'sMdiChildren
collection, like so:感谢您的回复。
我尝试了您的代码,但出现错误:索引超出了数组的范围。
我稍微更改了代码,使其正常工作,如下所示:
感谢您的帮助。
:-)
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:
Thanks for the help.
:-)