【问题标题】:MDI .NET Forms Enter Key Event Being Sent to Other FormMDI .NET 表单输入关键事件被发送到其他表单
【发布时间】:2011-02-04 12:06:27
【问题描述】:

当文本框获得焦点时,当在其中一个表单中按下回车键时,我在 MDI 表单中出现了一种奇怪的行为。

基本上,父窗体以某种方式将密钥发送到另一个子 MDI 窗体,而不是将其发送到按下 Enter 键的 TextBox 的窗体。

为了调试,我在每个窗体(父窗体和 2 个子窗体)上将 KeyPreview 设置为 true,并开始监听四个键事件(Preview、Up、Down、Press),如果一个按下普通键,如果输入在有问题的文本框中。

C1

如果按下任何其他键: C1.KeyDown -> P.KeyDown -> C1.KeyPress -> P.KeyPress -> C1.KeyUp -> P.KeyUp 结果,文本出现在它应该在的C1的TextBox中。

如果按下 Enter: C2.KeyUp -> P.KeyUp 因此,C2 是专注的。

为什么?!?!?!?!?!? :P

作为一个绝望的尝试,我在每个表单中都覆盖了 ProcessCmdKey 以发现发生了什么,但输入键甚至没有通过那里。

不知道是否重要,但这是我用来实例化C2表单的代码(在C1之前制作的)和实例化C1的代码...

注意:C1 表单被实例化并由父表单显示为对 C2 表单触发的自定义事件的响应...


C2:

    private void CalendarForm_Load(object sender, EventArgs e)
    {
        // Loop through all of the form's controls looking
        // for the control of type MdiClient.
        foreach (Control ctl in this.Controls)
        {
            if (ctl is MdiClient)
            {
                // Set the BackColor of the MdiClient control.
                ((MdiClient)ctl).BackColor = this.BackColor;
            }
        }

        // Shows the background form
        this._calendarContents.MdiParent = this;
        this._calendarContents.Show();
        //this._calendarContents.Dock = DockStyle.Fill;
    }

C1:

    private FloatingEventDetails _floatingEvent = null;
    private void _calendarContents_ElementDoubleClicked(object sender, ElementDoubleClickedEventArgs e)
    {
        // Checks if the form is not open
        if (this._floatingEvent == null)
        {
            // Opens the form
            this._floatingEvent = new FloatingEventDetails();
            this._floatingEvent.ModuleForm = this;
            this._floatingEvent.ListOfImages = this.ElementTypeImageList;
            this._floatingEvent.MdiParent = this;

            // Begins to listen for Focus and LostFocus events
            this._floatingEvent.GotFocus += new EventHandler(_floatingEvent_GotFocus);
            this._floatingEvent.LostFocus += new EventHandler(_floatingEvent_LostFocus);
        }
        // Displays the form
        this._floatingEvent.Show();
        this._floatingEvent.BringToFront();
        this._floatingEvent.Focus();

        // Loads the Event in the details form
        this._floatingEvent.EventId = e.EventId;
    }

【问题讨论】:

  • 是你的文本框多行...
  • 我有多个文本框(其中一个是多行),它们都存在相同的问题。好想捕捉回车键事件……
  • 另外,问题真的是keypreview中的key被给到了其他的form...
  • 有人吗?拜托,我真的需要这个...
  • 创建一个展示此行为的小项目并将其发布到文件共享服务或粘贴箱。

标签: c# .net winforms mdi


【解决方案1】:

这个问题真的是什么。该应用程序非常复杂,因为表单被加载到不同的应用程序域中,并且它使用自定义 DLL 执行与数据库的异步连接并处理表单中的一些事情。

无论如何,第二个表单是一种“对话框”,我希望它漂浮在 MDI 表单之上。但是现在,这个“对话框表单”被设置为不再使用MDI,从而成为一个完全分离的表单。这样,它就可以工作了......

【讨论】:

  • 我不太清楚您是否仍在寻找解决此问题的方法。如果您对解决方法感到满意,我不会费心尝试调试代码并解释行为。如果你关心的话,发布一个像 Hans 提到的 repro 项目会非常有帮助。显然,repro 项目需要包含您在当前项目中使用的任何基 Form 类。但我怀疑如果你想要一个对话框,你应该使用ShowDialog 方法来显示表单。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多