?
Solved

Lining up display data

Posted on 2005-03-13
11
Medium Priority
?
201 Views
Last Modified: 2006-11-17
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
Comment
Question by:maralans
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13530233
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
 
LVL 5

Author Comment

by:maralans
ID: 13530257


Show me how with a fixed width font so they line up exactly.  I am not familiar with those.
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13530302
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:maralans
ID: 13530380
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
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13530392
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
 
LVL 5

Author Comment

by:maralans
ID: 13530402
if its an easy fix yes but if not this will work.
0
 
LVL 12

Accepted Solution

by:
S-Twilley earned 2000 total points
ID: 13530424
       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
 
LVL 5

Author Comment

by:maralans
ID: 13530440
Perfect.  Absolutely perfect.  Thanks for all your help.
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13530441
No problem at all
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13530509
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
 
LVL 12

Expert Comment

by:farsight
ID: 13533355
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month8 days, 15 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question