• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

Smart Padding of Spaces

Here's a snippet of a .FRM file:
   Begin VB.Label lblDescription
      BackStyle       =   0  'Transparent
      Caption          =   "lblDescription"
      Height            =   255
      Left                =   120
      TabIndex        =   6
      Top                =   1560
      Width             =   4935
Observe how neatly the "=" are arranged in a vertical axis.Now suppose you're given this list -
BackStyle,Caption,Height,Left,TabIndex,Top & Width.How would you arrange it in precisely the same manner as above - with leading spaces & "="?
  • 2
1 Solution
If you ate working uder Visual Studio .net  pressing  Ctrl+k and then Ctrl+F should do the trick - this is the Autoformating function, also under Edit->Advanced.
Mike TomlinsonMiddle School Assistant TeacherCommented:
You have to use a MonoSpaced Font (like Courier or Courier New) and code such as the below.  If you don't want a hardcoded column width then you have to walk your data set first to determine the width of the largest item.  The code below outputs to Text1 which has its MultiLine property set to true.

Option Explicit

Private Sub Command1_Click()
    Dim separator As String
    Dim colWidth As Integer
    Dim item As Variant
    Dim items As Variant
    Dim values As Variant
    Text1.Text = ""
    separator = " = "
    colWidth = 15
    item = "BackStyle|0,Caption|""lbDescription"",Height|255,Left|120,TabIndex|6,Top|1560,Width|4935"
    items = Split(item, ",")
    For Each item In items
        values = Split(item, "|")
        Text1.Text = Text1.Text & trailingSpaces(values(0), colWidth) & separator & values(1) & vbCrLf
End Sub

Private Function trailingSpaces(ByVal strInput As String, ByVal columnWidth As Integer) As String
    Dim spacesNeeded As Integer
    spacesNeeded = columnWidth - Len(strInput)
    If spacesNeeded >= 0 Then
        trailingSpaces = strInput & Space(spacesNeeded) ' pad
        trailingSpaces = Left(strInput, columnWidth) ' truncate
    End If
End Function
EXwithRajAuthor Commented:
Hello Daniel,have you misunderstood my question?I am interested in a VB code that would perform the task that I'd explained.But your suggestion to press some hot-keys or set some menu options is indeed mystifying!What are you up to?
Mike TomlinsonMiddle School Assistant TeacherCommented:
I don't think it should be deleted with points refunded since my solution does what was asked.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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