Fetstil Fetstil Kursiv Understrykning linje färgläggning tabellverk Punktlista Nummerlista Vänster Centrerat högerställt Utfyllt Länk Bild htmlmode
  • Forum & Blog
    • Forum - översikt
      • .Net
        • asp.net generellt
        • c#
        • vb.net
        • f#
        • silverlight
        • microsoft surface
        • visual studio .net
      • databaser
        • sql-server
        • databaser
        • access
        • mysql
      • mjukvara klient
        • datorer och komponenter
        • nätverk, lan/wan
        • operativsystem
        • programvaror
        • säkerhet, inställningar
        • windows server
        • allmänt
        • crystal reports
        • exchange/outlook
        • microsoft office
      • mjukvara server
        • active directory
        • biztalk
        • exchange
        • linux
        • sharepoint
        • webbservers
        • sql server
      • appar (win/mobil)
      • programspråk
        • c++
        • delphi
        • java
        • quick basic
        • visual basic
      • scripting
        • asp 3.0
        • flash actionscript
        • html css
        • javascript
        • php
        • regular expresssion
        • xml
      • spel och grafik
        • DirectX
        • Spel och grafik
      • ledning
        • Arkitektur
        • Systemutveckling
        • krav och test
        • projektledning
        • ledningsfrågor
      • vb-sektioner
        • activeX
        • windows api
        • elektronik
        • internet
        • komponenter
        • nätverk
        • operativsystem
      • övriga forum
        • arbete karriär
        • erbjuda uppdrag och tjänster
        • juridiska frågor
        • köp och sälj
        • matematik och fysik
        • intern information
        • skrivklåda
        • webb-operatörer
    • Posta inlägg i forumet
    • Chatta med andra
  • Konto
    • Medlemssida
    • Byta lösenord
    • Bli bonsumedlem
    • iMail
  • Material
    • Tips & tricks
    • Artiklar
    • Programarkiv
  • JOBB
  • Student
    • Studentlicenser
  • KONTAKT
    • Om pellesoft
    • Grundare
    • Kontakta oss
    • Annonsering
    • Partners
    • Felanmälan
  • Logga in

Hem / Tips & tricks

#634 - Färglägg text i en richtextbox

Postat 2001-04-28 21:52:12 av Pelle Johansson i Kategori Programmering, C#, Kommandon med 0 Kommentarer

Option Explicit

Const MAX = 20

Private Enum colors
BLACK
RED
GREEN
BLUE
End Enum

Private Enum keyFlags
None
Spacebar
ReturnKey
End Enum

Dim keyFlag As keyFlags
Dim colorWords$(MAX, BLUE)
Dim rgbColor&(BLUE)


Private Sub Form_Load()
'Red Words
colorWords(0, RED) = "visual basic"
colorWords(1, RED) = "red"
colorWords(2, RED) = "russia"
'Green Words
colorWords(0, GREEN) = "comment"
colorWords(1, GREEN) = "green"
colorWords(2, GREEN) = "italy"
'Blue Words
colorWords(0, BLUE) = "ocean"
colorWords(1, BLUE) = "blue"
colorWords(2, BLUE) = "united states"

'Set the colorcodes
rgbColor(BLACK) = vbBlack
rgbColor(RED) = vbRed
rgbColor(GREEN) = vbGreen
rgbColor(BLUE) = vbBlue

Command1.Caption = "Load"
Command2.Caption = "Save"
Command3.Caption = "Color Line"
End Sub

Private Sub RichTextBox1_KeyDown(KeyCode As Integer, _
Shift As Integer)
Select Case KeyCode
Case vbKeySpace: keyFlag = Spacebar
Case vbKeyReturn: keyFlag = ReturnKey
Case Else: keyFlag = None
End Select
End Sub

Private Sub RichTextBox1_Change()
Static previousWord$
Static previousWordStart&
Static previousWordLength&

Select Case keyFlag
Case None: Exit Sub
Case ReturnKey
If Comment Then Exit Sub
Case Spacebar
If InComment Then Exit Sub
End Select

Dim currentWord$
Dim wordStart&
Dim lengthWord&
Dim selstartholder&

selstartholder = RichTextBox1.SelStart

currentWord = _
GetCurrentWord(wordStart, lengthWord)

If currentWord <> "" Then
Dim j%, w$, found As Boolean
Dim k As colors

currentWord = LCase$(currentWord)
For k = RED To BLUE
For j = 0 To MAX
w = IIf(InStr(colorWords(j, k), " "), _
previousWord & " " & currentWord, _
currentWord)

Select Case colorWords(j, k)
Case w: found = True
Exit For
Case ""
Exit For
End Select
Next
If found Then Exit For
Next

If found Then
Dim lgth&, beginning&
If InStr(w, " ") Then
beginning = previousWordStart
lgth = previousWordLength + _
lengthWord + 1
Else
beginning = wordStart
lgth = lengthWord
End If

colorArea beginning, lgth, k
End If

previousWord = currentWord
previousWordStart = wordStart
previousWordLength = lengthWord
With RichTextBox1
.SelColor = rgbColor(BLACK)
.SelStart = selstartholder
End With
End If
End Sub

