Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 208
  • Last Modified:

Lining up display data

Hi All,

I use the following to display some information on a label which comes out like this:

Colton = 800
Bloomington = 2700
 etc....

Is there any way that I might be able to change this so that the = signs and the numbers line up evenly?

Dim txtSubCity As String = ""
        For Each dRow As DataRow In das5.Tables("Subscribers").Rows
            txtSubCity = txtSubCity & dRow("SubCity").toString & " = " & dRow("Number_Of_Subscribers").toString & vbCrLf
        Next
        lblSubCity.Text = txtSubCity

Thanks for your help.
0
maralans
Asked:
maralans
  • 6
  • 4
1 Solution
 
S-TwilleyCommented:
simplest way would be to use a fixed width font... then pad with spaces to line up the "=" sign... this would line them up exactly...   it is still possible with other fonts in a texbox... but the line up won't be exact (just close).  Which would you prefer/be after?
0
 
maralansAuthor Commented:


Show me how with a fixed width font so they line up exactly.  I am not familiar with those.
0
 
S-TwilleyCommented:
Ok, if you set the textbox font to a fixed-width font like "Lucida Console" or "Courier New"... the following code should work.  (This is assuming that you have multiline set to true, and you might also want to turn word-wrap off)

        Dim maxLen As Integer = -1
        Dim currLine As String
        Dim currEqPos As Integer

        For Each currLine In TextBox1.Lines
            currEqPos = currLine.IndexOf("=")
            If currEqPos > maxLen Then maxLen = currEqPos
        Next

        Dim currLineIndex As Integer

        If maxLen > -1 Then
            Dim TextLines() As String = TextBox1.Lines
            For currLineIndex = 0 To TextLines.Length - 1
                currLine = TextLines(currLineIndex)
                currEqPos = currLine.IndexOf("=")
                If currEqPos >= 0 Then
                    currLine = New String(" ", maxLen - currEqPos) & currLine
                    TextLines(currLineIndex) = currLine
                End If

            Next
            TextBox1.Lines = TextLines
        End If

===========

I'll try and write the code for non fixed-width font's now
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
maralansAuthor Commented:
That is pretty impressive.   I pasted the info below so it may not look right.  The = signs and the numbers line up exactly but the cities are intended to the right.  Is this the way its suppose to be? Its, not a big deal unless its an easy fix.  


BLOOMINGTON = 851
        COLTON  = 2451
       FONTANA = 3931
 GRAND TERRACE = 780
   LYTLE CREEK = 102
        RIALTO = 3538
     RIVERSIDE = 18
SAN BERNARDINO = 1

0
 
S-TwilleyCommented:
Did you want it more like:

BLOOMINGTON    = 851
COLTON             = 2451
FONTANA           = 3931
GRAND TERRACE = 780
LYTLE CREEK       = 102
RIALTO               = 3538
RIVERSIDE           = 18
SAN BERNARDINO = 1
0
 
maralansAuthor Commented:
if its an easy fix yes but if not this will work.
0
 
S-TwilleyCommented:
       Dim maxLen As Integer = -1
        Dim currLine As String
        Dim currEqPos As Integer

        For Each currLine In TextBox1.Lines
            currLine = currLine.TrimStart(" ")
            currEqPos = currLine.IndexOf("=")
            If currEqPos > maxLen Then maxLen = currEqPos
        Next

        Dim currLineIndex As Integer

        If maxLen > -1 Then
            Dim TextLines() As String = TextBox1.Lines
            For currLineIndex = 0 To TextLines.Length - 1
                currLine = TextLines(currLineIndex)
                currLine = currLine.TrimStart(" ")
                currEqPos = currLine.IndexOf("=")

                If currEqPos >= 0 Then
                    currLine = currLine.Insert(currEqPos, New String(" ", maxLen - currEqPos))
                    TextLines(currLineIndex) = currLine
                End If

            Next
            TextBox1.Lines = TextLines
        End If

=============

I did that quickly... you may want to test it more
0
 
maralansAuthor Commented:
Perfect.  Absolutely perfect.  Thanks for all your help.
0
 
S-TwilleyCommented:
No problem at all
0
 
