I’ve converted code for text justification from C# to VB Express 2005
C# version:
<br />using System;<br />using System.Windows.Forms;<br />using System.Runtime.InteropServices;<br />public class AdvRichTextBox : RichTextBox<br />{ public new TextAlign SelectionAlignment<br /> {<br /> get<br /> {<br /> PARAFORMAT fmt = new PARAFORMAT();<br /> fmt.cbSize = Marshal.SizeOf( fmt );<br /> <br />
VB Version
<br />Imports System<br />Imports System.Windows.Forms<br />Imports System.Runtime.InteropServices<br /><br /><br />'<Designer("System.Windows.Forms.Design.ParentControlDesigner, SystemDesign", GetType(IDesigner))> _<br /><br /><Serializable()> _<br />Public Class RichTextBoxEx<br /> Inherits RichTextBox<br /><br /> Enum TextAlign<br /> Left = 1<br /> Right = 2<br /> Center = 3<br /> Justify = 4<br /> End Enum<br /><br /> <DllImport("user32", CharSet:=CharSet.Auto)> _<br /> Private Shared Function SendMessage(ByVal hWnd As HandleRef, _<br /> ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer<br /> End Function<br /><br /> <DllImport("user32", CharSet:=CharSet.Auto)> _<br /> Private Shared Function SendMessage(ByVal hWnd As HandleRef, _<br /> ByVal msg As Integer, ByVal wParam As Integer, ByVal lp As PARAFORMAT) As Integer<br /> End Function<br /><br /><br /> <StructLayout(LayoutKind.Sequential)> _<br /> Private Structure PARAFORMAT<br /> Public cbSize As Integer<br /> Public dwMask As UInteger<br /> Public wNumbering As Short<br /> Public wReserved As Short<br /> Public dxStartIndent As Integer<br /> Public dxRightIndent As Integer<br /> Public dxOffset As Integer<br /> Public wAlignment As Short<br /> Public cTabCount As Short<br /> <MarshalAs(UnmanagedType.ByValArray, SizeConst:=32)> _<br /> Public rgxTabs() As Integer<br /><br /> ' PARAFORMAT2 from here onwards.<br /> Public dySpaceBefore As Int32<br /> Public dySpaceAfter As Int32<br /> Public dyLineSpacing As Int32<br /> Public sStyle As Short<br /> Public bLineSpacingRule As Byte<br /> Public bOutlineLevel As Byte<br /> Public wShadingWeight As Short<br /> Public wShadingStyle As Short<br /> Public wNumberingStart As Short<br /> Public wNumberingStyle As Short<br /> Public wNumberingTab As Short<br /> Public wBorderSpace As Short<br /> Public wBorderWidth As Short<br /> Public wBorders As Short<br /> End Structure<br /><br /> Private updating As Int32 = 0<br /> Private oldEventMask As Int32 = 0<br /><br /> 'Constants from the Platform SDK.<br /> Private Const EM_SETEVENTMASK As Integer = 1073<br /> Private Const EM_GETPARAFORMAT As Integer = 1085<br /> Private Const EM_SETPARAFORMAT As Integer = 1095<br /> Private Const EM_SETTYPOGRAPHYOPTIONS As Integer = 1226<br /> Private Const WM_SETREDRAW As Integer = 11<br /> Private Const TO_ADVANCEDTYPOGRAPHY As Integer = 1<br /> Private Const PFM_ALIGNMENT As Integer = 8<br /> Private Const SCF_SELECTION As Integer = 1<br /><br /> Protected Overrides Sub OnHandleCreated(ByVal e As EventArgs)<br /><br /> MyBase.OnHandleCreated(e)<br /><br /> 'Enable support for justification.<br /> SendMessage(New HandleRef(Me, Handle), _<br /> EM_SETTYPOGRAPHYOPTIONS, _<br /> TO_ADVANCEDTYPOGRAPHY, _<br /> TO_ADVANCEDTYPOGRAPHY)<br /> End Sub<br /><br /> Public Overloads Property SelectionAlignment() As TextAlign<br /><br /> Get<br /><br /> Dim fmt As PARAFORMAT = New PARAFORMAT()<br /> fmt.cbSize = Marshal.SizeOf(fmt)<br /><br /> ' Get the alignment.<br /> SendMessage(New HandleRef(Me, Handle), _<br /> EM_GETPARAFORMAT, _<br /> SCF_SELECTION, fmt)<br /><br /> ' Default to Left align.<br /> If ((fmt.dwMask & PFM_ALIGNMENT) = 0) Then<br /> Return TextAlign.Left<br /> End If<br /> If fmt.wAlignment = 1 Then<br /> Return TextAlign.Left<br /> ElseIf fmt.wAlignment = 2 Then<br /> Return TextAlign.Right<br /> ElseIf fmt.wAlignment = 3 Then<br /> Return TextAlign.Center<br /> ElseIf fmt.wAlignment Then<br /> Return TextAlign.Justify<br /> End If<br /> End Get<br /> Set(ByVal value As TextAlign)<br /><br /> Dim fmt As PARAFORMAT = New PARAFORMAT()<br /> fmt.cbSize = Marshal.SizeOf(fmt)<br /> fmt.dwMask = PFM_ALIGNMENT<br /> fmt.wAlignment = CShort(value)<br /><br /> ' Set the alignment.<br /> SendMessage(New HandleRef(Me, Handle), _<br /> EM_SETPARAFORMAT, _<br /> SCF_SELECTION, fmt)<br /> End Set<br /> End Property<br />End Class<br /><br />
It was working in C# but in vb it gives error that “AccessViolationException was unhandled” at the following line
SendMessage(New HandleRef(Me, Handle), _
EM_SETPARAFORMAT, _
SCF_SELECTION, fmt)
Plz can anyone tell me what's the problem