I keep a dictionary whose keys are the TabPages and the values are HashSets of unvalidated controls within the corresponding tab. This is easily done by subscribing to all the validating and validated events of the controls in each tab. Finally, in OK_BUtton_Click, if ValidateChildren fails, I know one of the hashsets will be none empty and I simply jump to the first unvalidated tab (only if the currently selected tab doesn't have any error itself). Dictionary<TabPage, HashSet<Control>> _tabControls = new Dictionary<TabPage, HashSet<Control>>(); public OptionsForm() { InitializeComponent(); RegisterToValidationEvents(); } private void RegisterToValidationEvents() { foreach (TabPage tab in this.OptionTabs.TabPages) { var tabControlList = new HashSet<Control>(); _tabControls[tab] = tabControlList; foreach (Control control in tab.Controls) { var capturedControl = control; //this is necessary control.Validating += (sender, e) => tabControlList.Add(capturedControl); control.Validated += (sender, e) => tabControlList.Remove(capturedControl); } } } private void Ok_Button_Click(object sender, EventArgs e) { if (this.ValidateChildren()) { _settings.Save(); this.Close(); } else { var unvalidatedTabs = _tabControls.Where(kvp => kvp.Value.Count != 0) .Select(kvp => kvp.Key); TabPage firstUnvalidated = unvalidatedTabs.FirstOrDefault(); if (firstUnvalidated != null && !unvalidatedTabs.Contains(OptionTabs.SelectedTab)) OptionTabs.SelectedTab = firstUnvalidated; } }我认为这很不错! 这篇关于WinForms TabControl验证:切换到验证失败的选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-23 22:49