Microsoft KB Archive/831488

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

Article ID: 831488

Article Last Modified on 5/11/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic 2005

-



In this task

 * SUMMARY
 * Create a Microsoft Windows Application project
 * Use the PadLeft (Integer, Char) method to right-align strings when you print
 * Use the PadLeft (Integer) method to right-align strings when you print
 * Complete code listing (Form1.vb)
 * Verify that the application works
 * REFERENCES



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.
 * 1) Under Project Types, click Visual Basic Projects.

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

By default, the Form1 form is created.
 * 1) Add two Button controls to Form1.

By default, the Button1 control and the Button2 control are added to Form1.
 * 1) 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:  Double-click Button1.  Add the following code to the Button1_Click event-handler: Dim st As String = &quot;6.25&quot; Dim st2 As String = &quot;14.25&quot; Dim dt As String = &quot;|&quot; Dim st1 As String = &quot;$&quot; Dim mt As String = st1 + st + dt Dim mt1 As String = st1 + st2 + dt Dim pt As Char = &quot; &quot; ListBox1.Items.Add(&quot;|&quot; + mt.PadLeft(20 - Len(st), pt)) ListBox1.Items.Add(&quot;|&quot; + 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:  In the drop-down list box in the upper-left corner of Visual Studio .NET Editor, click to select the Button2 method. In the drop-down list box in the upper-right corner of Visual Studio .NET Editor, click to select the corresponding Click event.  Add the following code to the Button2_Click event-handler: Dim str As String = &quot;6.25&quot; Dim str1 As String = &quot;14.25&quot; Dim str2 As String = &quot;12345.6789&quot; ListBox1.Items.Add(&quot;K&quot; & str.PadLeft(20 - Len(str)) & &quot;K&quot;) ListBox1.Items.Add(&quot;K&quot; & str1.PadLeft(20 - Len(str1)) & &quot;K&quot;) ListBox1.Items.Add(&quot;K&quot; & str2.PadLeft(20 - Len(str2)) & &quot;K&quot;) 

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


 * 1) Region &quot; Windows Form Designer generated code &quot;

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 = &quot;Button1&quot; Me.Button1.TabIndex = 0 Me.Button1.Text = &quot;Button1&quot; '       'Button2 '       Me.Button2.Location = New System.Drawing.Point(176, 8) Me.Button2.Name = &quot;Button2&quot; Me.Button2.TabIndex = 1 Me.Button2.Text = &quot;Button2&quot; '       'ListBox1 '       Me.ListBox1.Location = New System.Drawing.Point(72, 136) Me.ListBox1.Name = &quot;ListBox1&quot; 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 = &quot;Form1&quot; Me.Text = &quot;Form1&quot; Me.ResumeLayout(False)

End Sub


 * 1) End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim st As String = &quot;6.25&quot; Dim st2 As String = &quot;14.25&quot; Dim dt As String = &quot;|&quot; Dim st1 As String = &quot;$&quot; Dim mt As String = st1 + st + dt       Dim mt1 As String = st1 + st2 + dt        Dim pt As Char = &quot; &quot; ListBox1.Items.Add(&quot;|&quot; + mt.PadLeft(20 - Len(st), pt)) ListBox1.Items.Add(&quot;|&quot; + 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 = &quot;6.25&quot; Dim str1 As String = &quot;14.25&quot; Dim str2 As String = &quot;1245.6789&quot; ListBox1.Items.Add(&quot;K&quot; & str.PadLeft(20 - Len(str)) & &quot;K&quot;) ListBox1.Items.Add(&quot;K&quot; & str1.PadLeft(20 - Len(str1)) & &quot;K&quot;) ListBox1.Items.Add(&quot;K&quot; & str2.PadLeft(20 - Len(str2)) & &quot;K&quot;)

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:

http://msdn2.microsoft.com/en-us/library/ms379584(vs.80).aspx

For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:

http://msdn2.microsoft.com/en-us/library/ms171843.aspx

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.
 * 1) 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

<div class="references_section">