e.keychar returns ascii 值。我如何获得 char/int 值?

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 枚举中找到,你可以做的一个技巧是输入 "switch (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
    }