MDI .NET 表单输入发送到其他表单的关键事件
当文本框聚焦时在其中一个表单中按下回车键时,我在 MDI 表单中出现了奇怪的行为。
基本上,父窗体以某种方式将键发送到另一个子 MDI 窗体,而不是将其发送到按下 Enter 键的 TextBox 所在的窗体。
为了调试,我在每个表单(父表单和 2 个子表单)上将 KeyPreview 设置为 true,并开始监听四个按键事件(Preview、Up、Down、Press),如果使用普通键,则其行为会有所不同按下并且输入是否在有问题的文本框中。
C1 <- 带文本框的 MDI 子级; C2 <- 其他 MDI 子项; P <- MDI Parent
如果按下任何其他键: C1.KeyDown-> P.KeyDown-> C1.按键-> 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;
}
I am having a weird behaviour in a MDI form when the enter key is pressed in one of the forms when a Textbox is focused.
Basically, the parent form somehow sends the key to the other child MDI form, instead of sending it to the form where the TextBox in which the enter key was pressed.
To Debug, I set the KeyPreview to true on every form (the parent and the 2 childs) and begun to listen to the four key events (Preview, Up, Down, Press) and the behaviour of this is different if a normal key is pressed and if enter is in the problematic Textboxes.
C1 <- MDI Child with the TextBox; C2 <- Other MDI Child; P <- MDI Parent
If any other key is pressed:
C1.KeyDown -> P.KeyDown -> C1.KeyPress -> P.KeyPress -> C1.KeyUp -> P.KeyUp
As a result, the text appears in the TextBox of C1 where it is supposed to be.
If Enter in pressed:
C2.KeyUp -> P.KeyUp
As a result, the C2 is Focused.
WHY?!?!?!?!?!? :P
As a desperate attempt, I overrode the ProcessCmdKey in every form to discover what is going on, but the enter key press don't even pass by there.
I don't know if it is important, but this is the code I used to instanciate the C2 form (which is made before C1) and the code to instanciate the C1...
NOTE: C1 form is instanciated and shown by the parent form as a response to a custom event fired by the C2 form...
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题实在是无所谓。该应用程序非常复杂,因为表单加载在不同的应用程序域中,并且它使用自定义 DLL 执行与数据库的异步连接并处理表单中的多项内容。
无论如何,第二个窗体有点像“对话框”,我希望它浮动在 MDI 窗体之上。但现在,这个“对话框窗体”被设置为不再使用MDI,从而成为一个完全独立的窗体。这样,它就有效了......
This problem is really whatever. The application is really complex, since the forms are loaded in a different application domain and it uses a custom DLL that performs async connections to the database and handles several things in the forms.
Regardless, the second form was sort of a "Dialog Box" that I wanted to have floating above the MDI form. But now, this "Dialog Form" was set not to use the MDI anymore and thus became a fully separated form. This way, it works....