Losing Link state with CHARFORMAT2 definition ;Closed
-
The below code sets the selected text in the RichTextBox to a protected link. This works but I have an issue when entering text before the link; particularly a space char. When I enter a space character before the link, the link loses it's link state; 1. The color changes to normal text color. 2. The underline disappears. 3. I no longer receive the link click message. 4. It remains protected. I can type other characters and do not lose the link state except when the caret is on the space immediately preceding the link. Test case with this text where link is the formatted link: " abc link" Hitting the spacebar anywhere between the 'c' and the 'l' causes the link to lose its state Hitting any other char does not affect the state except when the char is next to the 'l' and then it loses its state. Anyone have an idea of why the link is losing it's attribute? Thank you Public Function SetSelectionCharFormat(ByVal hEdit As IntPtr, ByVal cf As CHARFORMAT2, _ Optional ByVal fmt As EMFormatMasks = EMFormatMasks.SCF_SELECTION Or EMFormatMasks.SCF_WORD) As Boolean Dim bReturn As Boolean = False ' This is test code which overrides the input cf.dwMask = (CFMMask.CFM_LINK Or CFMMask.CFM_PROTECTED) ' &h20 or &h10 cf.dwEffects = (CFEEffects.CFE_LINK Or CFEEffects.CFE_PROTECTED) ' &h20 or &h10 Try Dim structPtr As IntPtr ' Allocate the memory for structure data structPtr = Marshal.AllocHGlobal(Marshal.SizeOf(cf)) ' Copy structure data to memory ptr Marshal.StructureToPtr(cf, structPtr, False) ' Return non-zero if passes; code from winctrl4.cpp ' fmt = the default (EMFormatMasks.SCF_SELECTION Or EMFormatMasks.SCF_WORD) bReturn = (MyUtilsLib.WindowsAPI.SendMessage(hEdit, EMMessages.EM_SETCHARFORMAT, fmt, structPtr) <> 0) ' Free memory Marshal.FreeHGlobal(structPtr) #If DEBUG Then If bReturn = False Then Debug.WriteLine("SetSelectionCharFormat() failed") End If #End If Catch ex As Exception MsgBox(ex.Message()) End Try Return bReturn End Function