Click here to Skip to main content
15,917,177 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
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 />            // Get the alignment.<br />            SendMessage( new HandleRef( this, Handle ),<br />                         EM_GETPARAFORMAT,<br />                         SCF_SELECTION, ref fmt );<br />            <br />            // Default to Left align.<br />            if ( ( fmt.dwMask &amp; PFM_ALIGNMENT ) == 0 )<br />                return TextAlign.Left;<br />            <br />            return ( TextAlign )fmt.wAlignment;<br />        }<br />        <br />        set<br />        {<br />            PARAFORMAT fmt = new PARAFORMAT();<br />            fmt.cbSize = Marshal.SizeOf( fmt );<br />            fmt.dwMask = PFM_ALIGNMENT;<br />            fmt.wAlignment = ( short )value;<br />            <br />            // Set the alignment.<br />            SendMessage( new HandleRef( this, Handle ),<br />                         EM_SETPARAFORMAT,<br />                         SCF_SELECTION, ref fmt );<br />        }<br />    }<br />    <br />    /// &lt;summary&gt;<br />    /// This member overrides<br />    /// &lt;see cref="Control"/&gt;.OnHandleCreated.<br />    /// &lt;/summary&gt;<br />    protected override void OnHandleCreated( EventArgs e )<br />    {<br />        base.OnHandleCreated( e );<br />        <br />        // Enable support for justification.<br />        SendMessage( new HandleRef( this, Handle ),<br />                     EM_SETTYPOGRAPHYOPTIONS,<br />                     TO_ADVANCEDTYPOGRAPHY,<br />                     TO_ADVANCEDTYPOGRAPHY );<br />    }<br />    <br />    private int updating = 0;<br />    private int oldEventMask = 0;<br />    <br />    // Constants from the Platform SDK.<br />    private const int EM_SETEVENTMASK = 1073;<br />    private const int EM_GETPARAFORMAT = 1085;<br />    private const int EM_SETPARAFORMAT = 1095;<br />    private const int EM_SETTYPOGRAPHYOPTIONS = 1226;<br />    private const int WM_SETREDRAW = 11;<br />    private const int TO_ADVANCEDTYPOGRAPHY = 1;<br />    private const int PFM_ALIGNMENT = 8;<br />    private const int SCF_SELECTION = 1;<br />    <br />    // It makes no difference if we use PARAFORMAT or<br />    // PARAFORMAT2 here, so I have opted for PARAFORMAT2.<br />    [StructLayout( LayoutKind.Sequential )]<br />    private struct PARAFORMAT<br />    {<br />        public int cbSize;<br />        public uint dwMask;<br />        public short wNumbering;<br />        public short wReserved;<br />        public int dxStartIndent;<br />        public int dxRightIndent;<br />        public int dxOffset;<br />        public short wAlignment;<br />        public short cTabCount;<br />        [MarshalAs( UnmanagedType.ByValArray, SizeConst = 32 )]<br />        public int[] rgxTabs;<br />        <br />        // PARAFORMAT2 from here onwards.<br />        public int dySpaceBefore;<br />        public int dySpaceAfter;<br />        public int dyLineSpacing;<br />        public short sStyle;<br />        public byte bLineSpacingRule;<br />        public byte bOutlineLevel;<br />        public short wShadingWeight;<br />        public short wShadingStyle;<br />        public short wNumberingStart;<br />        public short wNumberingStyle;<br />        public short wNumberingTab;<br />        public short wBorderSpace;<br />        public short wBorderWidth;<br />        public short wBorders;<br />    }<br />    <br />    [DllImport( "user32", CharSet = CharSet.Auto )]<br />    private static extern int SendMessage( HandleRef hWnd,<br />                                           int msg,<br />                                           int wParam,<br />                                           int lParam );<br />    <br />    [DllImport( "user32", CharSet = CharSet.Auto )]<br />    private static extern int SendMessage( HandleRef hWnd,<br />                                           int msg,<br />                                           int wParam,<br />                                           ref PARAFORMAT lp );<br />}<br /><br />/// &lt;summary&gt;<br />/// Specifies how text in a &lt;see cref="AdvRichTextBox"/&gt; is<br />/// horizontally aligned.<br />/// &lt;/summary&gt;<br />public enum TextAlign<br />{<br />    /// &lt;summary&gt;<br />    /// The text is aligned to the left.<br />    /// &lt;/summary&gt;<br />    Left = 1,<br />    <br />    /// &lt;summary&gt;<br />    /// The text is aligned to the right.<br />    /// &lt;/summary&gt;<br />    Right = 2,<br />    <br />    /// &lt;summary&gt;<br />    /// The text is aligned in the center.<br />    /// &lt;/summary&gt;<br />    Center = 3,<br />    <br />    /// &lt;summary&gt;<br />    /// The text is justified.<br />    /// &lt;/summary&gt;<br />    Justify = 4<br />}<br /><br />}<br /><br />


VB Version
<br />Imports System<br />Imports System.Windows.Forms<br />Imports System.Runtime.InteropServices<br /><br /><br />'&lt;Designer("System.Windows.Forms.Design.ParentControlDesigner, SystemDesign", GetType(IDesigner))&gt; _<br /><br />&lt;Serializable()&gt; _<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 />    &lt;DllImport("user32", CharSet:=CharSet.Auto)&gt; _<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 />    &lt;DllImport("user32", CharSet:=CharSet.Auto)&gt; _<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 />    &lt;StructLayout(LayoutKind.Sequential)&gt; _<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 />        &lt;MarshalAs(UnmanagedType.ByValArray, SizeConst:=32)&gt; _<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 &amp; 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
Posted

1 solution

In your C# code you pass fmt by reference. This allows the OS to alter it. In VB.Net, you pass it by value; this doesn't. The OS will probably try to alter it, then when it finds it can't, will throw an AccessViolationException. To fix it, pass the fmt variable by reference, using ByRef

 
Share this answer
 


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900