tags:

views:

37

answers:

2

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.

A: 

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;
}
Hans Passant
No use writing so much code for so-less-details? questions :)
Nayan
When I use the resulting textbox text in this code:int param = int.Parse(txtARGB.Text, NumberStyles.AllowHexSpecifier);it results in a Format Exception was unhandled message:Input string was not in a correct format
Jimmy
@Jimmy: you have to use NumberStyles.HexNumber.
Hans Passant
@Nayan: read this: http://en.wikipedia.org/wiki/Don%27t_repeat_yourself
Hans Passant
Thanks, Hans. That did it.
Jimmy
@Hans: Thanks for the link. I know this stuff, more or less. I was just joking :)
Nayan
+1  A: 

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);
Chris Taylor