Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
HTML IntelliSense 1.5 - Form1.vb

Form1.vb

Caricato da: Totem
Scarica il programma completo

  1. Imports System.Text.RegularExpressions
  2. Public Class Form1
  3.     Private Enum ListContentType
  4.         Tags
  5.         Attributes
  6.     End Enum
  7.  
  8.     'Costanti usate per lo scrolling
  9.     Private Const HorizontalScrolling = &H114
  10.     Private Const VerticalScrolling = &H115
  11.     Private Const HorizontalScrollBar = 0
  12.     Private Const VerticalScrollBar = 1
  13.     Private Const ThumbPosition = 4
  14.  
  15.  
  16.     Private TextModified As Boolean = False
  17.     'Ultimo path di salvataggio del file
  18.     Private LastSavingPath As String
  19.     'Parola da cercare per la funzione Trova
  20.     Private WordToSeek As String
  21.     'Ultimo indice verificato per tale parola
  22.     Private LastValidIndex As Int32
  23.     'Tipo di contenuto della lista
  24.     Private ContentType As ListContentType
  25.  
  26.     Private rtbSupport As New HTML_IntelliSense.HtmlTextBox
  27.  
  28.     Private FirstLine, LastLine As Int32
  29.  
  30. #Region "Platform Invoke"
  31.  
  32.     <System.Runtime.InteropServices.DllImport("user32.dll", CharSet:=Runtime.InteropServices.CharSet.Auto, EntryPoint:="SendMessage")> _
  33.     Private Shared Function SendMessage(ByVal Handle As IntPtr, ByVal Message As UInt32, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
  34.  
  35.     End Function
  36.  
  37.     <System.Runtime.InteropServices.DllImport("user32.dll")> _
  38.     Private Shared Function GetScrollPos(ByVal Handle As IntPtr, ByVal Bar As Int32) As Int32
  39.  
  40.     End Function
  41.  
  42. #End Region
  43.  
  44. #Region "Metodi addizionali"
  45.  
  46.     Private Sub AddItemsToList(ByVal List As List(Of String), ByVal StartString As String)
  47.         lstSuggest.Items.Clear()
  48.         For Each ATag As String In List
  49.             If ATag.ToLower.StartsWith(StartString.ToLower) Then
  50.                 lstSuggest.Items.Add(ATag)
  51.             End If
  52.         Next
  53.         lstSuggest.Tag = StartString
  54.     End Sub
  55.  
  56.     Private Sub ShowListAtCursor()
  57.         Dim P As Point = rtbCode.GetPositionFromCharIndex(rtbCode.SelectionStart)
  58.         lstSuggest.Location = New Point(P.X + 8, P.Y + 48)
  59.         lstSuggest.Visible = True
  60.         Me.Refresh()
  61.     End Sub
  62.  
  63.     Private Sub InsertText(ByVal Str As String)
  64.         If String.IsNullOrEmpty(Str) Then
  65.             Exit Sub
  66.         End If
  67.  
  68.         If rtbCode.SyntaxHighlightingEnabled Then
  69.             rtbCode.UpdateRtfLines()
  70.             rtbSupport = rtbCode.Clone
  71.  
  72.             Dim Line As String
  73.             Dim PrevRtf() As String = rtbCode.RtfLines
  74.             Dim RtfLineIndex As Int32 = rtbCode.RtfCurrentLineIndex
  75.             Dim PrevPos As Int32 = GetScrollPos(rtbCode.Handle, VerticalScrollBar)
  76.  
  77.             If rtbCode.Lines.Length = 0 Then
  78.                 Line = Str
  79.             Else
  80.                 Line = rtbCode.Lines(rtbCode.CurrentLineIndex).Insert(rtbCode.SelectionStart - rtbCode.GetFirstCharIndexOfCurrentLine, Str)
  81.             End If
  82.             rtbSupport.Text = Line
  83.             rtbSupport.ColorRtb()
  84.  
  85.             Dim RtfBuilder As New System.Text.StringBuilder
  86.  
  87.             rtbSupport.UpdateRtfLines()
  88.             For I As Int32 = 2 To rtbSupport.RtfLines.Length - 1
  89.                 If rtbSupport.RtfLines(I).EndsWith("}") Then
  90.                     Exit For
  91.                 End If
  92.                 RtfBuilder.Append(rtbSupport.RtfLines(I))
  93.             Next
  94.             Line = RtfBuilder.ToString
  95.  
  96.             RtfBuilder.Remove(0, RtfBuilder.Length)
  97.  
  98.             For I As Int32 = 0 To PrevRtf.Length - 1
  99.                 If I = RtfLineIndex Then
  100.                     RtfBuilder.AppendLine(Line)
  101.                 Else
  102.                     RtfBuilder.AppendLine(PrevRtf(I))
  103.                 End If
  104.             Next
  105.  
  106.             rtbCode.Rtf = RtfBuilder.ToString
  107.  
  108.             SendMessage(rtbCode.Handle, VerticalScrolling, 4 + &H10000 * PrevPos, 0)
  109.         Else
  110.             rtbCode.SelectedText = Str
  111.         End If
  112.     End Sub
  113.  
  114.     Private Sub AppendTextAtCursor(ByVal Str As String)
  115.         Dim Sel As Int32 = rtbCode.SelectionStart
  116.         InsertText(Str)
  117.         rtbCode.SelectionStart = Sel + Str.Length
  118.         lstSuggest.Tag = Str
  119.     End Sub
  120.  
  121.     Private Sub CompleteTag(ByVal Tag As HtmlTag)
  122.         Dim Sel As Int32 = rtbCode.SelectionStart
  123.         Dim LineIndex As Int32 = rtbCode.GetLineFromCharIndex(rtbCode.SelectionStart)
  124.  
  125.         If Not Tag.CanClose Then
  126.             'rtbCode.Text = rtbCode.Text.Insert(rtbCode.SelectionStart, ">")
  127.             InsertText(">")
  128.             rtbCode.ColorLineNumber(LineIndex)
  129.             rtbCode.SelectionStart = Sel + 1
  130.             Exit Sub
  131.         End If
  132.  
  133.         If Tag.ThreeLinesDeclaration Then
  134.             Dim Spaces As String = ""
  135.  
  136.             If LineIndex >= 0 Then
  137.                 Dim PrevLine As String = rtbCode.Lines(LineIndex)
  138.                 Dim Indent As New Regex("^(?<Indent>\s+)", RegexOptions.Multiline)
  139.                 Dim M As Match = Indent.Match(PrevLine)
  140.                 If M.Success Then
  141.                     Spaces = M.Groups("Indent").Value
  142.                 End If
  143.             End If
  144.  
  145.             'rtbCode.Text = rtbCode.Text.Insert(rtbCode.SelectionStart, _
  146.             '">" & vbCrLf & Spaces & "  " & vbCrLf & Spaces & "</" & Tag.Name & ">")
  147.             InsertText(">" & vbCrLf & Spaces & "  " & vbCrLf & Spaces & "</" & Tag.Name & ">")
  148.             rtbCode.SelectionStart = Sel + (Spaces.Length + 4)
  149.             rtbCode.ColorLineNumber(LineIndex)
  150.             rtbCode.ColorLineNumber(LineIndex + 2)
  151.         Else
  152.             InsertText("></" & Tag.Name & ">")
  153.             rtbCode.ColorLineNumber(LineIndex)
  154.             rtbCode.SelectionStart = Sel + 1
  155.         End If
  156.  
  157.         lstSuggest.Tag = ""
  158.     End Sub
  159.  
  160.     Private Sub RefreshTemplates()
  161.         strTemplates.DropDownItems.Clear()
  162.         For Each TemplateFile As String In IO.Directory.GetFiles(Application.StartupPath & "\Templates", "*.hist")
  163.             Dim T As New ToolStripMenuItem
  164.             T.Text = IO.Path.GetFileNameWithoutExtension(TemplateFile)
  165.             AddHandler T.Click, AddressOf strTemplatesChild_Click
  166.             strTemplates.DropDownItems.Add(T)
  167.         Next
  168.     End Sub
  169.  
  170. #End Region
  171.  
  172.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  173.         My.Application.SaveMySettingsOnExit = True
  174.         AddHandler My.Application.UnhandledException, AddressOf ExceptionHandler
  175.  
  176.         If Not IO.File.Exists(Application.StartupPath & "\Tags.dat") Then
  177.             MessageBox.Show("Il file contente i dati sui tag html non è presente nella directory del programma. Contattare lo sviluppatore per ottenerne uno nuovo!", Me.Text)
  178.         Else
  179.             Dim Reader As New IO.StreamReader(Application.StartupPath & "\Tags.dat")
  180.             Dim Line, Data() As String
  181.  
  182.             While Not Reader.EndOfStream
  183.                 Line = Reader.ReadLine
  184.                 If String.IsNullOrEmpty(Line) Then
  185.                     Continue While
  186.                 End If
  187.                 Data = Line.Split("|")
  188.  
  189.                 Dim T As New HtmlTag()
  190.                 T.Name = Data(0)
  191.                 If Data.Length > 3 Then
  192.                     For I As Int16 = 1 To Data.Length - 3
  193.                         T.Attributes.Add(Data(I))
  194.                     Next
  195.                 End If
  196.                 T.CanClose = CBool(Data(Data.Length - 2))
  197.                 T.ThreeLinesDeclaration = CBool(Data(Data.Length - 1))
  198.  
  199.                 Tags.Add(T.Name, T)
  200.                 TagNames.Add(T.Name)
  201.             End While
  202.  
  203.             Reader.Close()
  204.         End If
  205.  
  206.         If Not IO.Directory.Exists(Application.StartupPath & "\Templates") Then
  207.             IO.Directory.CreateDirectory(Application.StartupPath & "\Templates")
  208.         End If
  209.  
  210.         RefreshTemplates()
  211.  
  212.         My.Application.SaveMySettingsOnExit = True
  213.     End Sub
  214.  
  215.     Private Sub rtbCode_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rtbCode.HtmlChanged
  216.         TextModified = True
  217.         If rtbCode.SelectionStart > 0 And My.Settings.ISEnabled Then
  218.             Dim Start As Int32 = rtbCode.SelectionStart
  219.  
  220.             'Trova il < d'inizio.
  221.             Dim OpeningTag As Int32 = rtbCode.Text.LastIndexOf("<", Start - 1, Start)
  222.             'Se c'è, controlla che sia di questo tag
  223.             If OpeningTag = -1 Then
  224.                 Exit Sub
  225.             End If
  226.  
  227.             'Trova il primo > prima del cursore
  228.             Dim PrevEndingTag As Int32 = rtbCode.Text.LastIndexOf(">", Start - 1, Start)
  229.             'Se esiste, controlla che sia precedente al <
  230.             If (PrevEndingTag >= 0) AndAlso (PrevEndingTag > OpeningTag) Then
  231.                 'Il < trovato in realtà era di un altro tag già chiuso
  232.                 Exit Sub
  233.             End If
  234.  
  235.             'Controlla se il tag è chiuso
  236.             Dim EndingTag As Int32 = rtbCode.Text.IndexOf(">", Start)
  237.             Dim SuccOpeningTag As Int32 = rtbCode.Text.IndexOf("<", Start)
  238.             Dim IsClosed As Boolean = (EndingTag >= 0) And (EndingTag < SuccOpeningTag)
  239.  
  240.             'Localizza il tag
  241.             Dim Tag As String
  242.  
  243.             If Not IsClosed Then
  244.                 If SuccOpeningTag >= 0 Then
  245.                     'Situazione:
  246.                     '<a href="ciao" <div> ...
  247.                     Tag = rtbCode.Text.Substring(OpeningTag, SuccOpeningTag - OpeningTag + 1)
  248.                 Else
  249.                     'Situazione:
  250.                     '<a href="ciao" [Fine file]
  251.                     Tag = rtbCode.Text.Substring(OpeningTag)
  252.                 End If
  253.             Else
  254.                 'Situazione
  255.                 '<a href="ciao"> ...
  256.                 Tag = rtbCode.Text.Substring(OpeningTag, EndingTag - OpeningTag + 1)
  257.             End If
  258.  
  259.             lblStatus.Text = Tag
  260.  
  261.             If Tag.Length < 2 Then
  262.                 'E' presente solo <: mostra tutti i tag disponibili
  263.                 Me.AddItemsToList(TagNames, "")
  264.                 Me.ContentType = ListContentType.Tags
  265.                 Me.ShowListAtCursor()
  266.                 Exit Sub
  267.             End If
  268.  
  269.             'La porzione di stringa compresa tra l'inizio del tag e la posizione corrente
  270.             Dim Str As String = rtbCode.Text.Substring(OpeningTag, Start - OpeningTag)
  271.  
  272.             If Not Str.Contains(" ") Then
  273.                 'Se Str non contiene spazi, è una sola parola. Quindi del tipo:
  274.                 '<abcdefgh oppure </abcdef
  275.                 'e perciò è l'inizio di un tag. Ora suggerisce una lista di tutti i
  276.                 'tag che iniziano in quel modo
  277.  
  278.                 'Si tratta di un tag di chiusura: cancella il </ iniziale
  279.                 If Str.StartsWith("</") Then
  280.                     Str = Str.Remove(0, 2)
  281.                 Else
  282.                     'Cancella il < iniziale
  283.                     Str = Str.Remove(0, 1)
  284.                 End If
  285.  
  286.                 'Str è il nome parziale del tag
  287.                 Me.AddItemsToList(TagNames, Str)
  288.                 Me.ContentType = ListContentType.Tags
  289.                 If lstSuggest.Items.Count > 0 Then
  290.                     Me.ShowListAtCursor()
  291.                 End If
  292.             Else
  293.                 'Riduce il controllo al solo tag in modo che sia più facile
  294.                 'da analizzare
  295.                 Dim TagCursor As Int32 = Start - OpeningTag - 1
  296.                 Dim QuoteOpened As Boolean = False
  297.  
  298.                 For I As Int32 = 0 To Tag.Length - 1
  299.                     If Tag(I) = Chr(34) Then
  300.                         QuoteOpened = Not QuoteOpened
  301.                         Tag = Tag.Insert(I, "|")
  302.                         Tag = Tag.Remove(I + 1, 1)
  303.                     End If
  304.                     If QuoteOpened Then
  305.                         Tag = Tag.Insert(I, "|")
  306.                         Tag = Tag.Remove(I + 1, 1)
  307.                     End If
  308.                 Next
  309.  
  310.                 'Quindi, se all'indice corrente c'è un pipe, significa che il cursore
  311.                 'è dentro una stringa
  312.                 If Tag(TagCursor) = "|" Then
  313.                     'Nasconde la lista
  314.                     lstSuggest.Visible = False
  315.                 Else
  316.                     'Ottiene la parola che si sta scrivendo
  317.                     Dim PrevSpace As Int32 = Tag.LastIndexOf(" ", TagCursor, TagCursor + 1)
  318.                     Dim TagName, PartialAttrName As String
  319.  
  320.                     'Trova il nome del tag, togliendo il < iniziale e lo spazio finale
  321.                     TagName = Tag.Substring(0, Tag.IndexOf(" ")).Remove(0, 1)
  322.                     PartialAttrName = Tag.Substring(PrevSpace + 1, TagCursor - PrevSpace)
  323.                     'Ottiene solo gli attributi che iniziano con la stringa data
  324.                     If TagNames.Contains(TagName) Then
  325.                         Me.AddItemsToList(Tags(TagName).Attributes, PartialAttrName)
  326.                         Me.ContentType = ListContentType.Attributes
  327.                         If (lstSuggest.Items.Count = 1) AndAlso (TagName = lstSuggest.Items(0)) Then
  328.                             lstSuggest.Visible = False
  329.                         Else
  330.                             Me.ShowListAtCursor()
  331.                         End If
  332.                     Else
  333.                         lstSuggest.Visible = False
  334.                     End If
  335.                 End If
  336.             End If
  337.  
  338.             rtbCode.SelectionStart = Start
  339.             lstSuggest.Visible = (lstSuggest.Items.Count > 0)
  340.         End If
  341.     End Sub
  342.  
  343.     Private Sub rtbCode_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles rtbCode.KeyPress
  344.         If (e.KeyChar = ">" Or e.KeyChar = vbCr) And My.Settings.ISEnabled Then
  345.             If e.KeyChar = ">" Then
  346.                 Dim OpeningTag As Int32 = rtbCode.Text.LastIndexOf("<", rtbCode.SelectionStart, rtbCode.SelectionStart + 1)
  347.  
  348.                 Try
  349.                     If rtbCode.Text(OpeningTag + 1) = "/" Then
  350.                         lstSuggest.Visible = False
  351.                         Exit Sub
  352.                     End If
  353.  
  354.                     If lstSuggest.Visible And lstSuggest.Items.Count > 0 Then
  355.                         Dim TagName As String = lstSuggest.Items(0)
  356.                         Dim Str As String = TagName.Remove(0, lstSuggest.Tag.ToString.Length)
  357.  
  358.                         Me.AppendTextAtCursor(Str)
  359.                         Me.CompleteTag(Globals.Tags(TagName))
  360.                         lstSuggest.Visible = False
  361.                         e.Handled = True
  362.                         'rtbCode.ColorVisibleLines()
  363.                         Exit Sub
  364.                     End If
  365.                 Catch Ex As Exception
  366.                 End Try
  367.  
  368.                 Try
  369.                     Dim Str As String = rtbCode.Text.Substring(OpeningTag, rtbCode.SelectionStart - OpeningTag)
  370.                     Dim TagName As String
  371.                     If Not Str.Contains(" ") Then
  372.                         TagName = rtbCode.Text.Substring(OpeningTag + 1, rtbCode.SelectionStart - OpeningTag - 1)
  373.                     Else
  374.                         TagName = rtbCode.Text.Substring(OpeningTag + 1, rtbCode.Text.IndexOf(" ", OpeningTag) - OpeningTag - 1)
  375.                     End If
  376.                     Me.CompleteTag(Globals.Tags(TagName))
  377.                     lstSuggest.Visible = False
  378.                     e.Handled = True
  379.                     'rtbCode.ColorVisibleLines()
  380.                 Catch Ex As Exception
  381.                 End Try
  382.             End If
  383.  
  384.             If e.KeyChar = vbCr Then
  385.                 Dim LineIndex As Int32 = rtbCode.GetLineFromCharIndex(rtbCode.SelectionStart)
  386.                 Dim PrevLine As String = rtbCode.Lines(LineIndex - 1)
  387.                 Dim Indent As New Regex("^(?<Indent>\s+)", RegexOptions.Multiline)
  388.                 Dim M As Match = Indent.Match(PrevLine)
  389.  
  390.                 If M.Success Then
  391.                     rtbCode.HtmlChangedEnabled = False
  392.                     Me.AppendTextAtCursor(M.Groups("Indent").Value)
  393.                     rtbCode.HtmlChangedEnabled = True
  394.                 End If
  395.                 e.Handled = True
  396.                 'rtbCode.ColorVisibleLines()
  397.             End If
  398.  
  399.             lstSuggest.Visible = False
  400.         End If
  401.     End Sub
  402.  
  403.     Private Sub lstSuggest_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstSuggest.DoubleClick
  404.         'Aggiunge la parte di parola mancante
  405.         If lstSuggest.SelectedIndex >= 0 Then
  406.             Try
  407.                 Dim Str As String = CStr(lstSuggest.SelectedItem).Remove(0, lstSuggest.Tag.ToString.Length)
  408.                 Me.AppendTextAtCursor(Str)
  409.                 lstSuggest.Tag = ""
  410.             Catch Ex As Exception
  411.             End Try
  412.         End If
  413.     End Sub
  414.  
  415.     Private Sub rtbCode_PreviewKeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles rtbCode.PreviewKeyDown
  416.         If e.KeyCode = Keys.Space Then
  417.             If lstSuggest.Visible And lstSuggest.Items.Count > 0 Then
  418.                 Dim Str As String = CStr(lstSuggest.Items(0)).Remove(0, lstSuggest.Tag.ToString.Length)
  419.                 Me.AppendTextAtCursor(Str)
  420.             End If
  421.         ElseIf (e.Shift And e.KeyCode = Keys.D0) Then
  422.             If lstSuggest.Visible And lstSuggest.Items.Count > 0 And Me.ContentType = ListContentType.Attributes Then
  423.                 Dim Str As String = CStr(lstSuggest.Items(0)).Remove(0, lstSuggest.Tag.ToString.Length)
  424.                 Me.AppendTextAtCursor(Str)
  425.             End If
  426.         ElseIf (e.Alt And e.KeyCode = Keys.Oemplus) Then
  427.             If lstSuggest.Visible And lstSuggest.Items.Count > 0 And Me.ContentType = ListContentType.Attributes AndAlso _
  428.                 lstSuggest.Items(0).ToString.StartsWith("[") Then
  429.                 Dim Str As String = CStr(lstSuggest.Items(0)).Remove(0, lstSuggest.Tag.ToString.Length)
  430.                 Me.AppendTextAtCursor(Str.Replace("]", ""))
  431.             End If
  432.         ElseIf e.KeyCode = Keys.Down Then
  433.             If lstSuggest.Visible Then
  434.                 lstSuggest.SelectedIndex = 0
  435.                 lstSuggest.Focus()
  436.             End If
  437.         End If
  438.     End Sub
  439.  
  440.     Private Sub strOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strOpen.Click
  441.         If TextModified Then
  442.             If MessageBox.Show("Salvare le modifiche apportate al codice prima di aprirne un altro?", Me.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
  443.                 strSave_Click(Me, EventArgs.Empty)
  444.                 TextModified = False
  445.             End If
  446.         End If
  447.  
  448.         Dim Open As New OpenFileDialog
  449.         Open.Filter = "Pagine web|*.htm;*.html;*.php"
  450.         If Open.ShowDialog = Windows.Forms.DialogResult.OK Then
  451.             rtbCode.Text = IO.File.ReadAllText(Open.FileName)
  452.             strHighlightAll_Click(Me, EventArgs.Empty)
  453.         End If
  454.     End Sub
  455.  
  456.     Private Sub strSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strSave.Click
  457.         If String.IsNullOrEmpty(LastSavingPath) Then
  458.             Dim Save As New SaveFileDialog
  459.             Save.Filter = "Pagine web|*.htm;*.html|Pagina PHP|*.php"
  460.             If Save.ShowDialog = Windows.Forms.DialogResult.OK Then
  461.                 IO.File.WriteAllText(Save.FileName, rtbCode.Text)
  462.                 LastSavingPath = Save.FileName
  463.                 lblStatus.Text = "File salvato con successo"
  464.                 TextModified = False
  465.             End If
  466.         Else
  467.             IO.File.WriteAllText(LastSavingPath, rtbCode.Text)
  468.             lblStatus.Text = "File salvato con successo"
  469.             TextModified = False
  470.         End If
  471.     End Sub
  472.  
  473.     Private Sub strSaveAs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strSaveAs.Click
  474.         'Annulla la stringa, e richiama l'altra funzione, risparmiando spazio
  475.         LastSavingPath = Nothing
  476.         strSave_Click(Me, EventArgs.Empty)
  477.     End Sub
  478.  
  479.     Private Sub strExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strExit.Click
  480.         If TextModified Then
  481.             If MessageBox.Show("Salvare le modifiche apportate al sorgente prima di uscire?", Me.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
  482.                 strSave_Click(Me, EventArgs.Empty)
  483.             End If
  484.         End If
  485.         Me.Close()
  486.     End Sub
  487.  
  488.     Private Sub strFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strFind.Click
  489.         Dim Word As String = InputBox("Immettere la stringa da cercare:", Me.Text)
  490.  
  491.         If String.IsNullOrEmpty(Word) Then
  492.             Exit Sub
  493.         End If
  494.  
  495.         If Word.Length < 2 Then
  496.             MessageBox.Show("Inserire una stringa di almeno due caratteri!", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  497.             Exit Sub
  498.         End If
  499.  
  500.         Dim Index As Int32
  501.         Index = rtbCode.Find(Word)
  502.         If Index > -1 Then
  503.             WordToSeek = Word
  504.             LastValidIndex = Index
  505.             rtbCode.Select(Index, Word.Length)
  506.             rtbCode.ScrollToCaret()
  507.             lblStatus.Text = "Trovata istanza di """ & Word & """ alla posizione " & Index
  508.         End If
  509.     End Sub
  510.  
  511.     Private Sub strFindNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strFindNext.Click
  512.         If Not String.IsNullOrEmpty(WordToSeek) Then
  513.             Dim Index As Int32
  514.  
  515.             If LastValidIndex < rtbCode.TextLength - 1 Then
  516.                 Index = rtbCode.Find(WordToSeek, LastValidIndex + 1, RichTextBoxFinds.None)
  517.                 If Index > -1 Then
  518.                     LastValidIndex = Index
  519.                     rtbCode.Select(Index, WordToSeek.Length)
  520.                     rtbCode.ScrollToCaret()
  521.                     lblStatus.Text = "Trovata istanza di """ & WordToSeek & """ alla posizione " & Index
  522.                 Else
  523.                     MessageBox.Show("Nessun'altra occorenza di """ & WordToSeek & """ è stata trovata!", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  524.                 End If
  525.             Else
  526.                 MessageBox.Show("Nessun'altra occorenza di """ & WordToSeek & """ è stata trovata!", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  527.             End If
  528.         End If
  529.     End Sub
  530.  
  531.     Private Sub strReplace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strReplace.Click
  532.         Dim Word As String = InputBox("Inserire la stringa da sotituire:", Me.Text)
  533.         Dim Replace As String = InputBox("Inserire la stringa sostituto:", Me.Text)
  534.  
  535.         If (Not String.IsNullOrEmpty(Word)) And (Not String.IsNullOrEmpty(Replace)) Then
  536.             lblStatus.Text = "Attendere, sostituzione in corso..."
  537.             rtbCode.Enabled = False
  538.             Application.DoEvents()
  539.             rtbCode.Text = rtbCode.Text.Replace(Word, Replace)
  540.             Application.DoEvents()
  541.             rtbCode.Enabled = True
  542.             lblStatus.Text = "Sostituzione effettuata"
  543.         End If
  544.     End Sub
  545.  
  546.     Private Sub strEnabled_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strEnabled.Click
  547.         My.Settings.ISEnabled = strEnabled.Checked
  548.         If strEnabled.Checked Then
  549.             strEnabled.Text = "Attivato"
  550.         Else
  551.             strEnabled.Text = "Disattivato"
  552.         End If
  553.     End Sub
  554.  
  555.     Private Sub strAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strAbout.Click
  556.         My.Forms.The_Lair_AboutBox1.Show()
  557.     End Sub
  558.  
  559.     Private Sub ExceptionHandler(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs)
  560.         MessageBox.Show(String.Format( _
  561.         "Si è verificata un'eccezione del tipo {0}. Il testo di tale eccezione è di seguito riportato: {1}{1}{2}{1}{1}Contattare lo sviluppatore per maggiori dettagli.", _
  562.         e.Exception.GetType.FullName, vbCrLf, e.Exception.Message), Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
  563.     End Sub
  564.  
  565.     Private Sub lstSuggest_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles lstSuggest.DrawItem
  566.         'Rect rappresenta il rettangolo in cui è contenuto l'elemento
  567.         Dim Rect As Rectangle = e.Bounds
  568.         'Fnt rappresenta il font con cui l'elemento deve essere scritto
  569.         Dim Fnt As Font = e.Font
  570.         'Testo dell'elemento
  571.         Dim ItemText As String = lstSuggest.Items(e.Index)
  572.         'Immagine dell'elemento
  573.         Dim ItemImage As Image = Nothing
  574.  
  575.         Select Case Me.ContentType
  576.             Case ListContentType.Tags
  577.                 ItemImage = imgIcons.Images("tag")
  578.             Case ListContentType.Attributes
  579.                 If ItemText.StartsWith("[") Then
  580.                     ItemImage = imgIcons.Images("bool")
  581.                 Else
  582.                     ItemImage = imgIcons.Images("attribute")
  583.                 End If
  584.         End Select
  585.  
  586.         With e.Graphics
  587.             If e.State = DrawItemState.Selected Then
  588.                 .FillRectangle(New SolidBrush(Color.FromArgb(255, 51, 94, 168)), e.Bounds.X + ItemImage.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)
  589.                 .DrawString(ItemText, Fnt, New SolidBrush(Color.White), e.Bounds.X + ItemImage.Width, e.Bounds.Y)
  590.             ElseIf e.State = DrawItemState.None Then
  591.                 .FillRectangle(New SolidBrush(Color.White), e.Bounds.X, e.Bounds.Y, e.Bounds.Width + ItemImage.Width, e.Bounds.Height)
  592.                 .DrawString(ItemText, Fnt, New SolidBrush(Color.Black), e.Bounds.X + ItemImage.Width, e.Bounds.Y)
  593.             End If
  594.             .FillRectangle(Brushes.White, e.Bounds.X, e.Bounds.Y, ItemImage.Width, ItemImage.Height + 1)
  595.             .DrawImage(ItemImage, e.Bounds.X, e.Bounds.Y)
  596.         End With
  597.     End Sub
  598.  
  599.     Private Sub strISOption_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strISOption.Click
  600.         My.Forms.ISOption.ShowDialog()
  601.     End Sub
  602.  
  603.     Private Sub rtbCode_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rtbCode.SelectionChanged
  604.         If rtbCode.IsBusy Then
  605.             Exit Sub
  606.         End If
  607.  
  608.         Dim LineIndex As Int32 = rtbCode.CurrentLineIndex
  609.         Dim ColIndex As Int32 = rtbCode.SelectionStart - rtbCode.GetFirstCharIndexOfCurrentLine
  610.  
  611.         lblPosition.Text = "Posizione cursore:  " & (LineIndex + 1) & ", " & (ColIndex + 1)
  612.  
  613.         If (rtbCode.SelectionStart <= 0) Or (lstSuggest.Visible = False) Then
  614.             lstSuggest.Visible = False
  615.             Exit Sub
  616.         End If
  617.  
  618.         Dim StartTag As Int32 = rtbCode.Text.LastIndexOf("<", rtbCode.SelectionStart)
  619.         Dim CloseTag As Int32 = rtbCode.Text.LastIndexOf(">", rtbCode.SelectionStart)
  620.  
  621.         If (StartTag < CloseTag) Or StartTag = -1 Then
  622.             lstSuggest.Visible = False
  623.         End If
  624.     End Sub
  625.  
  626.     Private Sub strSHEnabled_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strSHEnabled.CheckedChanged
  627.         If strSHEnabled.Checked Then
  628.             rtbCode.SyntaxHighlightingEnabled = True
  629.             rtbCode.ColorVisibleLines()
  630.         Else
  631.             rtbCode.SyntaxHighlightingEnabled = False
  632.             rtbCode.SelectAll()
  633.             rtbCode.SelectionColor = Color.Black
  634.             rtbCode.SelectionFont = rtbCode.Font
  635.             rtbCode.DeselectAll()
  636.         End If
  637.     End Sub
  638.  
  639.     Private Sub strSHOptions_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strSHOptions.Click
  640.         My.Forms.SHOption.ShowDialog()
  641.         If My.Forms.SHOption.ColorAll Then
  642.             strHighlightAll_Click(Me, EventArgs.Empty)
  643.         Else
  644.             rtbCode.ColorVisibleLines()
  645.         End If
  646.     End Sub
  647.  
  648.     Private Sub strHighlightAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strHighlightAll.Click
  649.         lblStatus.Text = "Colorazione in corso..."
  650.         Application.DoEvents()
  651.         rtbCode.ColorRtb()
  652.         Application.DoEvents()
  653.         lblStatus.Text = "Colorazione completata!"
  654.     End Sub
  655.  
  656.     Private Sub strCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strCopy.Click
  657.         rtbCode.Copy()
  658.     End Sub
  659.  
  660.     Private Sub strPaste_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strPaste.Click
  661.         rtbCode.Paste()
  662.     End Sub
  663.  
  664.     Private Sub strCut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strCut.Click
  665.         rtbCode.Cut()
  666.     End Sub
  667.  
  668.     Private Sub strSelectAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strSelectAll.Click
  669.         rtbCode.SelectAll()
  670.     End Sub
  671.  
  672.     Private Sub strTemplatesChild_Click(ByVal sender As Object, ByVal e As EventArgs)
  673.         Try
  674.             Dim Code As String = IO.File.ReadAllText(Application.StartupPath & "\Templates\" & DirectCast(sender, ToolStripMenuItem).Text & ".hist")
  675.             rtbCode.SelectedText = Code
  676.             rtbCode.ColorVisibleLines()
  677.         Catch Ex As Exception
  678.             MessageBox.Show("Il template con questo nome è stato cancellato durante l'esecuzione del programma!", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  679.         End Try
  680.     End Sub
  681.  
  682.     Private Sub strAddTemplate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strAddTemplate.Click
  683.         Dim T As New CreateTemplateDialog
  684.         If T.ShowDialog = Windows.Forms.DialogResult.OK Then
  685.             RefreshTemplates()
  686.         End If
  687.     End Sub
  688. End Class