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 =...
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.It is just a multiline TextBox. String with linefeeds can be assigned to Text property, or assign string array to Lines property.
TextBox.Multiline Property (System.Windows.Forms)
Gets or sets a value indicating whether this is a multiline TextBox control.docs.microsoft.com
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
It is and there is no "customisation". Aif that is really a multiline textbox how can I customize it to display like that? it looks like a receipt.
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.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:
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.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()
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.
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.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.
It's simply a matter of setting theI'm just gonna search now how to do fixed-width font.
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.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.Here's something I posted at another site a long time ago that demonstrates how you can format text in aTextBox
. You can use aStringBuilder
as I have here, or you can create aList(Of String)
and add your lines, then callToArray
and then assign the result to theLines
property.