在文本框自动完成属性中,当我输入一个像'm‘这样的字符时,它会丢弃所有以'm’或'M‘开头的字符串,但当我写一个字符'أ’(这是一个阿拉伯字符)时,它只会丢弃以'أ‘开头的字符串,当我输入'أ’、'ا‘、'إ’或'آ‘时,它会丢弃所有以非键入字符开头的字符串,在windows应用程序中,我没有使用ASP.net任何建议?
发布于 2011-05-17 03:52:37
我非常惊讶地看到,文本框控件和自动完成字符串集合都无法为自动完成机制指定定义字符串相等的内容。我能想到的获得你想要的行为的唯一方法是创建你自己的自动补全机制。
在框架提供功能之前,我已经做过几次了,这并不困难。
只需使用可编辑的ComboBox而不是TextBox,并处理TextChanged事件来创建自动完成。
以下是该过程的一些未尝试过的伪代码:
bool textChangedProgramatically = false;
List<string> myStrings; // The list of items that can appear in the auto-complete.
private static myComboBox_TextChanged(object sender, EventArgs args)
{
if (textChangedProgramatically)
return;
string searchText = myComboBox.Text;
// Use appropriate culturally-sensative string StartsWith comparisons
List<string> matchingItems = GetMatchingStrings(searchText, myStrings);
string firstMatch;
if (matchingItems.Length > 0)
firstMatch = matchingItems[0];
else
firstMatch = string.Empty;
myComboBox.Items.Clear;
myComboBox.Items.AddRange(matchingItems);
string fulltext = searchText;
if (firstMatch.Length > fullText.Length)
{
fullText = fullText + firstMatch.Substring(fullText.Length);
textChangeProgramatically = true;
myComboBox.Text = fullText;
myComboBox.SelectionStart = searchText.Length;
myComboBox.SelectionLength = fullText.Length - searchText.Length;
textChangeProgramatically = false;
}
}诀窍是在GetMatchingStrings中获得正确的匹配行为。在进行比较之前,您可能希望使用字符串兼容性规范化将阿拉伯字符转换为非表示形式,但我希望正确的SubString重载可以为您处理所有这些情况。
https://stackoverflow.com/questions/6012469
复制相似问题