Subscribe to Lucky's Thoughts        RSS Feed
-----

Forum Post Reply - Troubles and Temporary Solution

Icon Leave Comment
Last couple of days, I'm having tons of problems, when I'm trying to reply to forum posts.

Here's a Youtube video of my troubles:


Well, as I said in one of my last replyes to that topic, I need to do something about it. Since we are programmers, I made quick Windows Forms solution with RichTextBox, and 7 buttons.

First I defined basic tag types:
    Friend Enum TagEnums
        Member
        Code
        IL
        Media
        URL
        Img
        Quote
    End Enum


Then some abstract or base classes:
#Region "Abstract classes"

    Friend MustInherit Class Tag
        Private _tag As TagEnums

        Public Sub New(tag As TagEnums)
            _tag = tag
        End Sub
    End Class

    Friend MustInherit Class TextInTagOnly
        Inherits Tag

        Public Sub New(tag As TagEnums)
            MyBase.New(tag)
        End Sub

        Public Function ReturnTagText(tagText As String, tagIn As TagEnums) As String
            Return String.Format("[{1}={0}]", tagText, tagIn.ToString.ToLower())
        End Function
    End Class

    Friend MustInherit Class TextInTagAndContinuationWithClosingTag
        Inherits Tag

        Public Sub New(tag As TagEnums)
            MyBase.New(tag)
        End Sub

        Public Function ReturnTagText(textBetweenTags As String, tagText As String, tagIn As TagEnums) As String
            Return String.Format("[{1}={0}]{2}[/{1}]", tagText, tagIn.ToString.ToLower(), textBetweenTags)
        End Function
    End Class

    Friend MustInherit Class TagWithClosingTag
        Inherits Tag

        Public Sub New(tag As TagEnums)
            MyBase.New(tag)
        End Sub

        Public Function ReturnTagText(textBetweenTags As String, tagIn As TagEnums) As String
            Return String.Format("[{0}]{1}[/{0}]", tagIn.ToString.ToLower(), textBetweenTags)
        End Function
    End Class

#End Region



Now, when I had fundamentals, I derived all needed specific classes:

#Region "Derived classes"

    Friend Class MemberTag
        Inherits TextInTagOnly

        Public Sub New()
            MyBase.New(TagEnums.Member)
        End Sub
    End Class

    Friend Class UrlTag
        Inherits TextInTagAndContinuationWithClosingTag

        Public Sub New()
            MyBase.New(TagEnums.URL)
        End Sub
    End Class

    Friend Class CodeTag
        Inherits TagWithClosingTag

        Public Sub New()
            MyBase.New(TagEnums.Code)
        End Sub
    End Class

    Friend Class IlTag
        Inherits TagWithClosingTag

        Public Sub New()
            MyBase.New(TagEnums.IL)
        End Sub
    End Class

    Friend Class MediaTag
        Inherits TagWithClosingTag

        Public Sub New()
            MyBase.New(TagEnums.Media)
        End Sub
    End Class

    Friend Class ImgTag
        Inherits TagWithClosingTag

        Public Sub New()
            MyBase.New(TagEnums.Img)
        End Sub
    End Class

    Friend Class QuoteTag
        Inherits TagWithClosingTag

        Public Sub New()
            MyBase.New(TagEnums.Quote)
        End Sub
    End Class

#End Region



It was time to use those:

Friend Class TagsManager
    Public Member As New MemberTag
    Public Url As New UrlTag
    Public Code As New CodeTag
    Public Il As New IlTag
    Public Media As New MediaTag
    Public Img As New ImgTag
    Public Quote As New QuoteTag

    Public Function ReturnTag(Of T As Tag)(ByVal tag As T,
                                            ByVal text As String,
                                            Optional tagText As String = "") As String
        If TypeOf (tag) Is MemberTag Then
            Return Member.ReturnTagText(text, TagEnums.Member)
        ElseIf TypeOf (tag) Is UrlTag Then
            Return Url.ReturnTagText(text, tagText, TagEnums.URL)
        ElseIf TypeOf (tag) Is CodeTag Then
            Return Code.ReturnTagText(text, TagEnums.Code)
        ElseIf TypeOf (tag) Is IlTag Then
            Return Il.ReturnTagText(text, TagEnums.IL)
        ElseIf TypeOf (tag) Is MediaTag Then
            Return Media.ReturnTagText(text, TagEnums.Media)
        ElseIf TypeOf (tag) Is ImgTag Then
            Return Img.ReturnTagText(text, TagEnums.Img)
        Else
            Return Quote.ReturnTagText(text, TagEnums.Quote)
        End If
    End Function
End Class



In object oriented maner, I just needed to handle buttons clicks in my form:
Public Class DicForumPost
    Private ReadOnly _tags As New TagsManager

    Private Sub btnMember_Click(sender As System.Object, e As System.EventArgs) Handles btnMember.Click
        rtbTextEditor.SelectedText = _tags.ReturnTag(_tags.Member, rtbTextEditor.SelectedText)
    End Sub

    Private Sub btnCode_Click(sender As System.Object, e As System.EventArgs) Handles btnCode.Click
        rtbTextEditor.SelectedText = _tags.ReturnTag(_tags.Code, rtbTextEditor.SelectedText)
    End Sub

    Private Sub btnIlCode_Click(sender As System.Object, e As System.EventArgs) Handles btnIlCode.Click
        rtbTextEditor.SelectedText = _tags.ReturnTag(_tags.Il, rtbTextEditor.SelectedText)
    End Sub

    Private Sub btnMedia_Click(sender As System.Object, e As System.EventArgs) Handles btnMedia.Click
        rtbTextEditor.SelectedText = _tags.ReturnTag(_tags.Media, rtbTextEditor.SelectedText)
    End Sub

    Private Sub btnImg_Click(sender As System.Object, e As System.EventArgs) Handles btnImg.Click
        rtbTextEditor.SelectedText = _tags.ReturnTag(_tags.Img, rtbTextEditor.SelectedText)
    End Sub

    Private Sub btnQuote_Click(sender As System.Object, e As System.EventArgs) Handles btnQuote.Click
        rtbTextEditor.SelectedText = _tags.ReturnTag(_tags.Quote, rtbTextEditor.SelectedText)
    End Sub

    Private Sub btnUrl_Click(sender As System.Object, e As System.EventArgs) Handles btnUrl.Click
        rtbTextEditor.SelectedText = _tags.ReturnTag(_tags.Url, rtbTextEditor.SelectedText, String.Format("""{0}""", rtbTextEditor.SelectedText))
    End Sub
End Class



It's all managed through TagsManager class, and its only function ReturnTag.

Well, if you need something like I did, here you have a simple solution. ;)

Edit: as mentioned site errors are fixed now, DL links are removed.
You can download it here (my DropBox, .exe file) or source solution in my DropBox (couldn't upload to DIC, because upload button isn't working.

0 Comments On This Entry

 

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

August 2014

S M T W T F S
     12
3456789
10111213141516
17181920212223
242526272829 30
31      

Tags

Recent Entries

Recent Comments

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)

Categories