0

我有一个包含子窗体对象的面板的应用程序。当我单击其中一个子窗体时,它会显示在前面。我想知道现在哪个在前面...我查看了事件列表,但找不到合适的事件形式我的目的:(

这些方法不起作用:

    protected void OpenedFileForm_Enter(object sender, EventArgs e)
    {
        MessageBox.Show("enter");
    }

    protected void OpenedFileForm_Click(object sender, EventArgs e)
    {
        MessageBox.Show("click");
    }

    protected void OpenedFileForm_Activated(object sender, EventArgs e)
    {
        MessageBox.Show("activated");
    }

    protected void OpenedFileForm_MouseClick(object sender, MouseEventArgs e)
    {
       MessageBox.Show("mouse click");
    }

    protected void OpenedFileForm_Shown(object sender, EventArgs e)
    {
        MessageBox.Show("shown");
    }

在此处输入图像描述

        OpenFileDialog openFile1 = new OpenFileDialog();

        openFile1.DefaultExt = "*.txt";
        openFile1.Filter = "TXT Files|*.txt|RTF Files|*.rtf";

        if (openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
           openFile1.FileName.Length > 0)
        {

            switch (Path.GetExtension(openFile1.FileName))
            {

                case ".txt":
                    txtForm childTXT = new txtForm();
                    this.childForms.Add(childTXT);
                    childTXT.Parent = this.mainPanel;              
                    childTXT.richTextBox1.LoadFile(openFile1.FileName, RichTextBoxStreamType.PlainText);                                               
                    childTXT.Show();
                    break;
            }


        }
4

3 回答 3

1

你试过Form.Activated事件吗?

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.activated(v=vs.80).aspx

编辑:

如果您在 MDI 应用程序中,则可能需要使用MdiChildActivate

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.mdichildactivate.aspx

于 2012-11-25T15:04:27.253 回答
1

仅当您将 Form.TopLevel 属性设置为 false 时,此代码才能工作。这使它变成了一个子控件,与 UserControl 几乎没有区别。

This has many side-effects, for one there is no notion of "front" anymore. The Z-order of child controls is determined by their position in their parent's Controls collection. And it affects the events it fires, Activated and Deactivated will never fire. Furthermore, the Form class was designed to be a container control, it doesn't like taking the focus itself. Its child controls get the focus, the Form class doesn't have any use for focus. Which is why the Enter, Click and MouseClick events don't fire, they are events that require focus.

Long story short, what you are trying to do doesn't make a wholeheckofalot of sense. If it is strictly the Z-order you want to fix then write an event handler for the MouseDown event:

    void OpenedFileForm_MouseDown(object sender, MouseEventArgs e) {
        var frm = (Form)sender;
        frm.BringToFront();
    }

您可以添加 frm.Select() 以触发 Enter 事件,但只有在表单本身不包含任何可聚焦控件时才这样做。请注意,有证据表明您没有在代码中正确分配事件。Shown 事件确实会触发。将 FormBorderStyle 设置为 None 也很重要,标题栏不能再指示激活状态。

于 2012-11-25T16:04:00.747 回答
0

好的,我明白了!谢谢大家的帮助。你给了我一个提示,让我考虑一下我奇怪的 MDI 想法的公平性,其中 Panel 是其他表单的父级。我删除了包含 Panel 的 SplitContainer,只是做了标准的 MDI 应用程序,其中 Forms 是主 Form 的 MDIChildren。childTXT.MdiParent = 这个;

于 2012-11-25T15:48:06.730 回答