none
Add an array of bytes to a ListBox RRS feed

  • Question

  • Hello

    I have an array of type byte, and I want to add it to a listbox.  How can I do that easily without looping construct?

    • Moved by CoolDadTx Monday, August 21, 2017 3:18 PM Winforms related
    Saturday, August 19, 2017 3:36 PM

Answers

  • Hi William,

    I must not have had enough coffee when I replied ... @Viorel's suggestion to use listBox1.DataSource = myByteArray was certainly the better answer (I pretty much always use a DataSource, so I have no idea why that didn't automatically pop into my head from the get-go). 

    So, then, the only other thing is the formatting. A FormatString of "X2" (as @Viorel suggests) will not get you "0xAA", it will only get you "AA". If you use "X4", unfortunately you will *still* not get what you want, because that will get you "00AA". I've been trying to figure out a way to get "0xAA", and I finally came up with this:

    First, you'll need to handle the Format event:

    this.listBox1.Format += ListBox1_Format;

    Which will look like this:

    private void ListBox1_Format(object sender, ListControlConvertEventArgs e)
    {
        int i = 0;
        if (int.TryParse(e.Value.ToString(), out i))
            e.Value = string.Format("0x{0}", i.ToString("X2"));
    }

    And, there you go ...


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by William256 Monday, August 21, 2017 3:48 PM
    Sunday, August 20, 2017 8:35 PM

All replies

  • You should be able to do it with the .AddRange() method, like this:

    this.listBox1.Items.AddRange(MyByteArray.Cast<object>().ToArray());


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, August 19, 2017 4:37 PM
  • We need some clarification:

    What kind of .NET Listbox: WinForms? WPF? UWP? ASP.Net? Other?

    Do you want to add the entire array as one element to the Listbox? Or each byte as a seperate entry?

    What do those bytes represent. i.e., how should the listbox display each element? (this ties into the first question).


    Remember to mark helpfull answers as helpfull and close threads by marking answers.


    Saturday, August 19, 2017 7:09 PM
  • Thanks for that tip. Works pretty slick.  One other thing I should have mentioned, how do I get my byte to be displayed in hex format, so 0xAA is displayed in the listbox as 0xAA and not decimal 170

    Thanks

    Sunday, August 20, 2017 4:05 PM
  • Try this way too:

    listBox1.FormatString = "X2";
    listBox1.DataSource = myByteArray;
    

    Sunday, August 20, 2017 4:42 PM
  • Hi William,

    I must not have had enough coffee when I replied ... @Viorel's suggestion to use listBox1.DataSource = myByteArray was certainly the better answer (I pretty much always use a DataSource, so I have no idea why that didn't automatically pop into my head from the get-go). 

    So, then, the only other thing is the formatting. A FormatString of "X2" (as @Viorel suggests) will not get you "0xAA", it will only get you "AA". If you use "X4", unfortunately you will *still* not get what you want, because that will get you "00AA". I've been trying to figure out a way to get "0xAA", and I finally came up with this:

    First, you'll need to handle the Format event:

    this.listBox1.Format += ListBox1_Format;

    Which will look like this:

    private void ListBox1_Format(object sender, ListControlConvertEventArgs e)
    {
        int i = 0;
        if (int.TryParse(e.Value.ToString(), out i))
            e.Value = string.Format("0x{0}", i.ToString("X2"));
    }

    And, there you go ...


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by William256 Monday, August 21, 2017 3:48 PM
    Sunday, August 20, 2017 8:35 PM
  • That's pretty slick.  Thanks for the find, Bonnie !!!
    Monday, August 21, 2017 3:49 PM
  • You're welcome, William. Glad I could help. =0)

    You should really give some kudos to @Viorel too ... since using a DataSource, in most cases, is better than simply using the .AddRange() method.

    However, what's also interesting is that the Format event handler will work either way!


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Monday, August 21, 2017 4:45 PM