![](/img/trans.png)
[英]How do I convert e.KeyChar to an actual character value when handling keyboard events?
[英]e.keychar returns ascii value. How do i get the char/int value?
我試圖從 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 來查看它是否已經輸入過一次。
您可以執行以下操作來識別按下的鍵是否為數字,如果數字不存在則處理。
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);
}
}
}
}
試試這個, game_KeyPress 和 game_KeyDown 方法檢測按下的鍵。 控制鍵可以在 windows 窗體的 Keys 枚舉中找到,你可以做的一個技巧是輸入“開關(鍵)”,Visual Studio 2019 的自動自動完成將顯示 Keys 中包含的所有枚舉。 並非所有按下的鍵都被 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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.