Resolved how to display text in a listbox?

LMCKC207

Member
Joined
Apr 9, 2020
Messages
17
Programming Experience
Beginner
Hello, I need some help, please. I'm trying to replicate this image but I have no idea how to do the squared one. It is a list box but I don't know how to display the text like that inside of that.
vb.jpg
 
Solution
Here's something I posted at another site a long time ago that demonstrates how you can format text in a TextBox. You can use a StringBuilder as I have here, or you can create a List(Of String) and add your lines, then call ToArray and then assign the result to the Lines property.
Lots of people ask how to display text formatted in columns. It's generally a bad idea to display a solid block of text formatted that way. More often you should use a grid control or a ListView, but if you really want to display tabular data in a TextBox, or perhaps in a file with fixed-width columns, then you'd do it much like this:
VB.NET:
Dim builder As New...

LMCKC207

Member
Joined
Apr 9, 2020
Messages
17
Programming Experience
Beginner
It is just a multiline TextBox. String with linefeeds can be assigned to Text property, or assign string array to Lines property.
Hello JohnH, yes that's what I thought at first. It was a multiline textbox or a richtextbox? something like that but my friend told me it was a listbox so I'm kinda confuse. Also, if that is really a multiline textbox how can I customize it to display like that? it looks like a receipt.

In addition, I tried to recreate this last night and I got it like that but I don't know if this correct.

CODE FOR THE BUTTON TO DISPLAY IN THE LISTBOX:
    Private Sub btnEnterVal_Click(sender As Object, e As EventArgs) Handles btnEnterVal.Click


        lstDisplay.Items.Insert(0, "NAME: " + txtName.Text + vbTab + "CONTACT NO: " + txtContact.Text)
        lstDisplay.Items.Insert(1, "ADDRESS: " + txtAddress.Text + vbTab + "AGE: " + txtAge.Text)
        lstDisplay.Items.Insert(2, "  --------------------------------------------------------------------------------")
        lstDisplay.Items.Insert(3, "MOVIE TITLE: " + cmbMovie.SelectedItem)
        lstDisplay.Items.Insert(4, "CASH: " + txtCash.Text)
        lstDisplay.Items.Insert(5, "NO. OF TICKETS: " + txtTickets.Text)
        lstDisplay.Items.Insert(6, "TOTAL: " + txtTotal.Text)
        lstDisplay.Items.Insert(7, "CHANGE: " + txtChange.Text)


    End Sub


Form1 21 Oct 2020 9_04_37 AM.png
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,350
Location
Sydney, Australia
Programming Experience
10+
if that is really a multiline textbox how can I customize it to display like that? it looks like a receipt.
It is and there is no "customisation". A TextBox simply displays the specified text in the specified font. That's it, that's all. You need to provide the actual text that you want displayed. If you expect whitespace to be displayed, you need to include whitespace in your text.

It's also worth noting that the original is obviously not using a fixed-width font, which is usually bad in such scenarios. If your spaces are very narrow compared to other characters then it's hard to get things to even approximately line up and impossible to get them lined up exactly. If you use a fixed-width font then the correct number of spaces will cause text to line up as though in actual columns.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,350
Location
Sydney, Australia
Programming Experience
10+
Here's something I posted at another site a long time ago that demonstrates how you can format text in a TextBox. You can use a StringBuilder as I have here, or you can create a List(Of String) and add your lines, then call ToArray and then assign the result to the Lines property.
Lots of people ask how to display text formatted in columns. It's generally a bad idea to display a solid block of text formatted that way. More often you should use a grid control or a ListView, but if you really want to display tabular data in a TextBox, or perhaps in a file with fixed-width columns, then you'd do it much like this:
VB.NET:
Dim builder As New System.Text.StringBuilder
Dim format As String = "{0,-4}{1,-10}{2,-12:d}{3,8}"

builder.AppendFormat(format, "ID", "Name", "DOB", "Children")
builder.AppendLine()
builder.AppendFormat(format, 1, "Peter", #6/19/1969#, 2)
builder.AppendLine()
builder.AppendFormat(format, 2, "Paul", #4/23/1974#, 4)
builder.AppendLine()
builder.AppendFormat(format, 3, "Mary", #12/4/1980#, 1)
builder.AppendLine()

Me.TextBox1.Text = builder.ToString()
The secret there is the format string. You can use a format string with a StringBuilder.AppendFormat, Console.WriteLine, String.Format, StreamWriter.WriteLine and other places, so your output options are many. Let's take a closer look at that format string.

It includes 4 format specifiers, so it will accept 4 values to format. You'll see that each call to AppendFormat passes the format string followed by 4 values. Funny, that. ;)

The first format specifier ({0,-4}) takes the first value ({0,-4}) and pads it out with whitespace to 4 characters ({0,-4}), left-aligning the string ({0,-4}).

The second format specifier take the second value, pads it out to 10 characters and left-aligns it.

The third format specifier ({2,-12:d}) takes the third value, pads it out to 12 characters, left-aligns it and displays it in the short date format ({2,-12:d}).

The fourth format specifier takes fourth value and pads it out to 8 characters, but this time the field width is greater than zero so it is right-aligned.

Note that to actually view the output in columns you must use a fixed-width font. Notepad will do that by default but if, if you're displaying the data in a TextBox, you must set the Font to Courier New or some other appropriate value.
 
Solution

LMCKC207

Member
Joined
Apr 9, 2020
Messages
17
Programming Experience
Beginner
It's also worth noting that the original is obviously not using a fixed-width font, which is usually bad in such scenarios. If your spaces are very narrow compared to other characters then it's hard to get things to even approximately line up and impossible to get them lined up exactly. If you use a fixed-width font then the correct number of spaces will cause text to line up as though in actual columns.
Hello jmcilhinney, Thank you for mentioning about the fixed-width font thing. I managed to recreate the design though and when I'm testing it, the alignment of the text inside is not looking good especially if your texts are long. it's really ugly.

I'm about to ask that. thank you for pointing that out beforehand. I'm just gonna search now how to do fixed-width font.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,350
Location
Sydney, Australia
Programming Experience
10+
I'm just gonna search now how to do fixed-width font.
It's simply a matter of setting the Font property of the TextBox to an appropriate value. Probably the best-known fixed-width font is Courier New, but there are other options too.
 

LMCKC207

Member
Joined
Apr 9, 2020
Messages
17
Programming Experience
Beginner
Here's something I posted at another site a long time ago that demonstrates how you can format text in a TextBox. You can use a StringBuilder as I have here, or you can create a List(Of String) and add your lines, then call ToArray and then assign the result to the Lines property.
Thank you for this. I saw alike of this on yt where he uses the format string to display table in a listbox. Thank you for the help.

Just curious though, What I have done, is that correct?
 
Top Bottom