S-TwilleyCommented:
Hi... I've also done this one below... this should work with other fonts... it measure the width depending on the font set to textbox1 (so you'd need to modify it should you change the textbox, or want to use it for more than one control)... this still works with fixed width fonts... but also works with others now. I will say if you're happy with fixed width fonts then use my previous implementation as it is less complex and less likely to cause undesired results... but you can have a go with this one (another note... since it can't line up exactly, sometimes the equals is shifted slightly ahead of the "longest" equals position as that is the closest match... so if you were to try lining up again, it would keep shifting forward ... you'll see whati mean):



    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim TextLines() As String = TextBox1.Lines
        Dim CurrentLine As String
        Dim CurrentLineIndex As Integer
        Dim EqualsPosition As Integer

        Dim EqualsString As String

        Dim EqualsStringLength As Double
        Dim MaxEqualsStringLength As Double = -1

        Dim PaddedNumber As Integer
        Dim PaddedString As String
        Dim PaddedLength As Double

        Dim ReplaceString As String
        Dim ReplaceLength As Double

        For Each CurrentLine In TextLines
            CurrentLine = CurrentLine.TrimStart(" ")
            EqualsPosition = CurrentLine.IndexOf("=")
            If EqualsPosition >= 0 Then
                EqualsStringLength = TextLength(CurrentLine.Substring(0, EqualsPosition + 1))
                If MaxEqualsStringLength < EqualsStringLength Then MaxEqualsStringLength = EqualsStringLength
            End If
        Next

        If MaxEqualsStringLength > -1 Then
            For CurrentLineIndex = 0 To TextLines.Length - 1
                CurrentLine = TextLines(CurrentLineIndex)
                CurrentLine = CurrentLine.TrimStart(" ")
                EqualsPosition = CurrentLine.IndexOf("=")

                If EqualsPosition >= 0 Then
                    EqualsString = CurrentLine.Substring(0, EqualsPosition + 1)
                    EqualsStringLength = TextLength(EqualsString)

                    If EqualsStringLength < MaxEqualsStringLength Then
                        Dim Forever As Boolean = True

                        PaddedNumber = 0
                        ReplaceString = ""
                        ReplaceLength = 0

                        While Forever
                            PaddedNumber += 1
                            PaddedString = EqualsString.Insert(EqualsPosition, New String(" ", PaddedNumber))
                            PaddedLength = TextLength(PaddedString)

                            If Math.Abs(MaxEqualsStringLength - PaddedLength) < Math.Abs(MaxEqualsStringLength - ReplaceLength) Then
                                ReplaceString = PaddedString
                                ReplaceLength = PaddedLength
                            End If

                            If PaddedLength > MaxEqualsStringLength Then Exit While
                        End While

                        CurrentLine = CurrentLine.Replace(EqualsString, ReplaceString)
                        TextLines(CurrentLineIndex) = CurrentLine
                    End If

                End If
            Next
            TextBox1.Lines = TextLines
        End If
    End Sub

    Function TextLength(ByVal sText As String) As Double
        Dim gTextbox As Graphics = TextBox1.CreateGraphics
        Dim newStringFormat As New StringFormat()
        Dim rSizeF As SizeF
        newStringFormat.FormatFlags = StringFormatFlags.MeasureTrailingSpaces

        rSizeF = gTextbox.MeasureString(sText, TextBox1.Font, 10000, newStringFormat)
        Return rSizeF.Width
    End Function
0
 
farsightCommented:
Here's how I'd do it.  (using a fixed-width font)

Key idea: String.Format()   or StringBuilder.AppendFormat()
Additional ideas:  StringBuilder to accumulate text, System.Environment.NewLine

        ' Right justified with room for 10 chars.
        ' (for left justified, use -10)
        Dim fmt As String = "{0,10} = {1}" & System.Environment.NewLine
        Dim sb As New System.Text.StringBuilder
        For Each dRow As DataRow In das5.Tables("Subscribers").Rows
            sb.AppendFormat(fmt, dRow("SubCity").ToString, dRow("Number_Of_Subscribers").ToString)
            ' Another very similar alternative.
            'sb.Append(String.Format(fmt, dRow("SubCity").ToString, dRow("Number_Of_Subscribers").ToString))
        Next
        lblSubCity.Text = sb.ToString
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now