我在 VS2005 用 RichTextBox 送 vbNewLine 進去會變成 vbLf
在 VB6 測則不會這樣。
在 VB6 測則不會這樣。
在視窗上拖一個 RichTextBox1 ,然後隨便找個事件加入下面程式碼:
Dim iChar As Integer
Dim strText As String
With RichTextBox1
.Text = vbNewLine
strText = .Text
Debug.WriteLine(strText.Length)
For iChar = 1 To strText.Length
Debug.WriteLine(AscW(Mid(strText, iChar, 1)))
Next
End With
.Text = vbNewLine
strText = .Text
Debug.WriteLine(strText.Length)
For iChar = 1 To strText.Length
Debug.WriteLine(AscW(Mid(strText, iChar, 1)))
Next
End With
在 VB2005 會變成
1
10
VB6 跑出來會變成
2
13
10
2
13
10
而微軟的作業系統一直用 CR + LF ,Linux 換行字元則用 LF ,所以我比較偏向這是 bug …
依據國外的 MVP 答覆,這個問題已經回報過,目前仍未解決:
"Bill McCarthy" 撰寫於news:e8gHy69mGHA.2392@TK2MSFTNGSA02.privatenews.microsoft.com…
this is a known bug that will not be fixed due to compatibility issues :(
(I asked for this sometime ago on ladybug)
this is a known bug that will not be fixed due to compatibility issues :(
(I asked for this sometime ago on ladybug)
As a work around I override the Text property:
Public Overrides Property Text() As String
Get
If Me.IsHandleCreated Then
Return Me.GetText
Else
Return MyBase.Text
End If
End Get
Set(ByVal value As String)
MyBase.Text = value
End Set
End Property
Get
If Me.IsHandleCreated Then
Return Me.GetText
Else
Return MyBase.Text
End If
End Get
Set(ByVal value As String)
MyBase.Text = value
End Set
End Property
Private Function GetText() As String
Return NativeMethods.GetText(Me)
End Function
Return NativeMethods.GetText(Me)
End Function
Private Class NativeMethods
Private Sub New()
‘ class is Shared
End Sub
‘ class is Shared
End Sub
Friend Shared Function GetText(ByVal editor As CodeEditor) As String
Dim href As New HandleRef(editor, editor.Handle)
Dim length As Int32 = SendMessageW(href, WM_GETTEXTLENGTH,
IntPtr.Zero, 0).ToInt32 + 2
Dim sb As New System.Text.StringBuilder(length + 2)
SendMessageW(href, WM_GETTEXT, New IntPtr(length), sb)
Return sb.ToString
End Function
Dim href As New HandleRef(editor, editor.Handle)
Dim length As Int32 = SendMessageW(href, WM_GETTEXTLENGTH,
IntPtr.Zero, 0).ToInt32 + 2
Dim sb As New System.Text.StringBuilder(length + 2)
SendMessageW(href, WM_GETTEXT, New IntPtr(length), sb)
Return sb.ToString
End Function
Private Declare Unicode Function SendMessageW Lib "user32" (ByVal hWnd
As HandleRef, ByVal msg As Int32, ByVal wParam As IntPtr, ByVal lparam As
StringBuilder) As IntPtr
Private Declare Unicode Function SendMessageW Lib "user32" (ByVal hWnd
As HandleRef, ByVal msg As Int32, ByVal wParam As IntPtr, ByRef lparam As
Int32) As IntPtr
As HandleRef, ByVal msg As Int32, ByVal wParam As IntPtr, ByVal lparam As
StringBuilder) As IntPtr
Private Declare Unicode Function SendMessageW Lib "user32" (ByVal hWnd
As HandleRef, ByVal msg As Int32, ByVal wParam As IntPtr, ByRef lparam As
Int32) As IntPtr
Private Const WM_GETTEXT As Int32 = &HD
Private Const WM_GETTEXTLENGTH As Int32 = &HE
Private Const WM_GETTEXTLENGTH As Int32 = &HE
End Class
供大家寫程式時參考。