有一个文本框,然后通过在其中输入名称来检查字典中是否存在这样的名称 如果有,它将显示一条消息,并要求重写名称 但是我有一个按键,可以跳过所有俄语字母,并且在输出名称对应消息之后,游戏仍在继续 这是带有按键和按钮的代码,其中包含是否有这样的名称的检查: 问题是如何解决名称重复的按键问题?
private void Button5_Click(object sender, EventArgs e)
{
var dict = new Dictionary<string, int>(); // where will the data be imported from the file
using (var stream = new StreamReader(@"C:\Users\HP\Desktop\photo\results.txt")) // open file
{
// auxiliary elements
var line = "";
string[] param;
// go through all the lines
while ((line = stream.ReadLine()) != null)
{
param = line.Split(' '); // Break line
dict.Add(param[0], Convert.ToInt32(param[1])); // add to dictionary
}
}
if (dict.ContainsKey(textBox1.Text) == false)
{
}
else
{
MessageBox.Show("This name already exists!");
}
}
private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
string Symbol = e.KeyChar.ToString();
if (!Regex.Match(Symbol, @"[а-яА-Я]").Success && (Symbol != "\b"))
{
e.Handled = true;
}
}
答案 0 :(得分:2)
首先,我建议使用提取方法,即
CV_8UC3
然后,为了防止在名称中添加俄语字母,让我们在using System.IO;
using System.Linq;
...
private static Dictionary<string, int> AllNames() {
return File
.ReadLines(@"C:\Users\HP\Desktop\картинки\results.txt")
.Where(line => !string.IsNullOrWhiteSpace(line))
.Select(item => item.Split(' '))
.ToDictionary(items => items[0],
items => int.Parse(items[1]));
}
private static bool NameExists(string name) {
return AllNames().ContainsKey(name);
}
private static bool IsValidNameCharacter(char value) {
// Russian letters are valid ones
if (value >= 'А' && value <= 'Я' ||
value >= 'а' && value <= 'я' ||
value == 'ё' ||
value == 'Ё')
return true;
// ...All the others are not
return false;
}
处测试textBox1.Text
:
TextChanged