Extending RichTextBox - Help Required

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Horn

    Extending RichTextBox - Help Required

    Hi,

    I am trying to add extra functionality to the standard RichText control, but
    I've fallen at the first hurdle,can someone take a look at the following
    code and tell me why it fails to return true if the current selection is
    bold.

    Thanks,

    Martin Horn.

    Imports System.Runtime. InteropServices

    Public Class RichTextEx
    Inherits RichTextBox

    <StructLayout(L ayoutKind.Seque ntial)> _
    Public Structure STRUCT_CHARFORM AT
    Public cbSize As Integer
    Public dwMask As UInt32
    Public dwEffects As UInt32
    Public yHeight As Int32
    Public yOffset As Int32
    Public crTextColor As Int32
    Public bCharSet As Byte
    Public bPitchAndFamily As Byte
    <MarshalAs(Unma nagedType.ByVal Array, SizeConst:=32)> _
    Public szFaceName As Char()
    End Structure

    <DllImport("use r32.dll")> _
    Private Shared Function SendMessage(ByV al hWnd As IntPtr, _
    ByVal msg As Int32, _
    ByVal wParam As Int32, _
    ByVal lParam As IntPtr) As Int32
    End Function

    Public Const EM_GETCHARFORMA T As Int32 = &H43A&
    Public Const SCF_SELECTION As Int32 = &H1&
    Public Const CFM_BOLD As Int32 = &H1&
    Public Const CFE_BOLD As Int32 = &H1&

    ' Just to test the SelectionBold Property
    Public Sub New()
    MyBase.New()
    Me.Font = New Font("Times New Roman", 10, FontStyle.Bold)
    End Sub

    Public Property SelectionBold() As Boolean
    Get
    Dim fmt As New STRUCT_CHARFORM AT
    fmt.cbSize = Marshal.SizeOf( fmt)
    Dim lParam As IntPtr
    lParam = Marshal.AllocCo TaskMem(Marshal .SizeOf(fmt))
    Marshal.Structu reToPtr(fmt, lParam, False)
    SendMessage(Me. Handle, EM_GETCHARFORMA T, SCF_SELECTION, lParam)
    If ((fmt.dwMask And CFM_BOLD) = 0) Then
    Return False
    End If
    If ((fmt.dwEffects And CFE_BOLD) = 0) Then
    Return False
    Else
    Return True
    End If
    End Get
    Set(ByVal value As Boolean)
    '// Not Implemented //
    End Set
    End Property

    End Class


  • Ken Tucker [MVP]

    #2
    Re: Extending RichTextBox - Help Required

    Hi,

    I changed your sendmessage api to pass lparam as
    STRUCT_CHARFORM AT byref.

    Public Class RichTextEx

    Inherits RichTextBox

    <StructLayout(L ayoutKind.Seque ntial)> _

    Public Structure STRUCT_CHARFORM AT

    Public cbSize As Integer

    Public dwMask As Int32

    Public dwEffects As Int32

    Public yHeight As Int32

    Public yOffset As Int32

    Public crTextColor As Int32

    Public bCharSet As Byte

    Public bPitchAndFamily As Byte

    <MarshalAs(Unma nagedType.ByVal Array, SizeConst:=32)> _

    Public szFaceName As Char()

    End Structure

    <DllImport("use r32.dll")> _

    Private Shared Function SendMessage(ByV al hWnd As IntPtr, _

    ByVal msg As Int32, _

    ByVal wParam As Int32, _

    ByRef lParam As STRUCT_CHARFORM AT) As Int32

    End Function

    Public Const EM_GETCHARFORMA T As Int32 = &H43A&

    Public Const SCF_SELECTION As Int32 = &H1&

    Public Const CFM_BOLD As Int32 = &H1&

    Public Const CFE_BOLD As Int32 = &H1&

    ' Just to test the SelectionBold Property

    Public Sub New()

    MyBase.New()

    Me.Font = New Font("Times New Roman", 10, FontStyle.Bold)

    End Sub

    Public Property SelectionBold() As Boolean

    Get

    Dim fmt As New STRUCT_CHARFORM AT

    fmt.cbSize = Marshal.SizeOf( fmt)

    SendMessage(Me. Handle, EM_GETCHARFORMA T, SCF_SELECTION, fmt)

    If ((fmt.dwMask And CFM_BOLD) = 0) Then

    Return False

    End If

    If ((fmt.dwEffects And CFE_BOLD) = 0) Then

    Return False

    Else

    Return True

    End If

    End Get

    Set(ByVal value As Boolean)

    '// Not Implemented //

    End Set

    End Property

    End Class





    Ken

    ---------------------

    "Martin Horn" <martin@nospam. mhorn.co.uk> wrote in message
    news:OamPd.760$ GW4.5@newsfe2-gui.ntli.net...
    Hi,

    I am trying to add extra functionality to the standard RichText control, but
    I've fallen at the first hurdle,can someone take a look at the following
    code and tell me why it fails to return true if the current selection is
    bold.

    Thanks,

    Martin Horn.

    Imports System.Runtime. InteropServices

    Public Class RichTextEx
    Inherits RichTextBox

    <StructLayout(L ayoutKind.Seque ntial)> _
    Public Structure STRUCT_CHARFORM AT
    Public cbSize As Integer
    Public dwMask As UInt32
    Public dwEffects As UInt32
    Public yHeight As Int32
    Public yOffset As Int32
    Public crTextColor As Int32
    Public bCharSet As Byte
    Public bPitchAndFamily As Byte
    <MarshalAs(Unma nagedType.ByVal Array, SizeConst:=32)> _
    Public szFaceName As Char()
    End Structure

    <DllImport("use r32.dll")> _
    Private Shared Function SendMessage(ByV al hWnd As IntPtr, _
    ByVal msg As Int32, _
    ByVal wParam As Int32, _
    ByVal lParam As IntPtr) As Int32
    End Function

    Public Const EM_GETCHARFORMA T As Int32 = &H43A&
    Public Const SCF_SELECTION As Int32 = &H1&
    Public Const CFM_BOLD As Int32 = &H1&
    Public Const CFE_BOLD As Int32 = &H1&

    ' Just to test the SelectionBold Property
    Public Sub New()
    MyBase.New()
    Me.Font = New Font("Times New Roman", 10, FontStyle.Bold)
    End Sub

    Public Property SelectionBold() As Boolean
    Get
    Dim fmt As New STRUCT_CHARFORM AT
    fmt.cbSize = Marshal.SizeOf( fmt)
    Dim lParam As IntPtr
    lParam = Marshal.AllocCo TaskMem(Marshal .SizeOf(fmt))
    Marshal.Structu reToPtr(fmt, lParam, False)
    SendMessage(Me. Handle, EM_GETCHARFORMA T, SCF_SELECTION, lParam)
    If ((fmt.dwMask And CFM_BOLD) = 0) Then
    Return False
    End If
    If ((fmt.dwEffects And CFE_BOLD) = 0) Then
    Return False
    Else
    Return True
    End If
    End Get
    Set(ByVal value As Boolean)
    '// Not Implemented //
    End Set
    End Property

    End Class



    Comment

    • Martin Horn

      #3
      Re: Extending RichTextBox - Help Required

      Thanks very much, that has made it work.

      "Ken Tucker [MVP]" <vb2ae@bellsout h.net> wrote in message
      news:OJpMlqQEFH A.1292@TK2MSFTN GP10.phx.gbl...[color=blue]
      > Hi,
      >
      > I changed your sendmessage api to pass lparam as
      > STRUCT_CHARFORM AT byref.
      >[/color]


      Comment

      • Martin Horn

        #4
        Re: Extending RichTextBox - Help Required

        Hi again,

        I've encountered another problem with coding this property, this is how I
        have it at the moment:

        Public Enum BoldStateEnum As Integer
        Mixed = 0
        Regular = 1
        Bold = 2
        End Enum

        Public Property SelectionBold() As BoldStateEnum
        Get
        Dim fmt As STRUCT_CHARFORM AT
        fmt = GetCharFormat()
        If ((fmt.dwMask And CFM_BOLD) = 0) Then
        Return BoldStateEnum.M ixed
        End If
        If ((fmt.dwEffects And CFE_BOLD) = 0) Then
        Return BoldStateEnum.R egular
        Else
        Return BoldStateEnum.B old
        End If
        End Get
        Set(ByVal value As BoldStateEnum)
        Dim fmt As New STRUCT_CHARFORM AT
        fmt = GetCharFormat()
        If value = BoldStateEnum.B old Then
        fmt.dwMask = fmt.dwMask Or CFM_BOLD
        fmt.dwEffects = (fmt.dwEffects Or CFE_BOLD)
        Else
        ' Debug.Assert("S electionBold = False - Not Implemeted")
        End If
        SetCharFormat(f mt)
        End Set
        End Property

        Everything seems to work as it should, however when I try to view the form
        designer for the form that contains my extended richtext control I get the
        following error:
        ---------------------
        One or more errors encountered while loading the designer.The errors are
        listed below. Some errors can be fixed by rebuilding your project, while
        others may require code changes. Clicking on each error will take you to the
        line of code that caused it.

        The name 'PC_Till_V2.Ric hTextBoxEx+Bold StateEnum' is not a valid type name.
        ---------------------

        If I remove the line Me.rtf1.Selecti onBold =
        RichTextBoxEx.B oldStateEnum.Re gular from the form's designer code it allows
        the design view to show again.

        So whilst I have a workaround for this I would still like to know what I am
        doing wrong, as I can't initialise the property properly.

        Any ideas?

        Thanks,

        Martin Horn.


        Comment

        • Ken Tucker [MVP]

          #5
          Re: Extending RichTextBox - Help Required

          Hi,

          I would make sure I build the control before I open the form
          designer. If that doesnt work remove the control from the form and add the
          control to the toolbox again. Then add it to the form. Maybe it is still
          using the older version of the control.

          Ken
          -----------------
          "Martin Horn" <martin@nospam. mhorn.co.uk> wrote in message
          news:UPrPd.664$ 1f5.437@newsfe2-win.ntli.net...
          Hi again,

          I've encountered another problem with coding this property, this is how I
          have it at the moment:

          Public Enum BoldStateEnum As Integer
          Mixed = 0
          Regular = 1
          Bold = 2
          End Enum

          Public Property SelectionBold() As BoldStateEnum
          Get
          Dim fmt As STRUCT_CHARFORM AT
          fmt = GetCharFormat()
          If ((fmt.dwMask And CFM_BOLD) = 0) Then
          Return BoldStateEnum.M ixed
          End If
          If ((fmt.dwEffects And CFE_BOLD) = 0) Then
          Return BoldStateEnum.R egular
          Else
          Return BoldStateEnum.B old
          End If
          End Get
          Set(ByVal value As BoldStateEnum)
          Dim fmt As New STRUCT_CHARFORM AT
          fmt = GetCharFormat()
          If value = BoldStateEnum.B old Then
          fmt.dwMask = fmt.dwMask Or CFM_BOLD
          fmt.dwEffects = (fmt.dwEffects Or CFE_BOLD)
          Else
          ' Debug.Assert("S electionBold = False - Not Implemeted")
          End If
          SetCharFormat(f mt)
          End Set
          End Property

          Everything seems to work as it should, however when I try to view the form
          designer for the form that contains my extended richtext control I get the
          following error:
          ---------------------
          One or more errors encountered while loading the designer.The errors are
          listed below. Some errors can be fixed by rebuilding your project, while
          others may require code changes. Clicking on each error will take you to the
          line of code that caused it.

          The name 'PC_Till_V2.Ric hTextBoxEx+Bold StateEnum' is not a valid type name.
          ---------------------

          If I remove the line Me.rtf1.Selecti onBold =
          RichTextBoxEx.B oldStateEnum.Re gular from the form's designer code it allows
          the design view to show again.

          So whilst I have a workaround for this I would still like to know what I am
          doing wrong, as I can't initialise the property properly.

          Any ideas?

          Thanks,

          Martin Horn.



          Comment

          • Martin Horn

            #6
            Re: Extending RichTextBox - Help Required

            Hi Ken,

            that still didn't fix it. The problem seems to occur if I create an
            inherited control class that has a property that reurns an enum value. See
            below for an example that demonstrates the problem.

            I created a new project with a single form. Then I added the follwing class:

            Public Class TestClass
            Inherits Button
            Private fValue As TestClassEnum

            Public Enum TestClassEnum
            Value1 = 0
            Value2 = 1
            End Enum

            Public Property ReturnEnum() As TestClassEnum
            Get
            Return fValue.Value1
            End Get
            Set(ByVal value As TestClassEnum)
            fValue = value
            End Set
            End Property
            End Class

            If I close the form designer, save the project then try to re-open the form
            designer I get the following error, but I can't see what I'm doing wrong.

            -------------------------------------------
            One or more errors encountered while loading the designer.The errors are
            listed below. Some errors can be fixed by rebuilding your project, while
            others may require code changes. Clicking on each error will take you to the
            line of code that caused it.

            The variable 'Value1' is either undeclared or was never assigned.
            Hide
            Edit

            at
            System.Componen tModel.Design.S erialization.Co deDomSerializer Base.Error(IDes ignerSerializat ionManager
            manager, String exceptionText, String helpLink)
            at
            System.Componen tModel.Design.S erialization.Co deDomSerializer Base.Deserializ eExpression(IDe signerSerializa tionManager
            manager, String name, CodeExpression expression)
            at
            System.Componen tModel.Design.S erialization.Co deDomSerializer Base.Deserializ eAssignStatemen t(IDesignerSeri alizationManage r
            manager, CodeAssignState ment statement)
            at
            System.Componen tModel.Design.S erialization.Co deDomSerializer Base.Deserializ eStatement(IDes ignerSerializat ionManager
            manager, CodeStatement statement)
            ---------------------------------------------------

            Thanks,

            Martin.


            Comment

            • Ken Tucker [MVP]

              #7
              Re: Extending RichTextBox - Help Required

              Hi,

              Sorry I cant reproduce the error. Make sure you have the latest
              service pack for the dotnet framework installed. If you are still having
              problems reinstall the dot net framework and as a last resort vb.net




              Ken
              ------
              "Martin Horn" <martin@nospam. mhorn.co.uk> wrote in message
              news:MYIPd.607$ IL4.13@newsfe3-win.ntli.net...
              Hi Ken,

              that still didn't fix it. The problem seems to occur if I create an
              inherited control class that has a property that reurns an enum value. See
              below for an example that demonstrates the problem.

              I created a new project with a single form. Then I added the follwing class:

              Public Class TestClass
              Inherits Button
              Private fValue As TestClassEnum

              Public Enum TestClassEnum
              Value1 = 0
              Value2 = 1
              End Enum

              Public Property ReturnEnum() As TestClassEnum
              Get
              Return fValue.Value1
              End Get
              Set(ByVal value As TestClassEnum)
              fValue = value
              End Set
              End Property
              End Class

              If I close the form designer, save the project then try to re-open the form
              designer I get the following error, but I can't see what I'm doing wrong.

              -------------------------------------------
              One or more errors encountered while loading the designer.The errors are
              listed below. Some errors can be fixed by rebuilding your project, while
              others may require code changes. Clicking on each error will take you to the
              line of code that caused it.

              The variable 'Value1' is either undeclared or was never assigned.
              Hide
              Edit

              at
              System.Componen tModel.Design.S erialization.Co deDomSerializer Base.Error(IDes ignerSerializat ionManager
              manager, String exceptionText, String helpLink)
              at
              System.Componen tModel.Design.S erialization.Co deDomSerializer Base.Deserializ eExpression(IDe signerSerializa tionManager
              manager, String name, CodeExpression expression)
              at
              System.Componen tModel.Design.S erialization.Co deDomSerializer Base.Deserializ eAssignStatemen t(IDesignerSeri alizationManage r
              manager, CodeAssignState ment statement)
              at
              System.Componen tModel.Design.S erialization.Co deDomSerializer Base.Deserializ eStatement(IDes ignerSerializat ionManager
              manager, CodeStatement statement)
              ---------------------------------------------------

              Thanks,

              Martin.



              Comment

              • Martin Horn

                #8
                Re: Extending RichTextBox - Help Required

                Okay, so it looks like it's a problem peculiar to my setup, I'll do as you
                suggest.

                Thanks,

                Martin.

                "Ken Tucker [MVP]" <vb2ae@bellsout h.net> wrote in message
                news:OWwO$agEFH A.208@TK2MSFTNG P12.phx.gbl...[color=blue]
                > Hi,
                >
                > Sorry I cant reproduce the error. Make sure you have the latest
                > service pack for the dotnet framework installed. If you are still having
                > problems reinstall the dot net framework and as a last resort vb.net
                >
                > http://msdn.microsoft.com/netframewo...s/default.aspx
                >
                >
                > Ken
                > ------
                > "Martin Horn" <martin@nospam. mhorn.co.uk> wrote in message
                > news:MYIPd.607$ IL4.13@newsfe3-win.ntli.net...
                > Hi Ken,
                >
                > that still didn't fix it. The problem seems to occur if I create an
                > inherited control class that has a property that reurns an enum value. See
                > below for an example that demonstrates the problem.
                >
                > I created a new project with a single form. Then I added the follwing
                > class:
                >
                > Public Class TestClass
                > Inherits Button
                > Private fValue As TestClassEnum
                >
                > Public Enum TestClassEnum
                > Value1 = 0
                > Value2 = 1
                > End Enum
                >
                > Public Property ReturnEnum() As TestClassEnum
                > Get
                > Return fValue.Value1
                > End Get
                > Set(ByVal value As TestClassEnum)
                > fValue = value
                > End Set
                > End Property
                > End Class
                >
                > If I close the form designer, save the project then try to re-open the
                > form
                > designer I get the following error, but I can't see what I'm doing wrong.
                >
                > -------------------------------------------
                > One or more errors encountered while loading the designer.The errors are
                > listed below. Some errors can be fixed by rebuilding your project, while
                > others may require code changes. Clicking on each error will take you to
                > the
                > line of code that caused it.
                >
                > The variable 'Value1' is either undeclared or was never assigned.
                > Hide
                > Edit
                >
                > at
                > System.Componen tModel.Design.S erialization.Co deDomSerializer Base.Error(IDes ignerSerializat ionManager
                > manager, String exceptionText, String helpLink)
                > at
                > System.Componen tModel.Design.S erialization.Co deDomSerializer Base.Deserializ eExpression(IDe signerSerializa tionManager
                > manager, String name, CodeExpression expression)
                > at
                > System.Componen tModel.Design.S erialization.Co deDomSerializer Base.Deserializ eAssignStatemen t(IDesignerSeri alizationManage r
                > manager, CodeAssignState ment statement)
                > at
                > System.Componen tModel.Design.S erialization.Co deDomSerializer Base.Deserializ eStatement(IDes ignerSerializat ionManager
                > manager, CodeStatement statement)
                > ---------------------------------------------------
                >
                > Thanks,
                >
                > Martin.
                >
                >
                >[/color]


                Comment

                • Martin Horn

                  #9
                  Re: Extending RichTextBox - Help Required

                  Just in case this may help anyone else, I have discovered that moving the
                  enum outside of the class fixes the problem, as follows:

                  Public Enum TestClassEnum
                  Value1 = 0
                  Value2 = 1
                  End Enum

                  Public Class TestClass
                  Inherits Button
                  Private fValue As TestClassEnum = TestClassEnum.V alue1
                  Public Property ReturnEnum() As TestClassEnum
                  Get
                  Return fValue.Value1
                  End Get
                  Set(ByVal value As TestClassEnum)
                  fValue = value
                  End Set
                  End Property
                  End Class

                  I hope posting a late update to this topic isn't a problem.

                  Regards,

                  Martin Horn.

                  "Martin Horn" <martin@nospam. mhorn.co.uk> wrote in message
                  news:GSOPd.799$ IL4.254@newsfe3-win.ntli.net...[color=blue]
                  > Okay, so it looks like it's a problem peculiar to my setup, I'll do as you
                  > suggest.
                  >
                  > Thanks,
                  >
                  > Martin.
                  >
                  > "Ken Tucker [MVP]" <vb2ae@bellsout h.net> wrote in message
                  > news:OWwO$agEFH A.208@TK2MSFTNG P12.phx.gbl...[color=green]
                  >> Hi,
                  >>
                  >> Sorry I cant reproduce the error. Make sure you have the latest
                  >> service pack for the dotnet framework installed. If you are still having
                  >> problems reinstall the dot net framework and as a last resort vb.net
                  >>
                  >> http://msdn.microsoft.com/netframewo...s/default.aspx
                  >>
                  >>
                  >> Ken
                  >> ------
                  >> "Martin Horn" <martin@nospam. mhorn.co.uk> wrote in message
                  >> news:MYIPd.607$ IL4.13@newsfe3-win.ntli.net...
                  >> Hi Ken,
                  >>
                  >> that still didn't fix it. The problem seems to occur if I create an
                  >> inherited control class that has a property that reurns an enum value.
                  >> See
                  >> below for an example that demonstrates the problem.
                  >>
                  >> I created a new project with a single form. Then I added the follwing
                  >> class:
                  >>
                  >> Public Class TestClass
                  >> Inherits Button
                  >> Private fValue As TestClassEnum
                  >>
                  >> Public Enum TestClassEnum
                  >> Value1 = 0
                  >> Value2 = 1
                  >> End Enum
                  >>
                  >> Public Property ReturnEnum() As TestClassEnum
                  >> Get
                  >> Return fValue.Value1
                  >> End Get
                  >> Set(ByVal value As TestClassEnum)
                  >> fValue = value
                  >> End Set
                  >> End Property
                  >> End Class
                  >>
                  >> If I close the form designer, save the project then try to re-open the
                  >> form
                  >> designer I get the following error, but I can't see what I'm doing wrong.
                  >>
                  >> -------------------------------------------
                  >> One or more errors encountered while loading the designer.The errors are
                  >> listed below. Some errors can be fixed by rebuilding your project, while
                  >> others may require code changes. Clicking on each error will take you to
                  >> the
                  >> line of code that caused it.
                  >>
                  >> The variable 'Value1' is either undeclared or was never assigned.
                  >> Hide
                  >> Edit
                  >>
                  >> at
                  >> System.Componen tModel.Design.S erialization.Co deDomSerializer Base.Error(IDes ignerSerializat ionManager
                  >> manager, String exceptionText, String helpLink)
                  >> at
                  >> System.Componen tModel.Design.S erialization.Co deDomSerializer Base.Deserializ eExpression(IDe signerSerializa tionManager
                  >> manager, String name, CodeExpression expression)
                  >> at
                  >> System.Componen tModel.Design.S erialization.Co deDomSerializer Base.Deserializ eAssignStatemen t(IDesignerSeri alizationManage r
                  >> manager, CodeAssignState ment statement)
                  >> at
                  >> System.Componen tModel.Design.S erialization.Co deDomSerializer Base.Deserializ eStatement(IDes ignerSerializat ionManager
                  >> manager, CodeStatement statement)
                  >> ---------------------------------------------------
                  >>
                  >> Thanks,
                  >>
                  >> Martin.
                  >>
                  >>
                  >>[/color]
                  >
                  >[/color]


                  Comment

                  Working...