简体   繁体   English

如何在C#中使用文本框将多个HEX值输入到数组中

[英]How to use a textbox to input multiple HEX values into an array in c#

I'm trying to use a textbox control so the user can input Hex values into it, press enter and it is stored in an array. 我正在尝试使用文本框控件,以便用户可以在其中输入十六进制值,然后按Enter键并将其存储在数组中。 Then enter another hex value press enter and it is stored in the array. 然后输入另一个十六进制值,按Enter键,它将存储在数组中。

I'm new to programming, so maybe there is a more suitable control than textbox to do this? 我是编程新手,所以也许有比文本框更合适的控件可以执行此操作?

This is the code I've come up with so far, I'm not sure how you would clear the textbox when the user presses enter though, maybe there is something more suitable to input the values? 这是我到目前为止提出的代码,但是我不确定在用户按下Enter键时如何清除文本框,也许还有一些更适合输入值的东西?

Any help would be appreciated! 任何帮助,将不胜感激!

    private void Test_TextChanged(object sender, EventArgs e)
    {

        string hexString = Test.Text;
        int num = Int32.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
        int[] arr = new int[20];

        for (uint i = 0; i < 5; i++)
        {

            arr[i] = num;
            ReadValue.Text = num.ToString();
        }

    }

You can use the NumericUpDown control and set its Hexadecimal property to true . 您可以使用NumericUpDown控件并将其Hexadecimal属性设置为true Than you can use his KeyDown event 比您可以使用他的KeyDown事件

private List<int> hexValues = new List<int>();

private void numericUpDown1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        hexValues.Add(Convert.ToInt32(numericUpDown1.Value));

        // Reset the value.
        numericUpDown1.Value = Decimal.Zero;
    }
}

Then if you need the array: 然后,如果需要数组:

int[] hexValuesArray = hexValues.ToArray();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM