Die Konvertierung ist einfacher, wenn Du den umgekehrten Weg gehst,
also die BBCode-Textattribute in eine Textbox eingibst und sie in
einer RTF-Box entsprechend anzeigen läßt.
Als kleines Beispiel habe ich das für die Fontstyle-Attribute
Fett, Kursiv, Unterstreichen und Durchstreichen programmiert.
Public Class frmBBCode
Dim tbo As New TextBox With _
{.Parent = Me, .Width = 300, .Height = 300, .Multiline = True}
Dim WithEvents btnConvert As New Button With _
{.Parent = Me, .Width = 100, .Height = 30, .Top = 330, _
.Text = "Konvertieren"}
Dim rbo As New RichTextBox With _
{.Parent = Me, .Width = 300, .Height = 300, .Top = 380}
Private Sub frmBBCode_Load(sender As System.Object, _
e As System.EventArgs) Handles MyBase.Load
Me.MinimumSize = New Size(350, 700)
End Sub
Private Sub btnConvert_Click(sender As Object, _
e As System.EventArgs) Handles btnConvert.Click
Dim attributes As String = "bius"
Dim area As Point = Nothing, startindex As Integer
Dim fs As FontStyle
With rbo
.Text = tbo.Text
'Schleife über die unterstützten Attribute
For i As Integer = 0 To attributes.Length - 1
startindex = 0
Do
'Suche nach nächstem Text-Bereich den das Attribut
' umschließt
area = FindArea(.Text, startindex, attributes(i))
If Not area = Nothing Then
'BBCode aus dem RichText entfernen
.SelectionStart = area.X
.SelectionLength = 3
.SelectedText = ""
.SelectionStart = area.X + area.Y - 4
.SelectionLength = 4
.SelectedText = ""
'zeichenweise das FontStyle-Attribut einfügen
For k As Integer = area.X To area.X + area.Y - 5
.SelectionStart = k
.SelectionLength = 1
fs = .SelectionFont.Style
Select Case attributes(i)
Case Is = "b"c
fs = fs Or FontStyle.Bold
Case Is = "i"c
fs = fs Or FontStyle.Italic
Case Is = "u"c
fs = fs Or FontStyle.Underline
Case Is = "s"c
fs = fs Or FontStyle.Strikeout
End Select
.SelectionFont = New Font(.Font.Name, .Font.Size, _
fs)
Next k
startindex = area.X + area.Y
End If
Loop While Not area = Nothing And startindex < .Text.Length
Next i
End With
End Sub
Private Function FindArea(ByVal text As String, _
ByVal startindex As Integer, _
ByVal Attribute As String) As Point
Dim start, ende As Integer
Try
start = text.ToLower.IndexOf("[" & Attribute & "]", startindex)
If start = -1 Then Return Nothing
ende = text.ToLower.IndexOf("[/" & Attribute & "]", start + 2)
If ende = -1 Then Return Nothing
Return New Point(start, ende - start + 1)
Catch
Return Nothing
End Try
End Function
End Class |