locked
how to change the underline style

    问题

  • Hi,

    Can someone tell me that how can i change the underline style in richtextbox in Visual Basic 2008

    thanks in advance.

    2012年2月28日 7:04

答案

  • Hi Humam505,

    Welcome to the MSDN forum.

    To reach your goal, I think we need to use a customer class that contains the styles we need.  I got a third-party class at this link: http://vbcity.com/forums/t/142522.aspx .

    Since I'm not sure if you are able to open this link, I will post the codes here. You need to add a class named Underline.vb:

    Imports System.Runtime.InteropServices
    Public Class Underline
        Public Sub New(ByVal RTB As RichTextBox)
            Me.R = RTB
        End Sub
        Private Const CFM_UNDERLINETYPE As Integer = 8388608
        Private Const EM_SETCHARFORMAT As Integer = 1092
        Private Const EM_GETCHARFORMAT As Integer = 1082
        Private Const SCF_SELECTION = &H1
        Private R As RichTextBox
        <StructLayout(LayoutKind.Sequential)> _
        Private Structure CHARFORMAT
            Public cbSize As Integer
            Public dwMask As UInteger
            Public dwEffects As UInteger
            Public yHeight As Integer
            Public yOffset As Integer
            Public crTextColor As Integer
            Public bCharSet As Byte
            Public bPitchAndFamily As Byte
            <MarshalAs(UnmanagedType.ByValArray, SizeConst:=32)> _
            Public szFaceName As Char()
            ' CHARFORMAT2 from here onwards.
            Public wWeight As Short
            Public sSpacing As Short
            Public crBackColor As Integer
            Public LCID As Integer
            Public dwReserved As UInteger
            Public sStyle As Short
            Public wKerning As Short
            Public bUnderlineType As Byte
            Public bAnimation As Byte
            Public bRevAuthor As Byte
        End Structure
        <DllImport("user32", CharSet:=CharSet.Auto)> _
        Private Shared Function SendMessage(ByVal hWnd As HandleRef, ByVal msg As Integer, ByVal wParam As Integer, ByRef lp As CHARFORMAT) As Integer
        End Function
        Public Property SelectionUnderlineStyle() As UnderlineStyle
            Get
                Dim fmt As CHARFORMAT = New CHARFORMAT()
                fmt.cbSize = Marshal.SizeOf(fmt)
                ' Get the underline style.
                SendMessage(New HandleRef(Me, R.Handle), EM_GETCHARFORMAT, SCF_SELECTION, fmt)
                ' Default to no underline.
                If (fmt.dwMask And CFM_UNDERLINETYPE) = 0 Then
                    Return UnderlineStyle.None
                End If
                Dim style As Byte = CByte(fmt.bUnderlineType And &HF)
                Return CType(style, UnderlineStyle)
            End Get
            Set(ByVal value As UnderlineStyle)
                ' Ensure we don't alter the color by accident.
                Dim color As UnderlineColor = SelectionUnderlineColor
                ' Ensure we don't show it if it shouldn't be shown.
                If value = UnderlineStyle.None Then
                    color = UnderlineColor.Black
                End If
                Dim fmt As CHARFORMAT = New CHARFORMAT()
                fmt.cbSize = Marshal.SizeOf(fmt)
                fmt.dwMask = CFM_UNDERLINETYPE
                fmt.bUnderlineType = CByte(CByte(value) Or CByte(color))
                ' Set the underline type.
                SendMessage(New HandleRef(Me, R.Handle), EM_SETCHARFORMAT, SCF_SELECTION, fmt)
            End Set
        End Property
        Public Enum UnderlineStyle
            ''' <summary>
            ''' No underlining.
            ''' </summary>
            None = 0
            ''' <summary>
            ''' Standard underlining across all words.
            ''' </summary>
            Normal = 1
            ''' <summary>
            ''' Standard underlining broken between words.
            ''' </summary>
            Word = 2
            ''' <summary>
            ''' Double line underlining.
            ''' </summary>
            [Double] = 3
            ''' <summary>
            ''' Dotted underlining.
            ''' </summary>
            Dotted = 4
            ''' <summary>
            ''' Dashed underlining.
            ''' </summary>
            Dash = 5
            ''' <summary>
            ''' Dash-dot ("-.-.") underlining.
            ''' </summary>
            DashDot = 6
            ''' <summary>
            ''' Dash-dot-dot ("-..-..") underlining.
            ''' </summary>
            DashDotDot = 7
            ''' <summary>
            ''' Wave underlining (like spelling mistakes in MS Word).
            ''' </summary>
            Wave = 8
            ''' <summary>
            ''' Extra thick standard underlining.
            ''' </summary>
            Thick = 9
            ''' <summary>
            ''' Extra thin standard underlining.
            ''' </summary>
            HairLine = 10
            ''' <summary>
            ''' Double thickness wave underlining.
            ''' </summary>
            DoubleWave = 11
            ''' <summary>
            ''' Thick wave underlining.
            ''' </summary>
            HeavyWave = 12
            ''' <summary>
            ''' Extra long dash underlining.
            ''' </summary>
            LongDash = 13
        End Enum
        Public Property SelectionUnderlineColor() As UnderlineColor
            Get
                Dim fmt As CHARFORMAT = New CHARFORMAT()
                fmt.cbSize = Marshal.SizeOf(fmt)
                ' Get the underline color.
                SendMessage(New HandleRef(Me, R.Handle), EM_GETCHARFORMAT, SCF_SELECTION, fmt)
                ' Default to black.
                If (fmt.dwMask And CFM_UNDERLINETYPE) = 0 Then
                    Return UnderlineColor.Black
                End If
                Dim style As Byte = CByte(fmt.bUnderlineType And &HF0)
                Return CType(style, UnderlineColor)
            End Get
            Set(ByVal value As UnderlineColor)
                ' Ensure we don't alter the style.
                Dim style As UnderlineStyle = SelectionUnderlineStyle
                ' Ensure we don't show it if it shouldn't be shown.
                If style = UnderlineStyle.None Then
                    value = UnderlineColor.Black
                End If
                Dim fmt As CHARFORMAT = New CHARFORMAT()
                fmt.cbSize = Marshal.SizeOf(fmt)
                fmt.dwMask = CFM_UNDERLINETYPE
                fmt.bUnderlineType = CByte(CByte(style) Or CByte(value))
                ' Set the underline color.
                SendMessage(New HandleRef(Me, R.Handle), EM_SETCHARFORMAT, SCF_SELECTION, fmt)
            End Set
        End Property
        Public Enum UnderlineColor
            ''' <summary>Black.</summary>
            Black = &H0
            ''' <summary>Blue.</summary>
            Blue = &H10
            ''' <summary>Cyan.</summary>
            Cyan = &H20
            ''' <summary>Lime green.</summary>
            LimeGreen = &H30
            ''' <summary>Magenta.</summary>
            Magenta = &H40
            ''' <summary>Red.</summary>
            Red = &H50
            ''' <summary>Yellow.</summary>
            Yellow = &H60
            ''' <summary>White.</summary>
            White = &H70
            ''' <summary>DarkBlue.</summary>
            DarkBlue = &H80
            ''' <summary>DarkCyan.</summary>
            DarkCyan = &H90
            ''' <summary>Green.</summary>
            Green = &HA0
            ''' <summary>Dark magenta.</summary>
            DarkMagenta = &HB0
            ''' <summary>Brown.</summary>
            Brown = &HC0
            ''' <summary>Olive green.</summary>
            OliveGreen = &HD0
            ''' <summary>Dark gray.</summary>
            DarkGray = &HE0
            ''' <summary>Gray.</summary>
            Gray = &HF0
        End Enum
    End Class


          Then you can use them like this:

      Dim U As New Underline(Me.RichTextBox1)

            U.SelectionUnderlineStyle = Underline.UnderlineStyle.Dotted

            U.SelectionUnderlineColor = Underline.UnderlineColor.Red


    I hope this will be helpful.


    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.       

    Best regards,


    Shanks Zen
    MSDN Community Support | Feedback to us

    2012年3月1日 2:08
    版主

