Question distances between chars

andrews

Well-known member
Joined
Nov 22, 2011
Messages
132
Programming Experience
5-10
Let us take
  Dim reeks1, reeks2 As String
        reeks1 = "11111"
        reeks2 = "1111111111"
        reeks1 = reeks1 & Space(30 - reeks1.Length) & "?" & vbCrLf
        reeks2 = reeks2 & Space(30 - reeks2.Length) & "?"
        MessageBox.Show(reeks1 & reeks2)
        Using fs As New FileStream("res.txt", FileMode.Create)
            Using sw As New StreamWriter(fs)
                sw.WriteLine(reeks1 & reeks2)
            End Using
        End Using
    End Sub

Why are the distances for "?" different on screen
But one I write this in a text file then the distances are the same.
Can I make the distances the same on the screen and in the text file.?
Thanks for any responce.
 
Last edited by a moderator:
But one I write this in a text file then the distances are the same.

Let me guess. What you actually did was open the text file in Notepad, right? The difference you're talking about has nothing to do with the text file and everything to do with how the two applications, i.e. your VB app and Notepad, display the text on screen. By default, Notepad displays using a fixed-width font, i.e. a font that uses the same width for each and every character. That means that two strings that contain the same number of characters will be displayed with the same width, regardless of what those characters are. When you display a MessageBox in VB, it uses a variable-width font. That means that the width of a displayed string will depend on what characters it contains. Generally speaking, a space will be quite narrow in a variable-width font.

You may be able to add Tab characters, i.e. ControlChars.Tab, to insert fixed-width spacing into your strings but I'm not sure whether a MessageBox will display them as desired. You may need to use your own form instead of a MessageBox and use a fixed-width font yourself. Note that the most well-known fixed-width font is Courier New, but there are others too.
 
Back
Top