【问题标题】:How to use a textbox to input multiple HEX values into an array in c#如何使用文本框将多个 HEX 值输入到 C# 中的数组中
【发布时间】:2014-12-04 12:59:16
【问题描述】:

我正在尝试使用文本框控件,以便用户可以在其中输入十六进制值,按回车键并将其存储在数组中。然后输入另一个十六进制值按回车,它存储在数组中。

我是编程新手,所以也许有比文本框更合适的控件来执行此操作?

这是我到目前为止提出的代码,但我不确定当用户按下回车时如何清除文本框,也许有更适合输入值的东西?

任何帮助将不胜感激!

    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();
        }

    }

【问题讨论】:

  • 它必须是一个数组或者它可以是任何非固定大小的集合?
  • 可以是任何非固定大小的集合...

标签: c# arrays textbox hex windows-forms-designer


【解决方案1】:

您可以使用NumericUpDown 控件并将其Hexadecimal 属性设置为true。 比你可以使用他的 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;
    }
}

那么如果你需要数组:

int[] hexValuesArray = hexValues.ToArray();

【讨论】:

  • 如果用户输入值而不是使用向上/向下箭头,这会起作用吗?
  • @Brendan 当然!如果您使用 TextBox,则必须过滤输入字符,而无需使用 NumericUpDown 控件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-20
  • 2015-05-08
  • 1970-01-01
  • 1970-01-01
  • 2019-02-27
  • 1970-01-01
相关资源
最近更新 更多