全部回复

  • Hi

    Try this one.

    I have used the following code to bold and unline the selected text style.

    this.richTextBox1.SelectionFont = new Font(this.richTextBox1.Font, FontStyle.Bold | FontStyle.Underline);


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful"


    2012年2月28日 7:31
  • nononononono i want to change the style of underline in richtextbox. e.g. Dash-Dot, Dot-Dot-Dash, Wave etc.

    2012年2月28日 11:26
  • Hi Humam505,

    Welcome to the MSDN forum.

    To reach your goal, I think we need to use a customer class that contains the styles we need.  I got a third-party class at this link: http://vbcity.com/forums/t/142522.aspx .

    Since I'm not sure if you are able to open this link, I will post the codes here. You need to add a class named Underline.vb:

    Imports System.Runtime.InteropServices
    Public Class Underline
        Public Sub New(ByVal RTB As RichTextBox)
            Me.R = RTB
        End Sub
        Private Const CFM_UNDERLINETYPE As Integer = 8388608
        Private Const EM_SETCHARFORMAT As Integer = 1092
        Private Const EM_GETCHARFORMAT As Integer = 1082
        Private Const SCF_SELECTION = &H1
        Private R As RichTextBox
        <StructLayout(LayoutKind.Sequential)> _
        Private Structure CHARFORMAT
            Public cbSize As Integer
            Public dwMask As UInteger
            Public dwEffects As UInteger
            Public yHeight As Integer
            Public yOffset As Integer
            Public crTextColor As Integer
            Public bCharSet As Byte
            Public bPitchAndFamily As Byte
            <MarshalAs(UnmanagedType.ByValArray, SizeConst:=32)> _
            Public szFaceName As Char()
            ' CHARFORMAT2 from here onwards.
            Public wWeight As Short
            Public sSpacing As Short
            Public crBackColor As Integer
            Public LCID As Integer
            Public dwReserved As UInteger
            Public sStyle As Short
            Public wKerning As Short
            Public bUnderlineType As Byte
            Public bAnimation As Byte
            Public bRevAuthor As Byte
        End Structure
        <DllImport("user32", CharSet:=CharSet.Auto)> _
        Private Shared Function SendMessage(ByVal hWnd As HandleRef, ByVal msg As Integer, ByVal wParam As Integer, ByRef lp As CHARFORMAT) As Integer
        End Function
        Public Property SelectionUnderlineStyle() As UnderlineStyle
            Get
                Dim fmt As CHARFORMAT = New CHARFORMAT()
                fmt.cbSize = Marshal.SizeOf(fmt)
                ' Get the underline style.
                SendMessage(New HandleRef(Me, R.Handle), EM_GETCHARFORMAT, SCF_SELECTION, fmt)
                ' Default to no underline.
                If (fmt.dwMask And CFM_UNDERLINETYPE) = 0 Then
                    Return UnderlineStyle.None
                End If
                Dim style As Byte = CByte(fmt.bUnderlineType And &HF)
                Return CType(style, UnderlineStyle)
            End Get
            Set(ByVal value As UnderlineStyle)
                ' Ensure we don't alter the color by accident.
                Dim color As UnderlineColor = SelectionUnderlineColor
                ' Ensure we don't show it if it shouldn't be shown.
                If value = UnderlineStyle.None Then
                    color = UnderlineColor.Black
                End If
                Dim fmt As CHARFORMAT = New CHARFORMAT()
                fmt.cbSize = Marshal.SizeOf(fmt)
                fmt.dwMask = CFM_UNDERLINETYPE
                fmt.bUnderlineType = CByte(CByte(value) Or CByte(color))
                ' Set the underline type.
                SendMessage(New HandleRef(Me, R.Handle), EM_SETCHARFORMAT, SCF_SELECTION, fmt)
            End Set
        End Property
        Public Enum UnderlineStyle
            ''' <summary>
            ''' No underlining.
            ''' </summary>
            None = 0
            ''' <summary>
            ''' Standard underlining across all words.
            ''' </summary>
            Normal = 1
            ''' <summary>
            ''' Standard underlining broken between words.
            ''' </summary>
            Word = 2
            ''' <summary>
            ''' Double line underlining.
            ''' </summary>
            [Double] = 3
            ''' <summary>
            ''' Dotted underlining.
            ''' </summary>
            Dotted = 4
            ''' <summary>
            ''' Dashed underlining.
            ''' </summary>
            Dash = 5
            ''' <summary>
            ''' Dash-dot ("-.-.") underlining.
            ''' </summary>
            DashDot = 6
            ''' <summary>
            ''' Dash-dot-dot ("-..-..") underlining.
            ''' </summary>
            DashDotDot = 7
            ''' <summary>
            ''' Wave underlining (like spelling mistakes in MS Word).
            ''' </summary>
            Wave = 8
            ''' <summary>
            ''' Extra thick standard underlining.
            ''' </summary>
            Thick = 9
            ''' <summary>
            ''' Extra thin standard underlining.
            ''' </summary>
            HairLine = 10
            ''' <summary>
            ''' Double thickness wave underlining.
            ''' </summary>
            DoubleWave = 11
            ''' <summary>
            ''' Thick wave underlining.
            ''' </summary>
            HeavyWave = 12
            ''' <summary>
            ''' Extra long dash underlining.
            ''' </summary>
            LongDash = 13
        End Enum
        Public Property SelectionUnderlineColor() As UnderlineColor
            Get
                Dim fmt As CHARFORMAT = New CHARFORMAT()
                fmt.cbSize = Marshal.SizeOf(fmt)
                ' Get the underline color.
                SendMessage(New HandleRef(Me, R.Handle), EM_GETCHARFORMAT, SCF_SELECTION, fmt)
                ' Default to black.
                If (fmt.dwMask And CFM_UNDERLINETYPE) = 0 Then
                    Return UnderlineColor.Black
                End If
                Dim style As Byte = CByte(fmt.bUnderlineType And &HF0)
                Return CType(style, UnderlineColor)
            End Get
            Set(ByVal value As UnderlineColor)
                ' Ensure we don't alter the style.
                Dim style As UnderlineStyle = SelectionUnderlineStyle
                ' Ensure we don't show it if it shouldn't be shown.
                If style = UnderlineStyle.None Then
                    value = UnderlineColor.Black
                End If
                Dim fmt As CHARFORMAT = New CHARFORMAT()
                fmt.cbSize = Marshal.SizeOf(fmt)
                fmt.dwMask = CFM_UNDERLINETYPE
                fmt.bUnderlineType = CByte(CByte(style) Or CByte(value))
                ' Set the underline color.
                SendMessage(New HandleRef(Me, R.Handle), EM_SETCHARFORMAT, SCF_SELECTION, fmt)
            End Set
        End Property
        Public Enum UnderlineColor
            ''' <summary>Black.</summary>
            Black = &H0
            ''' <summary>Blue.</summary>
            Blue = &H10
            ''' <summary>Cyan.</summary>
            Cyan = &H20
            ''' <summary>Lime green.</summary>
            LimeGreen = &H30
            ''' <summary>Magenta.</summary>
            Magenta = &H40
            ''' <summary>Red.</summary>
            Red = &H50
            ''' <summary>Yellow.</summary>
            Yellow = &H60
            ''' <summary>White.</summary>
            White = &H70
            ''' <summary>DarkBlue.</summary>
            DarkBlue = &H80
            ''' <summary>DarkCyan.</summary>
            DarkCyan = &H90
            ''' <summary>Green.</summary>
            Green = &HA0
            ''' <summary>Dark magenta.</summary>
            DarkMagenta = &HB0
            ''' <summary>Brown.</summary>
            Brown = &HC0
            ''' <summary>Olive green.</summary>
            OliveGreen = &HD0
            ''' <summary>Dark gray.</summary>
            DarkGray = &HE0
            ''' <summary>Gray.</summary>
            Gray = &HF0
        End Enum
    End Class


          Then you can use them like this:

      Dim U As New Underline(Me.RichTextBox1)

            U.SelectionUnderlineStyle = Underline.UnderlineStyle.Dotted

            U.SelectionUnderlineColor = Underline.UnderlineColor.Red


    I hope this will be helpful.


    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.       

    Best regards,


    Shanks Zen
    MSDN Community Support | Feedback to us

    2012年3月1日 2:08
    版主