Insert whitespace between characters in listbox

I am trying to insert items into a listbox in my asp.net C# application

I concatenate some values and put whitespace in between them but it doesn’t show up in the listbox.

        ListItem lt = new ListItem();
        lt.Text = ItemName + "    " + barcode + "    " + price; // problem
        lt.Value = barcode;
        lstMailItems.Items.Add(lt);

i even tried

lt.Text = ItemName + "tt" + barcode + "tt" + price; // problem
lt.Text = ItemName + "& nbsp;" + barcode + "& nbsp;" + price; // &nbsp shows up as text

but that even doesn’t seem to work. How can I put whitespace between these strings so that it shows up in the listbox as well

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

string spaces = Server.HtmlDecode("    "); 

lt.Text = ItemName + spaces + barcode + spaces + price; // works

Method 2

I had the same issue and the above answers led me to this which worked for me.

string space = " ";
                space = Server.HtmlDecode(space);
                line = line.Replace(" ", space);
                ClassCodeListBox.Items.Add(line);

Method 3

Try

lt.Text = string.Format("{0} {1} {2}",ItemName,barcode,price);

Replace with &nbsp If you cannot see.

Or

lt.Text = string.Format("{0} {1} {2}",ItemName,barcode,price);

Method 4

Here are two examples that work well, and how to get the current formatted:

  var SaleItem = new
    {
        name = "Super Cereal",
        barcode = "0000222345",
        price = 2.55m
    };

    ListItem lt = new ListItem();
    string space = " ";
    lt.Text = String.Concat(SaleItem.name, 
        space, SaleItem.barcode, space, SaleItem.price);
    lt.Value = SaleItem.barcode;

    ListItem lt2 = new ListItem();
    lt2.Text = string.Copy(String.Format("{0}: {1} {2}", 
               SaleItem.name, SaleItem.barcode, SaleItem.price.ToString("C")));
    lt2.Value = SaleItem.barcode;

    lstMailItems.Items.Add(lt);
    lstMailItems.Items.Add(lt2);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x