我有一个Form
,其中包含:
Control
实现了一个接口,并且可以选择-在加载表单时,只有第一个控件可见(这些Control
中的每个都有一个{{ 1}} s或聚焦自己); MouseDown
上工作(用户可以单击其中一个控件,并且跟踪栏会记住两个控件中每个控件的缩放百分比并进行自我更新以匹配当前关注的控件的缩放百分比); Select
内部。在表单加载时,第一个Control
自动聚焦。然后:
ToolStripContainer
,如果是,我:
如果:
Control
,表单会获得焦点,Control
获得关注; 问题似乎是Control
静态方法无法理解Control
控件。它在其中搜索一个集中的控件,但没有找到任何控件(尽管FindControlWithFocus
是我的ToolStripContainer
中唯一的直接子控件)。
我有一个事件处理程序附加到窗体(包括窗体)中所有控件的所有Enter事件。在此处理程序中,我使用此控件作为参数调用ToolStripContainer
,以查找最靠近具有焦点的表面(而非深层)控件。
问题在于,在使第二个孩子可见之后,Form
返回FindControlWithFocus
时,在没有首先散焦FindControlWithFocus
的情况下,单击第一个null
上的
Form
在上面标记为Control
的行上:public static Control FindControlWithFocus(Control parent)
{
if (parent.Focused)
{
return parent;
}
foreach (Control ctl in parent.Controls)
{
if (ctl.Focused)
{
return ctl;
}
Control child = FindControlWithFocus(ctl);
if (child != null)
{
return child;
}
}
return null;
}
private void Ce_FocusedControlChanged(object sender,
FocusMovedEventArgs e)
{
Control c = Utils.FindControlWithFocus(e.FocusedControl);
while (c != null && // (a)
!Utils.TypeImplements(c.GetType(), typeof(IClocksView)))
{
c = c.Parent;
}
if (c != null) // (b)
{
LastFocusedTimersView = c as IClocksView;
}
}
在上面标记为(a)
的行上:Control that got focus: {e.FocusedControl}; Surfacemost element with focus: {c};
在“输出”面板中,程序的开头有这些行(直到我开始单击):
(b)
当我单击第二个surfacemost parent that is IClocksView: {c}
时,输出为:
Control that got focus: {System.Windows.Forms.ToolStripContainer}; Surfacemost element with focus: null;
surfacemost parent that is IClocksView: null
Control that got focus: {System.Windows.Forms.ToolStripContentPanel, BorderStyle: System.Windows.Forms.BorderStyle.None}; Surfacemost element with focus: null;
surfacemost parent that is IClocksView: null
Control that got focus: {Text = "F&ull-screen" CheckState = Unchecked}; Surfacemost element with focus: null;
surfacemost parent that is IClocksView: null
Control that got focus: {cs_timed_silver.MainForm, Text: Timed Silver - new file}; Surfacemost element with focus: {Text = "F&ull-screen" CheckState = Unchecked};
Control that got focus: {cs_timed_silver.MainForm, Text: Timed Silver - new file}; Surfacemost element with focus: {System.Windows.Forms.ToolStripContentPanel, BorderStyle: System.Windows.Forms.BorderStyle.None};
Control that got focus: {cs_timed_silver.MainForm, Text: Timed Silver - new file}; Surfacemost element with focus: {System.Windows.Forms.ToolStripContainer};
Control that got focus: {cs_timed_silver.MainForm, Text: Timed Silver - new file}; Surfacemost element with focus: {cs_timed_silver.MainForm, Text: Timed Silver - new file};
Control that got focus: {cs_timed_silver.MainForm, Text: Timed Silver - new file}; Surfacemost element with focus: null;
surfacemost parent that is IClocksView: null
当我单击第一个Control
而没有首先使窗体散焦时,输出为:
Control that got focus: {System.Windows.Forms.TableLayoutPanel, BorderStyle: System.Windows.Forms.BorderStyle.None}; Surfacemost element with focus: {ClockDataGridView ID = 1};
surfacemost parent that is IClocksView: {ClockDataGridView ID = 1}
Control that got focus: {SplitterView: TimerGroupListView & SplitterView}; Surfacemost element with focus: {ClockDataGridView ID = 1};
surfacemost parent that is IClocksView: {ClockDataGridView ID = 1}
Control that got focus: {System.Windows.Forms.SplitterPanel, BorderStyle: System.Windows.Forms.BorderStyle.None}; Surfacemost element with focus: {ClockDataGridView ID = 1};
surfacemost parent that is IClocksView: {ClockDataGridView ID = 1}
Control that got focus: {SplitterView: ClockListView & ClockDataGridView}; Surfacemost element with focus: {ClockDataGridView ID = 1};
surfacemost parent that is IClocksView: {ClockDataGridView ID = 1}
Control that got focus: {System.Windows.Forms.SplitterPanel, BorderStyle: System.Windows.Forms.BorderStyle.None}; Surfacemost element with focus: {ClockDataGridView ID = 1};
surfacemost parent that is IClocksView: {ClockDataGridView ID = 1}
Control that got focus: {ClockDataGridView ID = 1}; Surfacemost element with focus: {ClockDataGridView ID = 1};
surfacemost parent that is IClocksView: {ClockDataGridView ID = 1}
Control that got focus: {System.Windows.Forms.Panel, BorderStyle: System.Windows.Forms.BorderStyle.None}; Surfacemost element with focus: {System.Windows.Forms.DataGridViewComboBoxEditingControl, Items.Count: 2};
Control that got focus: {System.Windows.Forms.Panel, BorderStyle: System.Windows.Forms.BorderStyle.None}; Surfacemost element with focus: {System.Windows.Forms.Panel, BorderStyle: System.Windows.Forms.BorderStyle.None};
Control that got focus: {System.Windows.Forms.Panel, BorderStyle: System.Windows.Forms.BorderStyle.None}; Surfacemost element with focus: {ClockDataGridView ID = 1};
surfacemost parent that is IClocksView: {ClockDataGridView ID = 1}
Control that got focus: {System.Windows.Forms.DataGridViewComboBoxEditingControl, Items.Count: 2}; Surfacemost element with focus: {System.Windows.Forms.DataGridViewComboBoxEditingControl, Items.Count: 2};
Control that got focus: {System.Windows.Forms.DataGridViewComboBoxEditingControl, Items.Count: 2}; Surfacemost element with focus: {System.Windows.Forms.Panel, BorderStyle: System.Windows.Forms.BorderStyle.None};
Control that got focus: {System.Windows.Forms.DataGridViewComboBoxEditingControl, Items.Count: 2}; Surfacemost element with focus: {ClockDataGridView ID = 1};
surfacemost parent that is IClocksView: {ClockDataGridView ID = 1}
Control that got focus: {System.Windows.Forms.Panel, BorderStyle: System.Windows.Forms.BorderStyle.None}; Surfacemost element with focus: {System.Windows.Forms.DataGridViewComboBoxEditingControl, Items.Count: 2};
Control that got focus: {System.Windows.Forms.Panel, BorderStyle: System.Windows.Forms.BorderStyle.None}; Surfacemost element with focus: {System.Windows.Forms.Panel, BorderStyle: System.Windows.Forms.BorderStyle.None};
Control that got focus: {System.Windows.Forms.Panel, BorderStyle: System.Windows.Forms.BorderStyle.None}; Surfacemost element with focus: {ClockDataGridView ID = 1};
surfacemost parent that is IClocksView: {ClockDataGridView ID = 1}
Control that got focus: {System.Windows.Forms.DataGridViewComboBoxEditingControl, Items.Count: 2}; Surfacemost element with focus: {System.Windows.Forms.DataGridViewComboBoxEditingControl, Items.Count: 2};
Control that got focus: {System.Windows.Forms.DataGridViewComboBoxEditingControl, Items.Count: 2}; Surfacemost element with focus: {System.Windows.Forms.Panel, BorderStyle: System.Windows.Forms.BorderStyle.None};
Control that got focus: {System.Windows.Forms.DataGridViewComboBoxEditingControl, Items.Count: 2}; Surfacemost element with focus: {ClockDataGridView ID = 1};
surfacemost parent that is IClocksView: {ClockDataGridView ID = 1}
问题在于,在上面的最后一行,该值为Control
。我需要它是Control that got focus: {System.Windows.Forms.TableLayoutPanel, BorderStyle: System.Windows.Forms.BorderStyle.None}; Surfacemost element with focus: null;
surfacemost parent that is IClocksView: null
Control that got focus: {SplitterView: ClockListView & ClockDataGridView}; Surfacemost element with focus: null;
surfacemost parent that is IClocksView: null
Control that got focus: {System.Windows.Forms.SplitterPanel, BorderStyle: System.Windows.Forms.BorderStyle.None}; Surfacemost element with focus: null;
surfacemost parent that is IClocksView: null
Control that got focus: {ClockListView ID = 1}; Surfacemost element with focus: null;
surfacemost parent that is IClocksView: null
Control that got focus: {cs_timed_silver.ClockFlowLayoutPanel, BorderStyle: System.Windows.Forms.BorderStyle.None}; Surfacemost element with focus: null;
surfacemost parent that is IClocksView: null
。