Hi,
I should like to submit this for the vb.NET code bank.
Make a RichTextBox with added text alignment: 'Justify'.
This was designed by Passel in December 2014 in response to a request.
I have used it many time since but only today did I check to see if it was in the code bank.
Start a new project.
Add a new Class, call it: SmartBox
Paste this code in the new class.
________________________________________________________________________________________________Code:Imports System.Windows.Forms Imports System.Runtime.InteropServices ' Represents a standard RichTextBox with some minor added functionality. ' SmartBox provides methods to maintain performance while it is being updated. ' Additional formatting features have also been added. Public Class SmartBox Inherits RichTextBox ' Maintains performance while updating. ' Remember to call EndUpdate when you are finished with the update. ' Nested calls are supported. ' Calling this method will prevent redrawing. ' It will also setup the event mask of the underlying richedit ' control so that no events are sent. Public Sub BeginUpdate() ' Deal with nested calls. updating += 1 If updating > 1 Then Return End If ' Prevent the control from raising any events. oldEventMask = SendMessage(New HandleRef(Me, Handle), EM_SETEVENTMASK, 0, 0) ' Prevent the control from redrawing itself. SendMessage(New HandleRef(Me, Handle), WM_SETREDRAW, 0, 0) End Sub Public Sub EndUpdate() ' Resumes drawing and event handling. ' This method should be called every time a call is made ' made to BeginUpdate. It resets the event mask to it's ' original value and enables redrawing of the control. updating -= 1 ' Deal with nested calls. If updating > 0 Then Return End If SendMessage(New HandleRef(Me, Handle), WM_SETREDRAW, 1, 0) ' Allow the control to redraw itself. SendMessage(New HandleRef(Me, Handle), EM_SETEVENTMASK, 0, oldEventMask) ' Allow the control to raise event messages. End Sub Public Shadows Property SelectionAlignment() As TextAlign ' Gets or sets the alignment to apply to the current selection or insertion point. ' Replaces the SelectionAlignment from RichTextBox Get Dim fmt As New PARAFORMAT() fmt.cbSize = Marshal.SizeOf(fmt) ' Get the alignment. SendMessage(New HandleRef(Me, Handle), EM_GETPARAFORMAT, SCF_SELECTION, fmt) ' Default to Left align. If (fmt.dwMask And PFM_ALIGNMENT) = 0 Then Return TextAlign.Left End If Return CType(fmt.wAlignment, TextAlign) End Get Set(value As TextAlign) Dim fmt As New PARAFORMAT() fmt.cbSize = Marshal.SizeOf(fmt) fmt.dwMask = PFM_ALIGNMENT fmt.wAlignment = CShort(value) ' Set the alignment. SendMessage(New HandleRef(Me, Handle), EM_SETPARAFORMAT, SCF_SELECTION, fmt) End Set End Property Protected Overrides Sub OnHandleCreated(e As EventArgs) ' This member overrides OnHandleCreated. MyBase.OnHandleCreated(e) ' Enable support for justification. SendMessage(New HandleRef(Me, Handle), EM_SETTYPOGRAPHYOPTIONS, TO_ADVANCEDTYPOGRAPHY, TO_ADVANCEDTYPOGRAPHY) End Sub Private updating As Integer = 0 Private oldEventMask As Integer = 0 ' Constants from the Platform SDK. Private Const EM_SETEVENTMASK As Integer = 1073 '1073 Private Const EM_GETPARAFORMAT As Integer = 1085 '1085 Private Const EM_SETPARAFORMAT As Integer = 1095 '1095 Private Const EM_SETTYPOGRAPHYOPTIONS As Integer = 1226 '1226 Private Const WM_SETREDRAW As Integer = 11 '11 Private Const TO_ADVANCEDTYPOGRAPHY As Integer = 1 '1 Private Const PFM_ALIGNMENT As Integer = 8 '8 Private Const SCF_SELECTION As Integer = 1 '1 ' It makes no difference if we use PARAFORMAT or ' PARAFORMAT2 here, so I have opted for PARAFORMAT2. <StructLayout(LayoutKind.Sequential)> _ Private Structure PARAFORMAT Public cbSize As Integer Public dwMask As UInteger Public wNumbering As Short Public wReserved As Short Public dxStartIndent As Integer Public dxRightIndent As Integer Public dxOffset As Integer Public wAlignment As Short Public cTabCount As Short <MarshalAs(UnmanagedType.ByValArray, SizeConst:=32)> _ Public rgxTabs As Integer() ' PARAFORMAT2 from here onwards. Public dySpaceBefore As Integer Public dySpaceAfter As Integer Public dyLineSpacing As Integer Public sStyle As Short Public bLineSpacingRule As Byte Public bOutlineLevel As Byte Public wShadingWeight As Short Public wShadingStyle As Short Public wNumberingStart As Short Public wNumberingStyle As Short Public wNumberingTab As Short Public wBorderSpace As Short Public wBorderWidth As Short Public wBorders As Short End Structure <DllImport("user32", CharSet:=CharSet.Auto)> _ Private Shared Function SendMessage(hWnd As HandleRef, msg As Integer, wParam As Integer, lParam As Integer) As Integer End Function <DllImport("user32", CharSet:=CharSet.Auto)> _ Private Shared Function SendMessage(hWnd As HandleRef, msg As Integer, wParam As Integer, ByRef lp As PARAFORMAT) As Integer End Function End Class Public Enum TextAlign ' Specifies how text in a SmartBox is horizontally aligned. Left = 1 ' The text is aligned to the left. Right = 2 ' The text is aligned to the right. Center = 3 ' The text is aligned in the center. Justify = 4 ' The text is justified. End Enum
Make only Form1: Private Sub Form1_Load. DO NOT write any code in it.
Declare the SmartBox thus:
Now run the project (and stop it)Code:Public Class Form1 Public SmartBox As SmartBox = New SmartBox Private Sub Form1_Load. End Sub End Class
A control called SmartBox will now be in the ToolBox for this project, you will be able to use the new control in this or any other form.
Use this control as any other TextBox or RichTextBox with the added advantage of being able to justify large amounts of text.
Poppa




Reply With Quote