Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help Editor in VB
#1
Hello, here I am asking for help again <!-- sBig Grin --><img src="{SMILIES_PATH}/icon_e_biggrin.gif" alt="Big Grin" title="Very Happy" /><!-- sBig Grin -->. So, what I want is to know if there's a way to make a programm like a PHP editor, I want my application to "read" the code and change some keywords like "for", "while" to a specified color.
#2
Okay it might be abit tricky but you will have to make a new class file either inside your current program or in a new dll file

but here is the code ive come up with and it works fine <!-- sSmile --><img src="{SMILIES_PATH}/icon_e_smile.gif" alt="Smile" title="Smile" /><!-- sSmile -->

Code:
Imports System.Drawing
Public Class SnX
    Inherits System.Windows.Forms.RichTextBox

    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
       (ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer





    Private Declare Function LockWindowUpdate Lib "user32" (ByVal hWnd As Integer) As Integer

    Private _SyntaxHighlight_CaseSensitive As Boolean = False

    Private Words As New DataTable

    Private Enum EditMessages
        LineIndex = 187
        LineFromChar = 201
        GetFirstVisibleLine = 206
        CharFromPos = 215
        PosFromChar = 1062
    End Enum



    Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
        ColorVisibleLines()
    End Sub



    Public Sub ColorRtb()
        Dim FirstVisibleChar As Integer
        Dim i As Integer = 0

        While i < Me.Lines.Length
            FirstVisibleChar = GetCharFromLineIndex(i)
            ColorLineNumber(i, FirstVisibleChar)
            i += 1
        End While
    End Sub



    Public Sub ColorVisibleLines()
        Dim FirstLine As Integer = FirstVisibleLine()
        Dim LastLine As Integer = LastVisibleLine()
        Dim FirstVisibleChar As Integer

        If (FirstLine = 0) And (LastLine = 0) Then
            Exit Sub
        Else
            While FirstLine < LastLine
                FirstVisibleChar = GetCharFromLineIndex(FirstLine)
                ColorLineNumber(FirstLine, FirstVisibleChar)
                FirstLine += 1
            End While
        End If

    End Sub



    Public Sub ColorLineNumber(ByVal LineIndex As Integer, ByVal lStart As Integer)
        Dim i As Integer = 0
        Dim Instance As Integer
        Dim LeadingChar, TrailingChar As String
        Dim SelectionAt As Integer = Me.SelectionStart
        Dim MyRow As DataRow
        Dim Line() As String, MyI As Integer, MyStr As String

        ' Lock the update
        LockWindowUpdate(Me.Handle.ToInt32)

        MyI = lStart

        If CaseSensitive Then
            Line = Split(Me.Lines(LineIndex).ToString, " ")
        Else
            Line = Split(Me.Lines(LineIndex).ToLower, " ")
        End If

        For Each MyStr In Line
            Me.SelectionStart = MyI
            Me.SelectionLength = MyStr.Length

            If Words.Rows.Contains(MyStr) Then
                MyRow = Words.Rows.Find(MyStr)
                If (Not CaseSensitive) Or (CaseSensitive And MyRow("Word") = MyStr) Then
                    Me.SelectionColor = Color.FromName(MyRow("Color"))
                End If
            Else
                Me.SelectionColor = Color.Black
            End If

            MyI += MyStr.Length + 1
        Next

        ' Restore the selectionstart
        Me.SelectionStart = SelectionAt
        Me.SelectionLength = 0
        Me.SelectionColor = Color.Black

        ' Unlock the update
        LockWindowUpdate(0)
    End Sub



    Public Function GetCharFromLineIndex(ByVal LineIndex As Integer) As Integer
        Return SendMessage(Me.Handle, EditMessages.LineIndex, LineIndex, 0)
    End Function



    Public Function FirstVisibleLine() As Integer
        Return SendMessage(Me.Handle, EditMessages.GetFirstVisibleLine, 0, 0)
    End Function



    Public Function LastVisibleLine() As Integer
        Dim LastLine As Integer = FirstVisibleLine() + (Me.Height / Me.Font.Height)

        If LastLine > Me.Lines.Length Or LastLine = 0 Then
            LastLine = Me.Lines.Length
        End If

        Return LastLine
    End Function



    Public Sub New()
        Dim MyRow As DataRow
        Dim arrKeyWords() As String, strKW As String

        Me.AcceptsTab = True

        ''Load all the keywords and the colors to make them
        Words.Columns.Add("Word")
        Words.PrimaryKey = New DataColumn() {Words.Columns(0)}
        Words.Columns.Add("Color")

        arrKeyWords = New String() {"select", "insert", "delete", _
           "truncate", "from", "where", "into", "inner", "update", _
           "outer", "on", "is", "declare", "set", "use", "values", "as", _
           "order", "by", "drop", "view", "go", "trigger", "cube", _
           "binary", "varbinary", "image", "char", "varchar", "text", _
           "datetime", "smalldatetime", "decimal", "numeric", "float", _
           "real", "bigint", "int", "smallint", "tinyint", "money", _
           "smallmoney", "bit", "cursor", "timestamp", "uniqueidentifier", _
           "sql_variant", "table", "nchar", "nvarchar", "ntext", "left", _
           "right", "like", "and", "all", "in", "null", "join", "not", "or"}

        For Each strKW In arrKeyWords
            MyRow = Words.NewRow()
            MyRow("Word") = strKW
            MyRow("Color") = Color.Blue.Name
            Words.Rows.Add(MyRow)
        Next

    End Sub



    Public Property CaseSensitive() As Boolean
        Get
            Return _SyntaxHighlight_CaseSensitive
        End Get
        Set(ByVal Value As Boolean)
            _SyntaxHighlight_CaseSensitive = Value
        End Set
    End Property
Website: <!-- m --><a class="postlink" href="http://www.PBAProductions.com">http://www.PBAProductions.com</a><!-- m -->
E-Mail: <!-- e --><a href="mailtoTongueatrick@AriSystems.Org">Patrick@AriSystems.Org</a><!-- e -->
Skype: Qwaxer
Youtube: Qwaxer
[Image: 2hnx8av.jpg]
#3
Well, you can try using something like this (Don't know how well it would work)
Code:
dim ss as integer = richtextbox1.selectionstart
dim sl as integer = richtextbox1.selectionlength
RichTextbox1.find("for")
richtextbox1.selectioncolor = color.red
richtextbox1.selectionstart = ss
richtextbox1.selectionlenth = sl
My Blog | My Setup | My Videos | Have a wonderful day.
#4
xolara Wrote:Hey if you can provide me with the full syntax list ill have a library ready for you soon

Wow. What a nice guy!
My Blog | My Setup | My Videos | Have a wonderful day.
#5
Updated my post <!-- sTongue --><img src="{SMILIES_PATH}/icon_razz.gif" alt="Tongue" title="Razz" /><!-- sTongue -->
Website: <!-- m --><a class="postlink" href="http://www.PBAProductions.com">http://www.PBAProductions.com</a><!-- m -->
E-Mail: <!-- e --><a href="mailtoTongueatrick@AriSystems.Org">Patrick@AriSystems.Org</a><!-- e -->
Skype: Qwaxer
Youtube: Qwaxer
[Image: 2hnx8av.jpg]
#6
So apparently your programming skills have really improved, Xolara. Nice work.
My Blog | My Setup | My Videos | Have a wonderful day.
#7
Been working on it for some time though just got around finishing it
Website: <!-- m --><a class="postlink" href="http://www.PBAProductions.com">http://www.PBAProductions.com</a><!-- m -->
E-Mail: <!-- e --><a href="mailtoTongueatrick@AriSystems.Org">Patrick@AriSystems.Org</a><!-- e -->
Skype: Qwaxer
Youtube: Qwaxer
[Image: 2hnx8av.jpg]
#8
xolara Wrote:Been working on it for some time though just got around finishing it

Ah wow, very nice!! This could definitely come in handy
My Blog | My Setup | My Videos | Have a wonderful day.
#9
Please report back to me about it <!-- sSmile --><img src="{SMILIES_PATH}/icon_e_smile.gif" alt="Smile" title="Smile" /><!-- sSmile -->
Website: <!-- m --><a class="postlink" href="http://www.PBAProductions.com">http://www.PBAProductions.com</a><!-- m -->
E-Mail: <!-- e --><a href="mailtoTongueatrick@AriSystems.Org">Patrick@AriSystems.Org</a><!-- e -->
Skype: Qwaxer
Youtube: Qwaxer
[Image: 2hnx8av.jpg]
#10
I finally tried this out - It works great!!!
My Blog | My Setup | My Videos | Have a wonderful day.
#11
I have recently found this control as well:
<!-- m --><a class="postlink" href="http://www.freevbcode.com/ShowCode.asp?ID=5176">http://www.freevbcode.com/ShowCode.asp?ID=5176</a><!-- m -->

I believe it is free to use in all applications, but don't quote me on that.

It includes very nice syntax highlighting, comment highlighting, and a very nice line number-er as well. Check it out!
My Blog | My Setup | My Videos | Have a wonderful day.
#12
I think that control doesn't work anymore. And the code that Xolara posted, could anyone explain how I use it? XD
#13
Covert2String Wrote:I think that control doesn't work anymore.

Actually, I am using that control in both TwitControl and HTML-IDEx, and it works perfectly, and very nicely as well. It comes bundled with a sample project, so you can experiment with that to figure out how to use it.

Quote: And the code that Xolara posted, could anyone explain how I use it? XD

As per Xolara's code, I did use it, and it works fine, but right now it is late, tomorrow I'll try to get a nice explanation online.
My Blog | My Setup | My Videos | Have a wonderful day.
#14
Well, I executed the example which was in the control's source, and it didn't changed the colour of the keywords. I'm using Visual Sudio 2010 btw.

Edit: The control works great, thanks! Btw, do you know any control to use configuration files?

file.cfg:

name = "oo"
age = "1"

Just like this, but I have to be able to read & write data to the file in my application.
#15
Yeah, it required a bit of tweaking to get the keywords to actually color themselves (For starters, the keywords had to actually be known). But I am glad you figured it out!

And as for the configuration file control, I do not know of any offhand, because I have always coded it myself. Here is the code for reading a config file, if this helps at all.

Code:
Dim tNAME as string
Dim tAGE As String
Dim reader as new system.io.streamreader(My.application.info.directorypath + "/file.cfg")
Dim split() as String = reader.readtoend.split(vbNewLine)
reader.close()
For each item in split
If item.contains("name=") then
Dim name as string = item.replace("name=","")
name = name.replace(Chr(34),"")
tNAME = name
end if
If item.contains("age=") then
Dim age as string = item.replace("age=","")
age = age.replace(Chr(34),"")
tAGE = age
End if
next
'Sorry if this code messes up, it was coded without experimentation and right on this post
This code will simply loop through every line of a config file, and if the line contains "name", it will remove the "name=", and remove the quotations, and leave you with "oo" (In this case)

Of course, this is tedious because it needs to be done with every single option, but it is really easy to understand.

Hope this helps!
My Blog | My Setup | My Videos | Have a wonderful day.
#16
Thank you for the code, but I coded on by myself <!-- sBig Grin --><img src="{SMILIES_PATH}/icon_e_biggrin.gif" alt="Big Grin" title="Very Happy" /><!-- sBig Grin --> It checks if the line contains a keyword and it reads after the =.
#17
Very nice!! If you would like to, you can share your code snippet in the code snippet section!
My Blog | My Setup | My Videos | Have a wonderful day.
#18
Covert2String Wrote:Hello, here I am asking for help again <!-- sBig Grin --><img src="{SMILIES_PATH}/icon_e_biggrin.gif" alt="Big Grin" title="Very Happy" /><!-- sBig Grin -->. So, what I want is to know if there's a way to make a programm like a PHP editor, I want my application to "read" the code and change some keywords like "for", "while" to a specified color.

PHP Editor would be really difficult, but you can always try a HTML Editor by using a TextBox, a WebBrowser, another textbox and a button!

Basically TextBox1 will be for the HTML code. So we don't really need any code on that one.

Our WebBrowser1 doesn't need any code, neither does TextBox2.

For Button1 just add (I keep thinking I'm working in Flash so I put on(release){ <!-- sBig Grin --><img src="{SMILIES_PATH}/icon_e_biggrin.gif" alt="Big Grin" title="Very Happy" /><!-- sBig Grin -->):

Code:
WebBrowser1.Navigate(TextBox2.Text)
TextBox1.Text = WebBrowser1.DocumentText


Possibly Related Threads…
Thread Author Replies Views Last Post
  HTML - Editor How to create "Preview in Webbrowser " Button Himansh 2 12,575 05-21-2012, 12:08 AM
Last Post: Himansh

Forum Jump:


Users browsing this thread: 1 Guest(s)