protected override void OnKeyDown(KeyEventArgs e) { ignoreFocusChange = true; if (e.KeyCode == Keys.Return && Focused) { var args = new CancelEventArgs(); ValueEntered?.Invoke(this, args); if (!args.Cancel) { ForeColor = SystemColors.ControlText; focusControl.Focus(); //because Microsoft forgot to put in Unfocus() smh suggestionsDropDown.Hide(); //because OnLostFocus won't get called e.SuppressKeyPress = true; } else { ForeColor = Color.Red; } } else { base.OnKeyDown(e); } ignoreFocusChange = false; }
protected override void OnLostFocus(EventArgs e) { base.OnLostFocus(e); if (ignoreFocusChange) { return; } Form parentForm = this.FindForm(); if (parentForm != null && parentForm.ContainsFocus) //another Control inside the parent form got focused, we can be very sure that this was intentional { var args = new CancelEventArgs(); ValueEntered?.Invoke(this, args); if (args.Cancel) { ForeColor = Color.Red; //mark the value red to indicate it's invalid } else { ForeColor = SystemColors.ControlText; } } suggestionsDropDown.Hide(); }
//-------------------------------------------------------------------------------------------------- void _OnSourceUpdated(object sender, DataTransferEventArgs eventArgs) { if (eventArgs.TargetObject == ValueEditBox) { IsInKeyboardMode = false; ValueEntered?.Invoke(this, Value); } }
private void SuggestionsDropDown_ItemSelected(object sender, EventArgs e) { Text = suggestionsDropDown.SelectedSuggestion; var args = new CancelEventArgs(); ValueEntered?.Invoke(this, args); if (args.Cancel) { ForeColor = Color.Red; //mark the value red to indicate it's invalid } else { ForeColor = SystemColors.ControlText; } ignoreFocusChange = true; focusControl.Focus(); //because Microsoft forgot to put in Unfocus() smh suggestionsDropDown.Hide(); //because OnLostFocus won't get called ignoreFocusChange = false; }