我试图从e.KeyChar获取interget值,但它只是给了我ascii值。
简而言之。
int[] row = {1,2,3};
private void Inputnr1box_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
else if (row.Contains(e.KeyChar) ) { MessageBox.Show("Well done boys");
}
else { MessageBox.Show("Fail!"); }
}
如果我尝试将数组中的一个值更改为49并发送" 1"一切都很好。
问题源于一组较大的文本框,用户可以在其中放置数值,但不允许输入重复值。我计划将值保存在整数数组中,然后使用contains来查看它是否已经输入一次。
答案 0 :(得分:0)
您可以执行以下操作来确定按下的键是否为数字,如果数字不存在则处理。
List<int> row = new List<int>();
private void Inputnr1box_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true; // Not a DIGIT
}
else
{
// Else test for existence
int number;
if (Int32.TryParse(e.KeyChar.ToString(), out number))
{
if (row.Contains(number))
{
e.Handled = true; // Already exists
}
else
{
row.Add(number);
}
}
}
}
答案 1 :(得分:0)
尝试此操作,game_KeyPress和game_KeyDown方法检测按下的键。可以在Windows窗体的``键''枚举中找到控制键,您可以做的一个技巧是放入``开关(键)'',Visual Studio 2019的自动自动完成功能将显示键中包含的所有枚举。 game_KeyPress方法无法检测到所有按下的键,因此我们使用game_KeyDown来检测箭头。有些键有其特定的事件。
private void game_KeyPress(object sender, KeyPressEventArgs e)
{
int iKey = 0;
char cKey = ' ';
string sKey = " ";
if (char.IsControl(e.KeyChar) || !char.IsDigit(e.KeyChar))
{
if (char.IsControl(e.KeyChar)) // Special key
{
if (e.KeyChar == (char)Keys.Back)
{
sKey = Keys.Back.ToString(); // to save value
MessageBox.Show(sKey, "information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
if (e.KeyChar == (char)Keys.Tab)
MessageBox.Show(Keys.Tab.ToString(), "information", MessageBoxButtons.OK, MessageBoxIcon.Information);
if (e.KeyChar == (char)Keys.Enter)
MessageBox.Show(Keys.Enter.ToString(), "information", MessageBoxButtons.OK, MessageBoxIcon.Information);
if (e.KeyChar == (char)Keys.Space)
MessageBox.Show(Keys.Space.ToString(), "information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else // Normal key
{
cKey = e.KeyChar; // to save value
MessageBox.Show(cKey.ToString(), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else // Number
{
iKey = Convert.ToInt32(char.GetNumericValue(e.KeyChar)); // to save value
MessageBox.Show(iKey.ToString(), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
e.Handled = true; // e.handled is a property then assigned to true, it deletes the character from the keychar property value
}
private void game_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Down)
MessageBox.Show(Keys.Down.ToString(), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
if (e.KeyCode == Keys.Up)
MessageBox.Show(Keys.Up.ToString(), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
if (e.KeyCode == Keys.Left)
MessageBox.Show(Keys.Left.ToString(), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
if (e.KeyCode == Keys.Right)
MessageBox.Show(Keys.Right.ToString(), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
e.Handled = true; // e.handled is a property then assigned to true, it deletes the character from the keychar property value
}