Jimmy
Jimmy

Reputation: 569

Concatenate hex numeric updowns to string for a textbox

I have 4 numeric up down controls on a form. They are set to hexidecimal, maximum 255, so they'll each have values from 0 to FF. I'd like to concatenate these values into a string for a textbox.

Upvotes: 0

Views: 1100

Answers (2)

Chris Taylor
Chris Taylor

Reputation: 53699

You can do something like the following

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

Upvotes: 1

Hans Passant
Hans Passant

Reputation: 941455

Assuming you gave the NUDs their default names:

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

Upvotes: 0

Related Questions