//-------------------------------------------------------------------------------------------------- protected override void OnKeyDown(KeyEventArgs e) { if (e.Key == Key.Return) { CommitTextChange(); TraversalRequest tRequest = new TraversalRequest(FocusNavigationDirection.Next); UIElement keyboardFocus = Keyboard.FocusedElement as UIElement; keyboardFocus?.MoveFocus(tRequest); e.Handled = true; } else if ((e.Key == Key.OemPeriod) || (e.Key == Key.OemComma) || (e.Key == Key.Decimal)) { if (Text.Contains(".") && !IsEvaluating) { e.Handled = true; } } else if ((e.Key == Key.OemMinus) || (e.Key == Key.Subtract)) { if ((CaretIndex != 0 && SelectionStart != 0 || Text.Contains("-") && !SelectedText.Contains("-")) && !IsEvaluating) { e.Handled = true; } } base.OnKeyDown(e); }
/// <summary> /// Start a new search /// </summary> public string getSearchTerm() { // If no text is selected, select the word if (SelectedText.Length == 0) { int wordStart = SelectionStart; while (wordStart > 0 && IsWordChar(Text[wordStart - 1])) { wordStart--; } int wordEnd = SelectionStart; while (wordEnd < Text.Length && IsWordChar(Text[wordEnd])) { wordEnd++; } return(Text.Substring(wordStart, wordEnd - wordStart)); } else if (!SelectedText.Contains("\n")) // if selection is single line, use that as default { return(SelectedText); } else { return(null); } }
private void SaveText(object commandParameter) { string tempSelectedText = ""; foreach (var item in BranchItemCheckedStates) { string tempBranchItemName = "[" + item.BranchItem.BranchItemName + "]"; if (SelectedText.Contains(tempBranchItemName)) { tempSelectedText += tempBranchItemName + "; "; } } SelectedText = tempSelectedText; }
/* * /// <summary> * /// Overriden Undo to undo replace operations * /// </summary> * /// <remarks> * /// Would be a lot simpler if the framework controls gave you access to their undo stack * /// </remarks> * public new void Undo() * { * if (Text.Equals(textAfterReplace)) * { * Text = textBeforeReplace; * } * else * { * // Call ordinary undo * base.Undo(); * } * } */ /* * /// <summary> * /// Overridden CanUndo to include local undo operation * /// </summary> * public new bool CanUndo * { * get * { * if (Text.Equals(textAfterReplace)) * { * return true; // Next undo would be our replace cancel operation * } * else * { * // Call ordinary CanUndo * return base.CanUndo; * } * } * } * */ /* * /// <summary> * /// Redo to redo replace operations * /// </summary> * public void Redo() * { * if (Text.Equals(textBeforeReplace)) * { * Text = textAfterReplace; * } * * } * */ /// <summary> /// Start a new search /// </summary> private void NewSearch(bool replaceMode) { // If no text is selected, select the word if (SelectedText.Length == 0) { int wordStart = SelectionStart; while (wordStart > 0 && IsWordChar(Text[wordStart - 1])) { wordStart--; } int wordEnd = SelectionStart; while (wordEnd < Text.Length && IsWordChar(Text[wordEnd])) { wordEnd++; } // Store the selection start position on the first search so that when all searches are complete // this fact can be reported to the user in the find dialog. originalSelectionStart = wordEnd; // matching behaviour of various MS apps if (searchStarted != null) { searchStarted(Text.Substring(wordStart, wordEnd - wordStart)); } findDialog1.Show(Text.Substring(wordStart, wordEnd - wordStart), replaceMode); } else if (!SelectedText.Contains("\n")) // if selection is single line, use that as default { originalSelectionStart = SelectionStart; // matching behaviour of various MS apps if (searchStarted != null) { searchStarted(SelectedText); } findDialog1.Show(SelectedText, replaceMode); } else { originalSelectionStart = SelectionStart; if (searchStarted != null) { searchStarted(SelectedText); } findDialog1.Show(replaceMode); } }
protected override void OnKeyPress(KeyPressEventArgs e) { if (e.KeyChar == '\t' && SelectedText.Contains(Environment.NewLine)) { string start = string.Empty; if (SelectionStart > 0) { start = Text.Substring(0, SelectionStart); } string exchange = Text.Substring(SelectionStart, SelectionLength); string end = string.Empty; if (SelectionStart + SelectionLength < Text.Length - 1) { end = Text.Substring(SelectionStart + SelectionLength); } Text = start + '\t' + exchange.Replace(Environment.NewLine, Environment.NewLine + "\t") + end; e.Handled = true; } base.OnKeyPress(e); }
//-------------------------------------------------------------------------------------------------- protected override void OnTextChanged(TextChangedEventArgs e) { using (DeclareChangeBlock()) { foreach (var c in e.Changes) { if (c.AddedLength == 0) { continue; } Select(c.Offset, c.AddedLength); if (SelectedText.Contains(",")) { SelectedText = SelectedText.Replace(',', '.'); } Select(c.Offset + c.AddedLength, 0); } } var text = Text; if (text.Length == 0) { Text = "0"; } else { if (text[0] == '.') { Text.Insert(0, "0"); } } EvaluateExpression(); base.OnTextChanged(e); }
protected override void OnPreviewKeyDown(KeyEventArgs e) { if (IsReadOnly) { return; } backspaceAction = 0; string keyPressed = _keyConv.ConvertToString(e.Key); if (!(keyPressed == "Space" || keyPressed == "Delete" || keyPressed == "Backspace" || keyPressed == "Down" || keyPressed == "Up")) { base.OnPreviewKeyDown(e); return; } if (keyPressed != null) { isCellValueChanged = true; if (keyPressed.Length == 1 && char.IsControl(keyPressed[0])) { e.Handled = false; } if (keyPressed == "Space") { if (SelectionLength == Convert.ToString(Text).Length) { Clear(); e.Handled = true; } } int selectionStart = SelectionStart; dotPosition = Text.IndexOf('.'); if (keyPressed == "Delete") { if (Text.Length > 0 && (SelectedText.Contains(".") || (SelectedText.Length == 0 && SelectionStart == dotPosition)) && Text.Length != SelectionLength) { Text = Text.Substring(0, Math.Min(selectionStart, Text.Length)); SelectionStart = Math.Min(selectionStart, Text.Length); e.Handled = true; } } else if (keyPressed == "Backspace") { if (Text.Length > 0 && (SelectedText.Contains(".") || (SelectedText.Length == 0 && SelectionStart - 1 == dotPosition)) && Text.Length != SelectionLength) { Text = Text.Substring(0, Math.Min(selectionStart, Text.Length)); if (SelectedText.Length == 0 && selectionStart - 1 == dotPosition && selectionStart - 1 >= 0) { SelectionStart = Math.Min(selectionStart - 1, Text.Length); } else { SelectionStart = Math.Min(selectionStart, Text.Length); } e.Handled = true; } else { backspaceAction = 1; } } else if ((keyPressed == "Down") && dotPosition > 0) { SelectionStart = hasDecimalChar ? dotPosition + 1 : 0; SelectionLength = hasDecimalChar ? MaxScale : 0; e.Handled = hasDecimalChar; return; } else if ((keyPressed == "Up") && dotPosition > 0) { dotPosition = Text.IndexOf('.'); string currentPrecisionText = ""; string currentScaleText = ""; if (hasDecimalChar) { currentPrecisionText = Text.Substring(0, dotPosition); currentScaleText = Text.Substring(dotPosition + 1); } SelectionStart = 0; SelectionLength = hasDecimalChar ? Math.Min(dotPosition, currentPrecisionText.Length) : 0; e.Handled = hasDecimalChar; return; } } base.OnPreviewKeyDown(e); }
protected override void OnKeyPress(KeyPressEventArgs e) { if (e.KeyChar == '\b' || e.KeyChar == 22 || e.KeyChar == 1 || e.KeyChar == 3) { return; } if (e.KeyChar == '.') { if (Text.Contains(".") && !SelectedText.Contains(".")) { e.Handled = true; return; } } else if (e.KeyChar == '-') { if (Text.Contains("-") && !SelectedText.Contains("-")) { e.Handled = true; return; } } switch (_type) { default: case DisplayType.Binary: if (!e.KeyChar.IsBinary()) { e.Handled = true; } break; case DisplayType.FixedPoint_12_4: case DisplayType.FixedPoint_20_12: case DisplayType.FixedPoint_16_16: if (!e.KeyChar.IsFixedPoint()) { e.Handled = true; } break; case DisplayType.Float: if (!e.KeyChar.IsFloat()) { e.Handled = true; } break; case DisplayType.Hex: if (!e.KeyChar.IsHex()) { e.Handled = true; } break; case DisplayType.Signed: if (!e.KeyChar.IsSigned()) { e.Handled = true; } break; case DisplayType.Unsigned: if (!e.KeyChar.IsUnsigned()) { e.Handled = true; } break; } }
protected override void OnPreviewKeyDown(KeyEventArgs e) { if (IsManagingInput) { NegativeIntegerPending = false; NegativeFractionPending = false; switch (e.Key) { case Key.OemPeriod: case Key.Decimal: // If the user typed a period and we're to the left of the decimal point, skip over the decimal point if (CaretIndex < Text.Length && Text[CaretIndex] == DecimalSeparator) { CaretIndex++; e.Handled = true; } // Or if there is already a decimal point, don't add a second one // (If user is replacing the part that includes the decimal point, accept it) else if (Text.Contains(DecimalSeparator.ToString()) && !SelectedText.Contains(DecimalSeparator.ToString())) { e.Handled = true; } break; case Key.Back: // If the user pressed Backspace and we're to the right of the only decimal point, skip over the decimal point if (CaretIndex > 0 && CaretIndex <= Text.Length && Text[CaretIndex - 1] == DecimalSeparator && Text.HasOnlyOne(DecimalSeparator)) { CaretIndex--; e.Handled = true; } break; case Key.Delete: // If the user pressed Delete and we're to the left of the only decimal point, skip over the decimal point if (CaretIndex < Text.Length && Text[CaretIndex] == DecimalSeparator && Text.HasOnlyOne(DecimalSeparator)) { CaretIndex++; e.Handled = true; } break; case Key.D0: case Key.NumPad0: case Key.D1: case Key.NumPad1: case Key.D2: case Key.NumPad2: case Key.D3: case Key.NumPad3: case Key.D4: case Key.NumPad4: case Key.D5: case Key.NumPad5: case Key.D6: case Key.NumPad6: case Key.D7: case Key.NumPad7: case Key.D8: case Key.NumPad8: case Key.D9: case Key.NumPad9: if (RequireNegativeHandling) { // If a new negative number is being formed, remember that state so we can correct // the carat position after the text is updated if (Text == "-") { NegativeIntegerPending = true; } else if (Text == "-.") { NegativeFractionPending = true; } } break; case Key.OemMinus: case Key.Subtract: // Only accept a negative sign if: // - We are at the beginning of the number and there is not already a negative sign there, or // - The user has highlighted the entire number, so it is all going to be replaced // NOTE: TextBox does not allow you to enter -0.0, not sure how to override that... if (!((CaretIndex == 0 && Text.IndexOf('-') == -1) || SelectionLength == Text.Length)) { e.Handled = true; } break; } } base.OnPreviewKeyDown(e); }
protected override void OnPreviewKeyDown(KeyEventArgs e) { if (_suggestionList.IsVisible && e.Key.In(Key.Tab, Key.Enter, Key.Return)) { var selectedItem = (Suggestion)_suggestionList.SelectedItem; if (selectedItem != null) { var selectedText = selectedItem.Completion; var index = CaretIndex; Text = Text.Insert(CaretIndex, selectedText); CaretIndex = index + selectedText.Length; } HideSuggestionList(); e.Handled = true; return; } if (e.Key == Key.Escape) { HideSuggestionList(); e.Handled = true; } if (ShouldShowSuggestionList(e)) { SyntaxParser.FindSuggestions(Text, SelectionStart); ShowSuggestionList(); e.Handled = true; } if (e.Key == Key.Tab && e.KeyboardDevice.Modifiers == ModifierKeys.Shift) { // with selected text if (SelectedText != string.Empty) { string[] lines = SelectedText.SplitLines(); for (int ii = 0; ii < lines.Length; ii++) { if (lines[ii].StartsWith(Tab)) { lines[ii] = lines[ii].Substring(Tab.Length); } else { lines[ii] = lines[ii].TrimStart(' '); } } SelectedText = String.Join(Environment.NewLine, lines); } else { var index = CaretIndex; var lastLine = Text.LastIndexOf(Environment.NewLine, index, StringComparison.Ordinal); if (lastLine == -1) { lastLine = Text.Length - 1; } var startLine = Text.IndexOf(Environment.NewLine, lastLine, StringComparison.Ordinal); if (startLine != -1) { startLine += Environment.NewLine.Length; } else { startLine = 0; } // find empty spaces var spaces = 0; for (var i = startLine; i < Text.Length - 1; i++) { if (Text[i] == ' ') { spaces++; } else { break; } } if (spaces > TabSize) { spaces = TabSize; } Text = Text.Remove(startLine, spaces); // set position of caret if (index >= startLine + spaces) { CaretIndex = index - spaces; } else if (index >= startLine && index < startLine + spaces) { CaretIndex = startLine; } else { CaretIndex = index; } } e.Handled = true; } // tab if (e.Key == Key.Tab && e.KeyboardDevice.Modifiers == ModifierKeys.None) { if (SelectedText == string.Empty) { var caretPosition = CaretIndex; Text = Text.Insert(caretPosition, Tab); CaretIndex = caretPosition + TabSize; } else { if (!SelectedText.Contains(Environment.NewLine)) { SelectedText = Tab; } else { SelectedText = Tab + SelectedText.Replace(Environment.NewLine, Environment.NewLine + Tab); } } e.Handled = true; } // enter respects indenting if (e.Key == Key.Return) { var index = CaretIndex; var lastLine = Text.LastIndexOf(Environment.NewLine, index, StringComparison.Ordinal); var spaces = 0; if (lastLine != -1) { var line = Text.Substring(lastLine, Text.Length - lastLine); var startLine = line.IndexOf(Environment.NewLine, StringComparison.Ordinal); if (startLine != -1) { line = line.Substring(startLine).TrimStart('\r', '\n'); } foreach (var c in line) { if (c == ' ') { spaces++; } else { break; } } } Text = Text.Insert(index, Environment.NewLine + new String(' ', spaces)); CaretIndex = index + Environment.NewLine.Length + spaces; e.Handled = true; } base.OnPreviewKeyDown(e); }
protected override void OnKeyPress(KeyPressEventArgs e) { base.OnKeyPress(e); if (e.KeyChar == '.' || e.KeyChar == ',') { e.KeyChar = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator[0]; } if (e.KeyChar != 22) { e.Handled = !char.IsDigit(e.KeyChar) && (e.KeyChar != ',' || (Text.Contains(",") && !SelectedText.Contains(","))) && e.KeyChar != (char)Keys.Back && (e.KeyChar != '-' || SelectionStart != 0 || (Text.Contains("-") && !SelectedText.Contains("-"))); } }