Hej! Jag tror att jag har fått det att fungera nu.Färga en hel rad i en RTB
Jag har lite problem med den här subfunktionen, den beter sig lite konstigt men jag ser inget fel med den. Ser ni nått fel?
<code>
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Const EM_LINEINDEX = &HBB
Public Const EM_LINELENGTH = &HC1
Public Sub ColorLines(TextBox As RichTextBox)
On Error Resume Next
Dim strLines() As String, Line, lngLineCount As Long
Dim lngStoredStart As Long, lngStoredLength As Long, lngStart As Long, lngLength As Long
lngLineCount = 0
lngStoredStart = TextBox.SelStart
lngStoredLength = TextBox.SelLength
strLines = Split(TextBox.Text, vbCrLf)
For Each Line In strLines
lngStart = SendMessage(TextBox.hwnd, EM_LINEINDEX, lngLineCount, 0&)
lngLength = SendMessage(TextBox.hwnd, EM_LINELENGTH, lngLineCount, 0&)
If lngStart = -1 Or lngLength = -1 Then
Exit For
End If
If Left(Line, 1) = "'" Then
TextBox.SelStart = lngStart
TextBox.SelLength = lngLength
TextBox.SelColor = vbRed
Else
TextBox.SelStart = lngStart
TextBox.SelLength = lngLength
TextBox.SelColor = vbBlack
End If
lngLineCount = lngLineCount + 1
Next Line
TextBox.SelStart = lngStoredStart
TextBox.SelLength = lngStoredLength
TextBox.SelColor = vbBlack
End Sub
</code>
Edit: Kom på att jag hade gjort något konstigt där, jag har ändrat det nu men problemet kvarstår.
/MickeSv: Färga en hel rad i en RTB
<code>
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Const EM_LINEINDEX = &HBB
Public Const EM_LINELENGTH = &HC1
Public Sub ColorLines(TextBox As RichTextBox)
On Error Resume Next
Dim strLines() As String, Line, lngLineCount As Long
Dim lngStoredStart As Long, lngStoredLength As Long, lngStart As Long, lngLength As Long
lngLineCount = 1
lngStoredStart = TextBox.SelStart
lngStoredLength = TextBox.SelLength
strLines = Split(TextBox.Text, vbCrLf)
For Each Line In strLines
lngStart = SendMessage(TextBox.hwnd, EM_LINEINDEX, lngLineCount - 1, 0&)
lngLength = SendMessage(TextBox.hwnd, EM_LINELENGTH, lngStart, 0&)
If lngStart = -1 Or lngLength = -1 Then
Exit For
End If
If Left(Line, 1) = "'" Then
TextBox.SelStart = lngStart
TextBox.SelLength = lngLength
TextBox.SelColor = vbRed
Else
TextBox.SelStart = lngStart
TextBox.SelLength = lngLength
TextBox.SelColor = vbBlack
End If
lngLineCount = lngLineCount + 1
Next Line
TextBox.SelStart = lngStoredStart
TextBox.SelLength = lngStoredLength
TextBox.SelColor = vbBlack
End Sub
</code>
/Micke