在Winform应用程序中获取Active MDI Child表单 [英] Get Active MDI Child form at winform app

查看:188
本文介绍了在Winform应用程序中获取Active MDI Child表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在运行时创建mdichild表单.我正在保存mdichild表单的内容.只可以使用一种子形式.当出现多个子窗体时,我无法找到激活了哪个mdi子窗体.

我需要获取活跃的MDI子表格.

请注意:
Mainform.activemdichild返回null
我不知道为什么.

请帮我...


这是我的示例代码:Parent&下面给出的子表单代码示例...

Hi
I am creating mdichild form at run time. I am saving contents of mdichild form. It is OK for only one child form. When more than one child form occur, I could not find out which mdi child is activated.

I need to get the active mdi child form.

note that:
Mainform.activemdichild return null
I could not find out why.

Plz help me...


Here is my sample code: Both Parent & child form code sample given below...

public partial class Main:Form   {        
MDIChildForm childForm;
private ShowNewForm()
{
            childForm = new MDIChildForm();
            childForm.MdiParent = this;
            childForm.Display();
}

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case (Keys.Control|Keys.S):
                    childForm = (MDIChildForm)this.ActiveMdiChild;
                    SaveForm();
                    this.Focus();
                    return true;
                default:
                    return false;
            }
        }

private void SaveForm()
{
..........
..........
}
}


public partial class MDIChildForm: Form
{
public MDIChildForm()
{

}
public void Display()
{
            Width = 100;
            Height = 100;
            Left = 0;
            Top = 0;
            Text = "";
            Name = GenerateId();
            BackColor = Color.White;
            AutoScroll = true;
            MaximizeBox = false;
            
            Graphics g = this.CreateGraphics();
            Pen pen = new Pen(Color.Blue, 2);
            g.Dispose();
            this.Focus();
}
}

推荐答案

当您说"Mainform.activemdichild返回null"是Mainform在设计时是表单的名称,还是在运行时是表单的实例?


尝试在父表单事件处理程序中使用this.ActiveMdiChild.
When you say "Mainform.activemdichild return null" is Mainform the name of your form at design time, or an instance of your form at runtime?


Try using this.ActiveMdiChild instead within your parent form event handler.


MDIChildFormDisplay()方法中,将this.Focus()更改为this.Show(),一切都会好起来.
In the Display() method of your MDIChildForm, change this.Focus() to this.Show() and all will be well.


这很奇怪.

您是否可能忘记设置子窗体的MDIParent属性?

发表评论后:

我已经使用您的ProcessCmdKey代码创建了一个测试应用程序,但无法重现您遇到的问题.即使使用ActivateMDIChild(null)也无法取消激活当前活动的表单.

没有看到更多的代码,我很困惑.

如果您决定显示更多代码,请通过编辑原始问题并将其添加到此处来完成,只要您记得将其包含在< pre>中,即可获得格式化和着色的好处. 您的代码在这里</pre>标签.
That is odd.

Is it possible that you are forgetting to set the MDIParent property of your child forms?

After your comments:

I have created a test application, using your ProcessCmdKey code, and I cannot reproduce the problem that you are having. Even using ActivateMDIChild(null) fails to de-activate the currently active form.

Without seeing more of your code I am stumped.

If you do decide to show more code please do so by editing your original question and adding it there, so that you get the benefits of formatting and colourization, providing that you remember to enclose it in <pre> your code here </pre> tags.


这篇关于在Winform应用程序中获取Active MDI Child表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