How to right-align strings when you print by using Visual Basic .NET or Visual Basic 2005 (831488)



The information in this article applies to:

  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual Basic 2005

SUMMARY

This step-by-step article describes how to right-align strings when you print by using Microsoft Visual Basic .NET or Microsoft Visual Basic 2005.

You may use the following methods to right-align strings when you print by using Visual Basic .NET or Visual Basic 2005:
  • The PadLeft (Integer, Char) method
  • The PadLeft (Integer) method
back to the top

Create a Microsoft Windows Application project

To create a Windows Application project, follow these steps:
  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.

    The New Project dialog box appears.
  3. Under Project Types, click Visual Basic Projects.

    Note In Visual Studio 2005, click Visual Basic instead of Visual Basic Projects.
  4. Under Templates, click Windows Application, and then click OK.

    By default, the Form1 form is created.
  5. Add two Button controls to Form1.

    By default, the Button1 control and the Button2 control are added to Form1.
  6. Add a ListBox control to Form1.

    By default, the ListBox1 control is added to Form1.
back to the top

Use the PadLeft (Integer, Char) method to right-align strings when you print

You can use the Items property of a ListBox control to bind all the list items to the ListBox control. The PadLeft (Integer, Char) method right-aligns the characters in a string by padding the characters with spaces on the left for a specified total length. To use this procedure, follow these steps:
  1. Double-click Button1.
  2. Add the following code to the Button1_Click event-handler:
    Dim st As String = "6.25"
    Dim st2 As String = "14.25"
    Dim dt As String = "|"
    Dim st1 As String = "$"
    Dim mt As String = st1 + st + dt
    Dim mt1 As String = st1 + st2 + dt
    Dim pt As Char = " "
    ListBox1.Items.Add("|" + mt.PadLeft(20 - Len(st), pt))
    ListBox1.Items.Add("|" + mt1.PadLeft(20 - Len(st2), pt))
back to the top

Use the PadLeft (Integer) method to right-align strings when you print

You can use the overloaded version of the PadLeft (Integer) method to right-align strings when you print. This version of the PadLeft method right-aligns the characters in a string by padding the characters with spaces on the left for a specified total length. To use this procedure, follow these steps:
  1. In the drop-down list box in the upper-left corner of Visual Studio .NET Editor, click to select the Button2 method.
  2. In the drop-down list box in the upper-right corner of Visual Studio .NET Editor, click to select the corresponding Click event.
  3. Add the following code to the Button2_Click event-handler:
    Dim str As String = "6.25"
    Dim str1 As String = "14.25"
    Dim str2 As String = "12345.6789"
    ListBox1.Items.Add("K" & str.PadLeft(20 - Len(str)) & "K")
    ListBox1.Items.Add("K" & str1.PadLeft(20 - Len(str1)) & "K")
    ListBox1.Items.Add("K" & str2.PadLeft(20 - Len(str2)) & "K")
back to the top

Complete code listing (Form1.vb)

Note The comments in the following code listing were auto-generated by Visual Studio .NET.
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.Button2 = New System.Windows.Forms.Button()
        Me.ListBox1 = New System.Windows.Forms.ListBox()
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(16, 8)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Button1"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(176, 8)
        Me.Button2.Name = "Button2"
        Me.Button2.TabIndex = 1
        Me.Button2.Text = "Button2"
        '
        'ListBox1
        '
        Me.ListBox1.Location = New System.Drawing.Point(72, 136)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.ListBox1.Size = New System.Drawing.Size(120, 95)
        Me.ListBox1.TabIndex = 3
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.ListBox1, Me.Button2, Me.Button1})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim st As String = "6.25"
        Dim st2 As String = "14.25"
        Dim dt As String = "|"
        Dim st1 As String = "$"
        Dim mt As String = st1 + st + dt
        Dim mt1 As String = st1 + st2 + dt
        Dim pt As Char = " "
        ListBox1.Items.Add("|" + mt.PadLeft(20 - Len(st), pt))
        ListBox1.Items.Add("|" + mt1.PadLeft(20 - Len(st2), pt))

   End  Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim str As String = "6.25"
        Dim str1 As String = "14.25"
        Dim str2 As String  = "1245.6789"
        ListBox1.Items.Add("K" & str.PadLeft(20 - Len(str)) & "K")
        ListBox1.Items.Add("K" & str1.PadLeft(20 - Len(str1)) & "K")
        ListBox1.Items.Add("K" & str2.PadLeft(20 - Len(str2)) & "K")


    End Sub

End Class
Note You must change the code in Visual Basic 2005. By default, Visual Basic creates two files for the project when you create a Windows Forms project. If the form is named Form1, the two files that represent the form are named Form1.vb and Form1.Designer.vb. You write the code in the Form1.vb file. The Windows Forms Designer writes the code in the Form1.Designer.vb file. The Windows Forms Designer uses the partial keyword to divide the implementation of Form1 into two separate files. This behavior prevents the designer-generated code from being interspersed with your code.

For more information about the new Visual Basic 2005 language enhancements, visit the following Microsoft Developer Network (MSDN) Web site: For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site: back to the top

Verify that the application works

  1. In Visual Studio .NET or Visual Studio 2005, click Start on the Debug menu to run the application.

    Form1 appears.
  2. Click both Button1 and Button2 to verify that the text is right-aligned when you print strings in the ListBox control.
back to the top

REFERENCES

For additional information, click the following article numbers to view the articles in the Microsoft Knowledge Base:

217012 HOWTO: Format Strings to Right-Justify When Printing

95945 How to Right Justify Numbers Using Format$

back to the top

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbprint kbString kbAppDev kbformat kbHOWTOmaster KB831488 kbAudDeveloper