private void Amount_Changed(object sender, System.Windows.Controls.TextChangedEventArgs e) { //屏蔽中文输入和非法字符粘贴输入 TextBox textBox = sender as TextBox; TextChange[] change = new TextChange[e.Changes.Count]; e.Changes.CopyTo(change, 0); int offset = change[0].Offset; if (change[0].AddedLength > 0) { int num = 0; if (!Int32.TryParse(textBox.Text, out num)) { textBox.Text = textBox.Text.Remove(offset, change[0].AddedLength); textBox.Select(offset, 0); } } if (TextBox_Amount.Text == "") { TextBox_Amount_Bg.Visibility = Visibility.Visible; } else { TextBox_Amount_Bg.Visibility = Visibility.Hidden; } }
private void TextBox_TextChanged(object sender, TextChangedEventArgs e) { TextBox textBox = sender as TextBox; TextChange[] change = new TextChange[e.Changes.Count]; e.Changes.CopyTo(change, 0); int offset = change[0].Offset; if (change[0].AddedLength > 0) { double num = 0; if (!Double.TryParse(textBox.Text, out num)) { textBox.Text = textBox.Text.Remove(offset, change[0].AddedLength); textBox.Select(offset, 0); } } }
private string makeInt(string s, TextChange tc) { // removing is free if (tc.AddedLength <= 0) return s; StringBuilder sb = new StringBuilder(); int start = 0; if (s[start] == '-') { sb.Append(s[start++]); } for (int i = start; i < s.Length; ++i) { if (s[i] >= '0' && s[i] <= '9') { sb.Append(s[i]); } } return sb.ToString(); }
private void txtPage_TextChanged(object sender, TextChangedEventArgs e) { var textBox = sender as TextBox; int total = Convert.ToInt32(labTotal.Content.ToString().Substring(labTotal.Content.ToString().IndexOf('/') + 1)); string str = textBox.Text.Trim(); if (str == "") { textBox.Text = "1"; } if (this.IsValidPhoneNumber(str) == false) { DialogShow.Show(StringHelper.FindLanguageResource("Invalidnum"), StringHelper.FindLanguageResource("error"), 2); textBox.Text = "1"; return; } //2015/7/29 TextChange[] change = new TextChange[e.Changes.Count]; e.Changes.CopyTo(change, 0); int offset = change[0].Offset; if (change[0].AddedLength > 0) { double num = 0; if (!Double.TryParse(textBox.Text, out num)) { textBox.Text = textBox.Text.Remove(offset, change[0].AddedLength); textBox.Select(offset, 0); } } if (Convert.ToInt32(textBox.Text.Trim().Replace(" ", "")) > total) { textBox.Text = total.ToString(); } //2015/7/29 }
private void txtNumber_TextChanged(object sender, TextChangedEventArgs e) { var textBox = sender as TextBox; int total = Convert.ToInt32(labTotal.Content.ToString().Substring(labTotal.Content.ToString().IndexOf('/') + 1)); string str = textBox.Text; TextChange[] change = new TextChange[e.Changes.Count]; e.Changes.CopyTo(change, 0); int offset = change[0].Offset; if (change[0].AddedLength > 0) { double num = 0; if (!Double.TryParse(textBox.Text, out num)) { textBox.Text = textBox.Text.Remove(offset, change[0].AddedLength); textBox.Select(offset, 0); } } }
private void TimeoutTextBox_TextChanged(object sender, TextChangedEventArgs e) { TextBox textBox = sender as TextBox; var change = new TextChange[e.Changes.Count]; e.Changes.CopyTo(change, 0); int offset = change[0].Offset; if (change[0].AddedLength > 0) { int num = 0; if (textBox == null || int.TryParse(textBox.Text, out num)) { if (num < 0) { if (textBox != null) textBox.Text = 0.ToString(); } //else if (num > 10000) if (textBox != null) textBox.Text = 10000.ToString(); return; } textBox.Text = textBox.Text.Remove(offset, change[0].AddedLength); textBox.Select(offset, 0); } }
private string makeFloat(string s, TextChange tc) { // removing is free if (tc.AddedLength <= 0) return s; // check new segment for non float characters bool newComma = false; int newCommaPos = -1; for (int i = tc.Offset; i < s.Length; ++i) { if (s[i] == '.') { newComma = true; newCommaPos = i; break; } } StringBuilder sb = new StringBuilder(); int start = 0; if (s[start] == '-') { sb.Append(s[start++]); } for (int i = start; i < s.Length; ++i) { if (s[i] >= '0' && s[i] <= '9') { sb.Append(s[i]); } else if (s[i] == '.' && (i == newCommaPos || !newComma)) { sb.Append(s[i]); } } return sb.ToString(); }
/// <summary> /// Koloruje składnie. /// </summary> private void TryColorSyntax(string value, TextChange current) { textBoxQuery.TextChanged -= this.TextChangedEventHandler; TextRange r; try { r = new TextRange(this.textBoxQuery.Document.ContentStart.GetPositionAtOffset(current.Offset - 1 - value.Length), this.textBoxQuery.Document.ContentStart.GetPositionAtOffset(current.Offset)); } catch { textBoxQuery.Document.Blocks.Clear(); textBoxQuery.AppendText(" "); textBoxQuery.TextChanged += this.TextChangedEventHandler; return; } if (CheckingKeywords(value)) { r.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Blue)); } else { r.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Black)); } textBoxQuery.TextChanged += this.TextChangedEventHandler; }
private bool MergePropertyChange(TextChange leftChange, TextChange rightChange) { if (leftChange.Offset + leftChange.PropertyCount >= rightChange.Offset) { if (leftChange.Offset + leftChange.PropertyCount < rightChange.Offset + rightChange.PropertyCount) { // right change is partially inside left, but not completely. int overlap = leftChange.Offset + leftChange.PropertyCount - rightChange.Offset; int delta = rightChange.PropertyCount - overlap; leftChange.SetPropertyCount(leftChange.PropertyCount + delta); } rightChange.SetPropertyCount(0); return true; } return false; }
private void MergeTextChangeRight(TextChange oldChange, TextChange newChange, int offset, int length) { // If the old change is an addition, find the length of the overlap // and adjust the addition and new deletion counts accordingly int addedLengthOverlap = oldChange.AddedLength > 0 ? offset + length - oldChange.Offset : 0; #if PROPERTY_CHANGES // Check for a property change in the new change that overlaps the old change int propertyOverlap = newChange.Offset + newChange.PropertyCount - oldChange.Offset; if (propertyOverlap > 0) { int delta = Math.Min(propertyOverlap, addedLengthOverlap); newChange.SetPropertyCount(newChange.PropertyCount - delta); // Don't need to adjust oldChange.PropertyCount, since oldChange is about to be removed } #endif // adjust removed count if (addedLengthOverlap >= oldChange.AddedLength) { // old change is entirely within new one newChange.RemovedLength += (oldChange.RemovedLength - oldChange.AddedLength); Changes.Remove(oldChange.Offset); } else { newChange.RemovedLength += (oldChange.RemovedLength - addedLengthOverlap); newChange.AddedLength += (oldChange.AddedLength - addedLengthOverlap); Changes.Remove(oldChange.Offset); } }
// returns true if the change merged into an earlier insertion private bool MergeTextChangeLeft(TextChange oldChange, TextChange newChange, bool isDeletion, int length) { // newChange is inserting or deleting text inside oldChange. int overlap; #if PROPERTY_CHANGES // Check for a property change in the old change that overlaps the new change if (oldChange.Offset + oldChange.PropertyCount >= newChange.Offset) { if (isDeletion) { overlap = oldChange.PropertyCount - (newChange.Offset - oldChange.Offset); oldChange.SetPropertyCount(oldChange.PropertyCount - Math.Min(overlap, length)); DeleteChangeIfEmpty(oldChange); } else { oldChange.SetPropertyCount(oldChange.PropertyCount + length); } } #endif if (oldChange.Offset + oldChange.AddedLength >= newChange.Offset) { // If any text was deleted in the new change, adjust the added count of the // previous change accordingly. The removed count of the new change must be // adjusted by the same amount. if (isDeletion) { overlap = oldChange.AddedLength - (newChange.Offset - oldChange.Offset); int cancelledCount = Math.Min(overlap, newChange.RemovedLength); oldChange.AddedLength -= cancelledCount; oldChange.RemovedLength += (length - cancelledCount); } else { oldChange.AddedLength += length; } #if PROPERTY_CHANGES if (newChange.PropertyCount == 0) { // We've merged the data from the new change into an older one, so we can // delete the change from the list. Changes.Remove(newChange.Offset); } else { // Can't delete the change, since there's pre-existing property change data, so // just clear the data instead. newChange.AddedLength = 0; newChange.RemovedLength = 0; } #else // We've merged the data from the new change into an older one, so we can // delete the change from the list. Changes.Remove(newChange.Offset); #endif return true; } return false; }
private void DeleteChangeIfEmpty(TextChange change) { #if PROPERTY_CHANGES if (change.AddedLength == 0 && change.RemovedLength == 0 && change.PropertyCount == 0) #else if (change.AddedLength == 0 && change.RemovedLength == 0) #endif { Changes.Remove(change.Offset); } }
//----------------------------------------------------- // // Private Methods // //------------------------------------------------------ // // First we discover where the new change goes in the sorted list. If there's already // a change there, merge the new one with the old one. // // Next, if the new change is an insertion or deletion, see if the offset of the change // is within a range covered by an insertion at a lower offset. If so, merge the change at // the higher offset into the previous change and delete the change at the higher offset. // // Next, if the change is a deletion, see if we delete enough characters to run into a // change at a higher offset. If so, merge the change at the higher offset into this newer // change and delete the older, higher-offset change. // // Finally, update all offsets higher than the current change to reflect the number of // characters inserted or deleted. // private void AddChangeToList(PrecursorTextChangeType textChange, int offset, int length) { int offsetDelta = 0; // Value by which to adjust other offsets in list int curIndex; // loop counter TextChange change = null; // initialize to satisfy compiler bool isDeletion = false; #if PROPERTY_CHANGES bool shiftPropertyChanges = false; #endif // // Is there already a change at this offset in our list? If so, use it // int keyIndex = Changes.IndexOfKey(offset); if (keyIndex != -1) { change = Changes.Values[keyIndex]; } else { change = new TextChange(); change.Offset = offset; Changes.Add(offset, change); keyIndex = Changes.IndexOfKey(offset); } // // Merge values from new change into empty or pre-existing change // if (textChange == PrecursorTextChangeType.ContentAdded || textChange == PrecursorTextChangeType.ElementAdded) { change.AddedLength += length; offsetDelta = length; #if PROPERTY_CHANGES if (change.PropertyCount > 0) { shiftPropertyChanges = true; } #endif } else if (textChange == PrecursorTextChangeType.ContentRemoved || textChange == PrecursorTextChangeType.ElementExtracted) { // Any deletions made after an addition just cancel out the earlier addition change.RemovedLength += Math.Max(0, length - change.AddedLength); change.AddedLength = Math.Max(0, change.AddedLength - length); #if PROPERTY_CHANGES // If an element was extracted, any property change made on that element should be removed as well if (textChange == PrecursorTextChangeType.ElementExtracted) { change.SetPropertyCount(0); } else { change.SetPropertyCount(Math.Max(0, change.PropertyCount - length)); } #endif offsetDelta = -length; isDeletion = true; } #if PROPERTY_CHANGES else { // Property change if (change.PropertyCount < length) { change.SetPropertyCount(length); } } #endif // // Done with simple case. Now look for changes that intersect. // There are two possible (non-exclusive) merge conditions: // 1. A positionally preceding change spans this offset (this change is inserting // into or deleting from the middle or right edge of previous inserted text) // 2. On deletion, the change spans one or more positionally later changes // if (keyIndex > 0 && textChange != PrecursorTextChangeType.PropertyModified) { curIndex = keyIndex - 1; // Since we don't merge property changes, there could be an arbitrary number of // them between the new change and an overlapping insertion or overlapping property // change. In fact, there could be multiple property changes that overlap this // change. We need to adjust ALL of them. There can be only one overlapping // insertion, but there's no way to leverage that fact. TextChange mergedChange = null; while (curIndex >= 0) { TextChange prevChange = Changes.Values[curIndex]; #if PROPERTY_CHANGES if (prevChange.Offset + prevChange.AddedLength >= offset || prevChange.Offset + prevChange.PropertyCount >= offset) #else if (prevChange.Offset + prevChange.AddedLength >= offset) #endif { if (MergeTextChangeLeft(prevChange, change, isDeletion, length)) { mergedChange = prevChange; } } curIndex--; } if (mergedChange != null) { // the change got merged. Use the merged change as the basis for righthand merging. change = mergedChange; } // changes may have been deleted, so update the index of the change we're working with keyIndex = Changes.IndexOfKey(change.Offset); } curIndex = keyIndex + 1; if (isDeletion && curIndex < Changes.Count) { while (curIndex < Changes.Count && Changes.Values[curIndex].Offset <= offset + length) { // offset and length must be passed because if we've also merged left, we haven't yet adjusted indices. // Note that MergeTextChangeRight() always removes Changes.Values[curIndex] from the list, so // we don't need to increment curIndex. MergeTextChangeRight(Changes.Values[curIndex], change, offset, length); } // changes may have been deleted, so update the index of the change we're working with keyIndex = Changes.IndexOfKey(change.Offset); } // Update all changes to reflect new offsets. // If offsetDelta is positive, go right to left; otherwise, go left to right. // The order of the offsets will never change, so we can use an indexer safely. // To avoid nasty N-squared perf, create a new list instead of moving things within // the old one. // if (offsetDelta != 0) { SortedList<int, TextChange> newChanges = new SortedList<int, TextChange>(Changes.Count); for (curIndex = 0; curIndex < Changes.Count; curIndex++) { TextChange curChange = Changes.Values[curIndex]; if (curIndex > keyIndex) { curChange.Offset += offsetDelta; } newChanges.Add(curChange.Offset, curChange); } _changes = newChanges; } DeleteChangeIfEmpty(change); #if PROPERTY_CHANGES // Finally, if the change was an insertion and there are property changes starting // at this location, the insertion is not part of the property changes. Shift the // property changes forward by the length of the insertion. if (shiftPropertyChanges) { int propertyCount = change.PropertyCount; change.SetPropertyCount(0); AddChangeToList(PrecursorTextChangeType.PropertyModified, offset + offsetDelta, propertyCount); } #endif }
protected override void OnTextChanged(TextChangedEventArgs e) { initial++; if (initial == 1) { if (this.Text.Length > 0) { if (this.SelText) { this.Focus(); this.SelectAll(); } else { this.SelectionStart = this.Text.Length; } } } if (this.InitialReload && !GetReloadValue(this) && initial == 2) { TextChange[] change = new TextChange[e.Changes.Count]; e.Changes.CopyTo(change, 0); int offset = change[0].Offset; if (change[0].AddedLength > 0) { this.Text = this.Text.Remove(0, this.Text.Length - change[0].AddedLength); SetReloadValue(this, true); } } if (this.IsNumeric) { TextChange[] change = new TextChange[e.Changes.Count]; e.Changes.CopyTo(change, 0); int offset = change[0].Offset; if (change[0].AddedLength > 0) { double num = 0; if (!string.IsNullOrEmpty(this.Text) && !Double.TryParse(this.Text, out num)) { this.Text = this.Text.Remove(offset, change[0].AddedLength); this.Select(offset, 0); } else { if (!TextCheck()) { this.Text = this.Text.Remove(offset, change[0].AddedLength); this.Select(offset, 0); } } } } if (this.IsPassword) { if (dirtyBaseText == true) return; string currentText = this.BaseText; int selStart = this.SelectionStart; if (currentText.Length < _password.Length) { // Remove deleted chars _password = _password.Remove(selStart, _password.Length - currentText.Length); SetRealText(this, _password); } if (!string.IsNullOrEmpty(currentText)) { for (int i = 0; i < currentText.Length; i++) { if (currentText[i] != PWD_CHAR) { // Replace or insert char if (this.BaseText.Length == _password.Length) { _password = _password.Remove(i, 1).Insert(i, currentText[i].ToString()); } else { _password = _password.Insert(i, currentText[i].ToString()); } } } SetRealText(this, _password); this.BaseText = new string(PWD_CHAR, _password.Length); this.SelectionStart = selStart; } base.OnTextChanged(e); } if (this.WaterMarkLable != null) { if (this != null && !string.IsNullOrEmpty(this.Text)) { this.WaterMarkLable.Visibility = Visibility.Hidden; } else { this.WaterMarkLable.Visibility = Visibility.Visible; } } return; }
private void TextBox_OnlyDigits(object sender, TextChangedEventArgs e) { //屏蔽中文输入和非法字符粘贴输入 TextBox textBox = sender as TextBox; TextChange[] change = new TextChange[e.Changes.Count]; e.Changes.CopyTo(change, 0); int offset = change[0].Offset; if (change[0].AddedLength > 0) { int num = 0; if (!Int32.TryParse(textBox.Text, out num)) { textBox.Text = textBox.Text.Remove(offset, change[0].AddedLength); textBox.Select(offset, 0); } } }
private void txtNumber_TextChanged(object sender, TextChangedEventArgs e) { var textBox = sender as TextBox; TextChange[] change = new TextChange[e.Changes.Count]; e.Changes.CopyTo(change, 0); int offset = change[0].Offset; if (change[0].AddedLength > 0) { double num = 0; string str = textBox.Text.Replace("*", "").Replace("#", ""); if (!Double.TryParse(str, out num) && str!="") { textBox.Text = textBox.Text.Remove(offset, change[0].AddedLength); textBox.Select(offset, 0); } txtNumber.ToolTip = textBox.Text; } }
public void TextChangedHandler(object sender, TextChangedEventArgs args) { if (this.Text.Length != 0) { TextChange[] a = new TextChange[1]; args.Changes.CopyTo(a, 0); if (a[0].AddedLength != 0) { if (Uri.IsHexDigit(this.Text[a[0].Offset]) == false) { this.Text = this.Text.Remove(a[0].Offset, 1); this.Text = this.Text.ToUpper(); this.Select(a[0].Offset, 0); } else { this.Text = this.Text.ToUpper(); this.Select(a[0].Offset + 1, 0); } } } }