如何从子窗体更新MDI父窗体中的控件?
我查看了一些类似的问题,但我没有真正得到答案,所以我再次询问,希望有人能解释一下。
情况:
我有一个 MDI 表单,其中有一些菜单和状态栏之类的东西。更改状态栏文本并对父表单执行其他操作的唯一方法是将其称为静态吗?或者如果没有,您能否给出一个更新子表单中父表单中存在的(例如)状态栏的示例?
谢谢!
I looked to some similar questions but I didn't really get my answer, so I ask again hopefuly someone can explain it.
The situation:
I have a MDI form that has some menues and a status bar and stuff like that. Is the only way of altering text for status bar and doing other things to the parent form is to call it as static
? Or if not, can you please give an example for updating (for example) status bar that exist in parent form within the child forms?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要使子表单将父表单实例作为构造函数参数。
子级可以将此参数保存到私有字段,然后随意与父级交互。
为了实现最佳设计,您应该通过父级实现的接口从子级中抽象出父级,其中包含执行子级所需的方法和属性。然后孩子们应该只与这个界面交互。
You need to make the child forms take a parent form instance as a constructor parameter.
The children can save this parameter to a private field, then interact with the parent at will later.
For optimal design, you should abstract the parent from the child through an interface implemented by the parent, containing methods and properties that do what the children need. The children should then only interact with this interface.
Form 类已公开属性 MdiParent 确保父表单 IsMdiContainer 属性进行相应设置。
The Form class already exposes a property MdiParent ensure the parent forms IsMdiContainer property is set accordingly.
另一种选择是使用事件(您可以将这些事件构建到基类中并让所有子表单继承它):
Another option is to use events (you can build these events into a base class and let all your child forms inherit from it):