Private Function GetCurrentWord(start&, length&)
length = Len(RichTextBox1.Text) - keyFlag
For start = length To 2 Step -1
Select Case Asc(Mid$(RichTextBox1.Text, start, 1))
Case vbKeySpace, 10
start = start + 1
Exit For
End Select
Next

length = length - start + 1
If length > 0 Then
GetCurrentWord = Mid$(RichTextBox1.Text, start, length)
Else
GetCurrentWord = ""
End If
End Function

'Sub to load a file
Private Sub Command1_Click()
With RichTextBox1
.filename = "c:\text.txt"
If Dir$(.filename) = "" Then Exit Sub

If LCase$(Right$(.filename, 3)) <> "rtf" Then
colorTheWords
colorTheComments
End If

.SelStart = Len(.Text)
.SetFocus
End With
End Sub

Private Sub Command2_Click()
RichTextBox1.SaveFile "c:\text.txt", rtfText
End Sub

Private Sub Command3_Click()
Dim beginning&, ending&
Dim selstartholder&

With RichTextBox1
selstartholder = .SelStart
beginning = InComment

If beginning Then
For ending = beginning To Len(.Text)
If Mid$(.Text, ending, 1) = vbCr Then
Exit For
End If
Next
colorArea beginning, ending - beginning, GREEN
Else
colorTheWords .SelStart
End If

.SelStart = selstartholder
.SetFocus
End With
End Sub

Private Sub colorTheWords(Optional start& = 0)
Dim j%, t$, match&, L&, w$
Dim beginning&, ending&
Dim k As colors

t = LCase$(RichTextBox1.Text)
L = Len(t)

If start Then
For beginning = start To 2 Step -1
If Mid$(t, beginning, 1) = vbLf Then
beginning = beginning + 1
Exit For
End If
Next
For ending = start To L - 1
If Mid$(t, ending, 1) = vbCr Then
ending = ending - 1
Exit For
End If
Next
Else
start = RichTextBox1.SelStart
beginning = 1
ending = L
End If


For k = RED To BLUE
For j = 0 To MAX
w = colorWords(j, k)
If w = "" Then Exit For
match = InStr(beginning, t, w)
Do While match >= beginning And _
match < ending
If InComment(match) = 0 Then
colorArea match, Len(w), k
End If
match = InStr(match + 1, t, w)
Loop
Next
Next

With RichTextBox1
.SelStart = start
.SetFocus
End With
End Sub

Private Sub colorTheComments()
Dim beginning&, ending&
Dim selstartholder&

With RichTextBox1
selstartholder = .SelStart

beginning = InStr(.Text, "'")
Do While beginning
If InComment(beginning) Then
For ending = beginning To Len(.Text)
If Mid$(.Text, ending, 1) = vbCr Then
Exit For
End If
Next
colorArea beginning, ending - beginning, GREEN
End If
beginning = InStr(beginning + 1, .Text, "'")
Loop
.SelStart = selstartholder
End With
End Sub

Private Function Comment() As Boolean
Dim beginning&, k%, ending&, m%
Dim selstartholder&

selstartholder = RichTextBox1.SelStart
Comment = False

With RichTextBox1
'We just entered Cr Lf so backup 2 chars
ending = .SelStart - 2
'Beginning of file
If ending <= 0 Then Exit Function
beginning = InComment(ending)
If beginning = 0 Then Exit Function

colorArea beginning, _
ending - beginning + 1, GREEN

.SelStart = selstartholder
End With

Comment = True
End Function

Private Function InComment(Optional start& = 0) As Long
Dim beginning&

With RichTextBox1
If start = 0 Then start = .SelStart

For beginning = start To 2 Step -1
If Mid$(.Text, beginning, 1) = vbLf Then
beginning = beginning + 1
Exit For
End If
Next

If Mid$(.Text, beginning, 1) = "'" Then
InComment = beginning
Else
InComment = 0
End If
End With
End Function

Private Sub colorArea(begin&, lgth&, clr As colors)
With RichTextBox1
.SelStart = begin - 1
.SelLength = lgth
.SelColor = rgbColor(clr)
.SelLength = 0
End With
End Sub

Sample:
Size:

Nyligen

  • 09:09 Vill du köpa medicinska tester?
  • 12:47 Vem beviljar assistansen – kommune
  • 14:17 Någon med erfarenhet av hemstädnin
  • 14:14 Bör man använda sig av en båtförme
  • 14:12 Finns det någon intressant hundblo
  • 14:25 Tips på verktyg för att skapa QR-k
  • 14:23 Tips på verktyg för att skapa QR-k
  • 20:52 Fungerer innskuddsbonuser egentlig

Sidor

  • Hem
  • Bli bonusmedlem
  • Läs artiklar
  • Chatta med andra
  • Sök och erbjud jobb
  • Kontakta oss
  • Studentlicenser
  • Skriv en artikel

Statistik

Antal besökare:
Antal medlemmar:
Antal inlägg:
Online:
På chatten:
4 569 159
27 952
271 704
950
0

Kontakta oss

Frågor runt konsultation, rådgivning, uppdrag, rekrytering, annonsering och övriga ärenden. Ring: 0730-88 22 24 | pelle@pellesoft.se

© 1986-2013 PelleSoft AB. Last Build 4.1.7169.18070 (2019-08-18 10:02:21) 4.0.30319.42000
  • Om
  • Kontakta
  • Regler
  • Cookies