将十六进制数字更新连接到文本框的字符串

时间:2010-04-15 19:30:51

标签: c# winforms

我在表单上有4个数字向上控件。它们被设置为十六进制,最大值为255,因此它们每个都具有从0到FF的值。我想将这些值连接成一个文本框的字符串。

2 个答案:

答案 0 :(得分:1)

您可以执行以下操作

textBox1.Text = string.Format("{0:X2}{1:X2}{2:X2}{3:X2}", 
        (int)numericUpDown1.Value, 
        (int)numericUpDown2.Value,
        (int)numericUpDown3.Value,
        (int)numericUpDown4.Value);

答案 1 :(得分:0)

假设你给了NUD他们的默认名字:

private void button1_Click(object sender, EventArgs e) {
  string txt = "";
  for (int ix = 1; ix <= 4; ++ix) {
    var nud = Controls["numericUpDown" + ix] as NumericUpDown;
    int hex = (int)nud.Value;
    txt += hex.ToString("X2");
  }
  textBox1.Text = txt;
}