ToolStripContainer的焦点/选择问题

时间:2019-03-03 16:04:55

标签: c# winforms focus .net-4.6.1

我有一个Form,其中包含:

  • 至少有2个Control实现了一个接口,并且可以选择-在加载表单时,只有第一个控件可见(这些Control中的每个都有一个{{ 1}} s或聚焦自己);
  • 包含自定义ToolStripItem的工具条,例如用于缩放的滑块(跟踪栏);
  • 缩放跟踪栏可在实现该界面的当前关注的MouseDown上工作(用户可以单击其中一个控件,并且跟踪栏会记住两个控件中每个控件的缩放百分比并进行自我更新以匹配当前关注的控件的缩放百分比);
  • 上面列举的所有内容都在Select内部。

在表单加载时,第一个Control自动聚焦。然后:

  1. 我使第二个控件可见,并且
  2. 我点击第二个ToolStripContainer
  3. 获得关注。

如果是,我:

  1. 再次单击第一个控件,
  2. 它没有获得焦点。

如果:

  1. 表单失去焦点,然后
  2. 通过单击第一个Control,表单会获得焦点,
  3. 第一个Control获得关注;
  4. 然后,第二个控件将重点放在单击它上,而无需使表单失去焦点。

问题似乎是Control静态方法无法理解Control控件。它在其中搜索一个集中的控件,但没有找到任何控件(尽管FindControlWithFocus是我的ToolStripContainer中唯一的直接子控件)。

代码

我有一个事件处理程序附加到窗体(包括窗体)中所有控件的所有Enter事件。在此处理程序中,我使用此控件作为参数调用ToolStripContainer,以查找最靠近具有焦点的表面(而非深层)控件。

问题在于,在使第二个孩子可见之后,Form返回FindControlWithFocus时,在没有首先散焦FindControlWithFocus的情况下,单击第一个null上的

Form

跟踪点

  1. 在上面标记为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; } }

  2. 在上面标记为(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

0 个答案:

没有答案