【发布时间】: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...
-
有人吗?拜托,我真的需要这个...
-
创建一个展示此行为的小项目并将其发布到文件共享服务或粘贴箱。