[英]e.keychar returns ascii value. How do i get the char/int value?
Iam trying to get the interget value from e.KeyChar but it just gives me the ascii value.我试图从 e.KeyChar 获取 interget 值,但它只给了我 ascii 值。
To make it short.使其简短。
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!"); }
}
If i try to change one value in my array to say 49 and send "1" everything is fine.如果我尝试将数组中的一个值更改为 49 并发送“1”,则一切正常。
The problem origins from a larger set of textboxes where the user can put numeric values, but is not allowed to enter duplicate values.问题源于一组较大的文本框,用户可以在其中输入数值,但不允许输入重复值。 I planed to save the values in an integer array and then use contains to see if it already have been entered once.
我打算将这些值保存在一个整数数组中,然后使用 contains 来查看它是否已经输入过一次。
You can do the following to identify whether the pressed key is an Number and handle if the number is not there.您可以执行以下操作来识别按下的键是否为数字,如果数字不存在则处理。
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);
}
}
}
}
Try this, the game_KeyPress and game_KeyDown methods detect the pressed keys.试试这个, game_KeyPress 和 game_KeyDown 方法检测按下的键。 The control keys can be found in the Keys enumeration of windows forms, a trick you can do is put "switch (Keys)" and the automatic autocomplete of Visual studio 2019 will show all the enumerations included in Keys.
控制键可以在 windows 窗体的 Keys 枚举中找到,你可以做的一个技巧是输入“开关(键)”,Visual Studio 2019 的自动自动完成将显示 Keys 中包含的所有枚举。 Not all pressed keys are detected by the game_KeyPress method so we use game_KeyDown to detect arrows.
并非所有按下的键都被 game_KeyPress 方法检测到,因此我们使用 game_KeyDown 来检测箭头。 Some keys have their specific events.
一些键有其特定的事件。
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.