12 Replies Latest reply on Jan 21, 2015 2:25 PM by Keith Rice

    Text alignment in a multi line Text Box

    JOHN GEORGE

      I am working on a solidworks API help example code and trying to get the result inside a textbox

      Below is the code and screen shot showing the immediate result window

      Because of the property names, the result alignments are different in certain lines

      Is it possible to align the " = " and result values vertically as shown in the second picture

       

      Debug.Print Chr$(9) & vCustInfoName & Chr$(9) & " = " & Chr$(9) & swModel.GetCustomInfoValue(vConfigName, vCustInfoName)
      

       

      Result

      immediate window.png

      To get like this

      final result.png

        • Re: Text alignment in a multi line Text Box
          Keith Rice

          I don't think the Format() is going to create a table like you want. The simplest approach would be to use commas and vbTab, like this:

           

          Sub main()
              Debug.Print "Contractor", "=", "CADSharp"
              Debug.Print "Project", "=", "SolidWorks API Training"
          End Sub
          

           

          Output:

           

          The comma automatically creates a "cell" with width equal  to 14 spaces. The downside is that if your line of text exceeds 14 characters, you'll have to add in additional commas in the "cells" that aren't longer than 14 spaces:

           

          Sub main()
              Debug.Print "Contractor Name", "=", "CADSharp"
              Debug.Print "Project", , "=", "SolidWorks API Training"
          End Sub
          

           

           

          If you want more control over the spacing, don't use commas at all and only use vbTab:

           

          Sub main()
              Debug.Print "Contractor" & vbTab & "=" & vbTab & vbTab & "CADSharp"
              Debug.Print "Project" & vbTab & vbTab & "=" & vbTab & vbTab & "SolidWorks API Training"
          End Sub
          

           

          Output:

           

           

          Alternatively, here is a function I wrote that will ensure that each item (separated by a delimiter) is separated by a certain number of tabs. I added a conditional, however, that reduces the column size for a "cell" that contains an equal sign. This offers a lot more control with minimal typing.

           

          Sub main()
              Debug.Print PrintColumns("Contractor|=|CADSharp", 4)
              Debug.Print PrintColumns("Project|=|SolidWorks API Training", 4)
          End Sub
          
          Function PrintColumns(text As String, columnWidthInTabs As Integer) As String
              Dim vSplit As Variant
              Dim currentTabCount As Integer
              Dim i As Integer, j As Integer
              
              vSplit = Split(text, "|")
              If UBound(vSplit) = -1 Then
                  PrintColumns = ""
                  Exit Function
              End If
              
              Dim neededTabs As Integer
              For i = 0 To UBound(vSplit)
                  If vSplit(i) = "=" Then
                      vSplit(i) = vSplit(i) & vbTab
                  Else
                      neededTabs = columnWidthInTabs - Len(vSplit(i)) \ 4
                      
                      For j = 1 To neededTabs
                          vSplit(i) = vSplit(i) & vbTab
                      Next j
                  End If
                  
                  PrintColumns = PrintColumns & vSplit(i)
              Next i
          End Function
          

           

          Output:

           

          Keith

          SolidWorks API Macros

          • Re: Text alignment in a multi line Text Box
            JOHN GEORGE

            Is it possible to add a RichTextBox to the UserForm in SW2015 VB?

            I looked thru the Form Controls and couldn't find one

            Any options?