C# 删除组合框中最后输入的字符
我还有一个问题 我已经设置了comboBox,以便它只接受与comboBoxItems中任何项的名称匹配的字符 现在我遇到了一个问题。请查看我的代码,然后我将向您解释问题:C# 删除组合框中最后输入的字符,c#,wpf,combobox,C#,Wpf,Combobox,我还有一个问题 我已经设置了comboBox,以便它只接受与comboBoxItems中任何项的名称匹配的字符 现在我遇到了一个问题。请查看我的代码,然后我将向您解释问题: private void myComboBox_KeyUp(object sender, KeyEventArgs e) { // Get the textbox part of the combobox TextBox textBox = cbEffectOn.Template.Fi
private void myComboBox_KeyUp(object sender, KeyEventArgs e)
{
// Get the textbox part of the combobox
TextBox textBox = cbEffectOn.Template.FindName("PART_EditableTextBox", cbEffectOn) as TextBox;
// holds the list of combobox items as strings
List<String> items = new List<String>();
// indicates whether the new character added should be removed
bool shouldRemoveLastChar = true;
for (int i = 0; i < cbEffectOn.Items.Count; i++)
{
items.Add(cbEffectOn.Items.GetItemAt(i).ToString());
}
for (int i = 0; i < items.Count; i++)
{
// legal character input
if (textBox.Text != "" && items.ElementAt(i).StartsWith(textBox.Text))
{
shouldRemoveLastChar = false;
break;
}
}
// illegal character input
if (textBox.Text != "" && shouldRemoveLastChar)
{
textBox.Text = textBox.Text.Remove(textBox.Text.Length - 1);
textBox.CaretIndex = textBox.Text.Length;
}
}
private void myComboBox\u KeyUp(对象发送方,KeyEventArgs e)
{
//获取组合框的文本框部分
TextBox TextBox=cbEffectOn.Template.FindName(“PART_EditableTextBox”,cbEffectOn)作为TextBox;
//将组合框项目列表作为字符串保存
列表项=新列表();
//指示是否应删除添加的新字符
bool shouldRemoveLastChar=true;
对于(int i=0;i
在最后一个if条件中,我将从组合框中删除最后一个字符。但用户可以使用箭头键或鼠标更改光标的位置,并在文本中间输入文本
因此,如果在文本中间输入一个字符,如果文本变得无效,我的意思是如果它与组合框中的项目不匹配,那么我应该删除最后输入的字符。有人能建议我如何获取最后插入的字符并将其删除吗
更新:
string OldValue = "";
private void myComboBox_KeyDown(object sender, KeyEventArgs e)
{
TextBox textBox = cbEffectOn.Template.FindName("PART_EditableTextBox", cbEffectOn) as TextBox;
List<String> items = new List<String>();
for (int i = 0; i < cbEffectOn.Items.Count; i++)
{
items.Add(cbEffectOn.Items.GetItemAt(i).ToString());
}
OldValue = textBox.Text;
bool shouldReplaceWithOldValue = true;
string NewValue = textBox.Text.Insert(textBox.CaretIndex,e.Key.ToString()).Remove(textBox.CaretIndex + 1,textBox.Text.Length - textBox.CaretIndex);
for (int i = 0; i < items.Count; i++)
{
// legal character input
if (NewValue != "" && items.ElementAt(i).StartsWith(NewValue, StringComparison.InvariantCultureIgnoreCase))
{
shouldReplaceWithOldValue = false;
break;
}
}
//// illegal character input
if (NewValue != "" && shouldReplaceWithOldValue)
{
e.Handled = true;
}
}
字符串OldValue=”“;
私有void mycombox_KeyDown(对象发送方,KeyEventArgs e)
{
TextBox TextBox=cbEffectOn.Template.FindName(“PART_EditableTextBox”,cbEffectOn)作为TextBox;
列表项=新列表();
对于(int i=0;i
在这里,我尝试移动KeyDown事件中的所有代码来解决上述问题。此代码工作正常,但有一个问题
如果我有任何名为Birds&Animals的项目,那么在键入Birds和空格后,我无法键入&
我知道问题出在哪里,但不知道解决办法
问题是:要输入&我必须按shift键,然后按7键。但两者都作为不同的密钥发送
我想到的解决方案:
1) 我应该将代码移动到KeyUp事件。但是这里会出现长按和快速打字的问题。
2) 我想我应该用什么东西来代替e键。但是不知道是什么。您是否考虑过使用字符串变量来保存组合框文本框部分的最后一个合法文本值
最初,该字符串为空,因为用户尚未键入任何内容,然后在处理每个
KeyUp
事件时,如果输入了无效字符,则使用前一个字符串值替换文本框的文本;否则,以前的字符串值现在用新的完整字符串更新;等待用户输入。您是否考虑过使用字符串变量在组合框的文本框部分保存最后一个合法文本值
最初,该字符串为空,因为用户尚未键入任何内容,然后在处理每个
KeyUp
事件时,如果输入了无效字符,则使用前一个字符串值替换文本框的文本;否则,以前的字符串值现在用新的完整字符串更新;等待用户的输入。在组合框上订阅TextChanged
事件,而不是KeyUp
事件。在事件处理程序中,可以获取发生更改的偏移量。您可以在hanlder中使用验证逻辑,并删除偏移量处的字符(如果它使文本无效)
private void Window_Loaded(object sender, RoutedEventArgs e)
{
TextBox textBox = cbEffectOn.Template.FindName("PART_EditableTextBox", cbEffectOn) as TextBox;
textBox.TextChanged += new TextChangedEventHandler(textBox_TextChanged);
}
void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
int index = e.Changes.First().Offset;
}
在组合框
文本框
上订阅TextChanged
事件,而不是KeyUp
事件。在事件处理程序中,可以获取发生更改的偏移量。您可以在hanlder中使用验证逻辑,并删除偏移量处的字符(如果它使文本无效)
private void Window_Loaded(object sender, RoutedEventArgs e)
{
TextBox textBox = cbEffectOn.Template.FindName("PART_EditableTextBox", cbEffectOn) as TextBox;
textBox.TextChanged += new TextChangedEventHandler(textBox_TextChanged);
}
void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
int index = e.Changes.First().Offset;
}
我不确定您是否正在尝试这样做,但我觉得您正在尝试做我们通常在visual studio Intellisense中看到的那样,在键入时对结果进行筛选
您应该使用WPF提供的验证机制,而不是删除击键。这是一个如何工作的示例
所涵盖的情景:
输入与组合框项目完全匹配:TypedInput
&SelectedItem
两者都显示完全匹配
输入部分匹配某些元素:TypedInput
短名单弹出列表。当SelectedItem
保持为空时,绑定显示匹配的文本
输入与列表中的任何项都不匹配,无论是从开始还是在某个时间
随机点:用户在视觉上得到反馈(有可能
添加
namespace Sample
{
public partial class MainWindow { public MainWindow() { InitializeComponent(); } }
}
namespace Sample
{
using System.Globalization;
using System.Linq;
using System.Windows.Controls;
public class ContainsValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
var result = MainWindowViewModel.CurrentInstance.Items.Any(x => x.ToLower(cultureInfo).Contains((value as string).ToLower(cultureInfo)));
return new ValidationResult(result, "No Reason");
}
}
}
namespace Sample
{
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
public sealed class MainWindowViewModel : INotifyPropertyChanged
{
private string _typedText;
private string _selectedItem;
private static readonly MainWindowViewModel Instance = new MainWindowViewModel();
private MainWindowViewModel()
{
Items = new[] { "Apples", "Apples Green", "Bananas", "Bananas & Oranges", "Oranges", "Grapes" };
}
public static MainWindowViewModel CurrentInstance { get { return Instance; } }
public string SelectedItem
{
get { return _selectedItem; }
set
{
if (value == _selectedItem) return;
_selectedItem = value;
OnPropertyChanged();
}
}
public string TypedText
{
get { return _typedText; }
set
{
if (value == _typedText) return;
_typedText = value;
OnPropertyChanged();
OnPropertyChanged("FilteredItems");
}
}
public IEnumerable<string> Items { get; private set; }
public IEnumerable<string> FilteredItems
{
get
{
return Items == null || TypedText == null ? Items : Items.Where(x => x.ToLowerInvariant().Contains(TypedText.ToLowerInvariant()));
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}