[英]Call MdiParent Method on Ctrl+S of MdiChild form
I have an mdiParent form in which i have a save method. 我有一个mdiParent表单,其中有一个保存方法。 Now I want that whenever i press Ctrl+S in my activeMdiChild form the parents Save method should get called.
现在,我希望每当我在activeMdiChild表单中按Ctrl + S时,都应调用父级Save方法。 Any hint or suggestion is welcome.
任何提示或建议都欢迎。
Thanx in advance. 提前感谢。
Handle the KeyDown
event in activeMdiChild
. 处理
activeMdiChild
的KeyDown
事件。
private void activeMdiChild _KeyDown(object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.S && e.Modifiers == Keys.Control)
{
dynamic parent = this.Parent;
parent.Save();
}
}
EDIT: as mentioned in comments, mdiParent.Save
must be public 编辑:如评论中所述,
mdiParent.Save
必须是公共的
If you have a MenuStrip in the MDI Parent Form, then you don't need to do anything else than adding a menu with the Ctrl + S shortcut. 如果您在MDI父窗体中有一个MenuStrip,则除了使用Ctrl + S快捷键添加菜单外,您无需执行其他任何操作。 Then if the user press those combination even in child form, the code for that menu of parent will execute.
然后,如果用户甚至按子形式按下这些组合,则将执行父菜单的代码。
But if you don't want to add a menu, then you can override ProcessCmdKey
in the "MDI Parent Form" and check if the Ctrl + S combination was pressed, then perform the desired action: 但是,如果您不想添加菜单,则可以覆盖“ MDI父窗体”中的
ProcessCmdKey
并检查是否按下了Ctrl + S组合键,然后执行所需的操作:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.S))
{
MessageBox.Show("Handled in main form.");
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
The
ProcessCmdKey
method overrides the baseContainerControl.ProcessCmdKey
implementation to provide additional handling of main menu command keys and MDI accelerators.ProcessCmdKey
方法重写基本的ContainerControl.ProcessCmdKey
实现,以提供对主菜单命令键和MDI加速器的附加处理。
One of possible solutions: 可能的解决方案之一:
1. Create an interface 1.创建一个界面
ISave { void Save(); }
2. Let your parent form inherit interface 2.让您的父表单继承接口
3. In your child form check for ctrl + s if so do 3.在您的子窗体中检查ctrl + s(如果这样做)
ISave saver = this.Parent as ISave; if(saver != null) saver.Save();
That's all 就这样
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.