palak_paneer 0 Newbie Poster

Hi, i hav converted a C# code for text justification to vb.net. When i use this code, bydefault cursor position position sets to left side of rtb even if i set righttoleft property true b4 text justification. I need to change this code so that after text justification, cursor remains at rightmost side of rtb. Here's the code

Public Class RichTextBoxEx
    Inherits RichTextBox

    Enum TextAlign
        Left = 1
        Right = 2
        Center = 3
        Justify = 4
    End Enum

    <DllImport("user32", CharSet:=CharSet.Auto)> _
    Private Shared Function SendMessage(ByVal hWnd As HandleRef, _
    ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    End Function

    <DllImport("user32", CharSet:=CharSet.Auto)> _
    Private Shared Function SendMessage(ByVal hWnd As HandleRef, _
    ByVal msg As Integer, ByVal wParam As Integer, ByRef lp As PARAFORMAT) As Integer
    End Function


    <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 Int32
        Public dySpaceAfter As Int32
        Public dyLineSpacing As Int32
        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

    Private updating As Int32 = 0
    Private oldEventMask As Int32 = 0

    'Constants from the Platform SDK.
    Private Const EM_SETEVENTMASK As Integer = 1073
    Private Const EM_GETPARAFORMAT As Integer = 1085
    Private Const EM_SETPARAFORMAT As Integer = 1095
    Private Const EM_SETTYPOGRAPHYOPTIONS As Integer = 1226
    Private Const WM_SETREDRAW As Integer = 11
    Private Const TO_ADVANCEDTYPOGRAPHY As Integer = 1
    Private Const PFM_ALIGNMENT As Integer = 8
    Private Const SCF_SELECTION As Integer = 1

    Protected Overrides Sub OnHandleCreated(ByVal e As EventArgs)

        MyBase.OnHandleCreated(e)

        'Enable support for justification.
        SendMessage(New HandleRef(Me, Handle), _
                     EM_SETTYPOGRAPHYOPTIONS, _
                     TO_ADVANCEDTYPOGRAPHY, _
                     TO_ADVANCEDTYPOGRAPHY)
    End Sub

    Public Overloads Property SelectionAlignment() As TextAlign

        Get

            Dim fmt As PARAFORMAT = 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 & PFM_ALIGNMENT) = 0) Then
                Return TextAlign.Left
            End If
            If fmt.wAlignment = 1 Then
                Return TextAlign.Left
            ElseIf fmt.wAlignment = 2 Then
                Return TextAlign.Right
            ElseIf fmt.wAlignment = 3 Then
                Return TextAlign.Center
            ElseIf fmt.wAlignment Then
                Return TextAlign.Justify
            End If
        End Get
        Set(ByVal value As TextAlign)

            Dim fmt As PARAFORMAT = 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
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.