开发者

Concatenate hex numeric updowns to string for a textbox

开发者 https://www.devze.com 2022-12-26 19:33 出处:网络
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

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.


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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号