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 - Form1.vb

Form1.vb

Caricato da: Totem
Scarica il programma completo

  1. Imports System.Text.RegularExpressions
  2. Public Class Form1
  3.     Private Tags As New List(Of String)
  4.     Private Attributes As New Dictionary(Of String, List(Of String))
  5.  
  6.     Private TextModified As Boolean = False
  7.     'Ultimo path di salvataggio del file
  8.     Private LastSavingPath As String
  9.     'Parola da cercare per la funzione Trova
  10.     Private WordToSeek As String
  11.     'Ultimo indice verificato per tale parola
  12.     Private LastValidIndex As Int32
  13.  
  14.     Private Sub AddItemsToList(ByVal List As List(Of String), ByVal StartString As String)
  15.         lstSuggest.Items.Clear()
  16.         For Each ATag As String In List
  17.             If ATag.ToLower.StartsWith(StartString.ToLower) Then
  18.                 lstSuggest.Items.Add(ATag)
  19.             End If
  20.         Next
  21.         lstSuggest.Tag = StartString
  22.     End Sub
  23.  
  24.     Private Sub ShowListAtCursor()
  25.         Dim P As Point = rtbCode.GetPositionFromCharIndex(rtbCode.SelectionStart)
  26.         lstSuggest.Location = New Point(P.X + 8, P.Y + 48)
  27.         lstSuggest.Visible = True
  28.     End Sub
  29.  
  30.     Private Sub AppendTextAtCursor(ByVal Str As String)
  31.         Dim Sel As Int32 = rtbCode.SelectionStart
  32.         rtbCode.Text = rtbCode.Text.Insert(rtbCode.SelectionStart, Str)
  33.         rtbCode.SelectionStart = Sel + Str.Length
  34.         lstSuggest.Tag = Str
  35.     End Sub
  36.  
  37.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  38.         My.Application.SaveMySettingsOnExit = True
  39.         AddHandler My.Application.UnhandledException, AddressOf ExceptionHandler
  40.  
  41.         If Not IO.File.Exists(Application.StartupPath & "\Tags.dat") Then
  42.             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)
  43.         Else
  44.             Dim Reader As New IO.StreamReader(Application.StartupPath & "\Tags.dat")
  45.             Dim Line, Data() As String
  46.  
  47.             While Not Reader.EndOfStream
  48.                 Line = Reader.ReadLine
  49.                 If String.IsNullOrEmpty(Line) Then
  50.                     Continue While
  51.                 End If
  52.                 Data = Line.Split("|")
  53.                 Tags.Add(Data(0))
  54.                 If Data.Length > 1 Then
  55.                     Dim Temp As New List(Of String)
  56.                     For I As Int16 = 1 To Data.Length - 1
  57.                         Temp.Add(Data(I))
  58.                     Next
  59.                     Attributes.Add(Data(0), Temp)
  60.                 End If
  61.             End While
  62.  
  63.             Reader.Close()
  64.         End If
  65.     End Sub
  66.  
  67.     Private Sub rtbCode_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rtbCode.TextChanged
  68.         TextModified = True
  69.         If rtbCode.SelectionStart > 0 And My.Settings.ISEnabled Then
  70.             Dim Start As Int32 = rtbCode.SelectionStart
  71.  
  72.             'Trova il < d'inizio.
  73.             Dim OpeningTag As Int32 = rtbCode.Text.LastIndexOf("<", Start - 1, Start)
  74.             'Se c'è, controlla che sia di questo tag
  75.             If OpeningTag = -1 Then
  76.                 Exit Sub
  77.             End If
  78.  
  79.             'Trova il primo > prima del cursore
  80.             Dim PrevEndingTag As Int32 = rtbCode.Text.LastIndexOf(">", Start - 1, Start)
  81.             'Se esiste, controlla che sia precedente al <
  82.             If (PrevEndingTag >= 0) AndAlso (PrevEndingTag > OpeningTag) Then
  83.                 'Il < trovato in realtà era di un altro tag già chiuso
  84.                 Exit Sub
  85.             End If
  86.  
  87.             'Controlla se il tag è chiuso
  88.             Dim EndingTag As Int32 = rtbCode.Text.IndexOf(">", Start)
  89.             Dim SuccOpeningTag As Int32 = rtbCode.Text.IndexOf("<", Start)
  90.             Dim IsClosed As Boolean = (EndingTag >= 0) And (EndingTag < SuccOpeningTag)
  91.  
  92.             'Localizza il tag
  93.             Dim Tag As String
  94.  
  95.             If Not IsClosed Then
  96.                 If SuccOpeningTag >= 0 Then
  97.                     'Situazione:
  98.                     '<a href="ciao" <div> ...
  99.                     Tag = rtbCode.Text.Substring(OpeningTag, SuccOpeningTag - OpeningTag + 1)
  100.                 Else
  101.                     'Situazione:
  102.                     '<a href="ciao" [Fine file]
  103.                     Tag = rtbCode.Text.Substring(OpeningTag)
  104.                 End If
  105.             Else
  106.                 'Situazione
  107.                 '<a href="ciao"> ...
  108.                 Tag = rtbCode.Text.Substring(OpeningTag, EndingTag - OpeningTag + 1)
  109.             End If
  110.  
  111.             lblStatus.Text = Tag
  112.  
  113.             If Tag.Length < 2 Then
  114.                 'E' presente solo <: mostra tutti i tag disponibili
  115.                 Me.AddItemsToList(Tags, "")
  116.                 Me.ShowListAtCursor()
  117.                 Exit Sub
  118.             End If
  119.  
  120.             'La porzione di stringa compresa tra l'inizio del tag e la posizione corrente
  121.             Dim Str As String = rtbCode.Text.Substring(OpeningTag, Start - OpeningTag)
  122.  
  123.             If Not Str.Contains(" ") Then
  124.                 'Se Str non contiene spazi, è una sola parola. Quindi del tipo:
  125.                 '<abcdefgh oppure </abcdef
  126.                 'e perciò è l'inizio di un tag. Ora suggerisce una lista di tutti i
  127.                 'tag che iniziano in quel modo
  128.  
  129.                 'Si tratta di un tag di chiusura: cancella il </ iniziale
  130.                 If Str.StartsWith("</") Then
  131.                     Str = Str.Remove(0, 2)
  132.                 Else
  133.                     'Cancella il < iniziale
  134.                     Str = Str.Remove(0, 1)
  135.                 End If
  136.  
  137.                 'Str è il nome parziale del tag
  138.                 Me.AddItemsToList(Tags, Str)
  139.                 If lstSuggest.Items.Count > 0 Then
  140.                     Me.ShowListAtCursor()
  141.                 End If
  142.             Else
  143.                 'Riduce il controllo al solo tag in modo che sia più facile
  144.                 'da analizzare
  145.                 Dim TagCursor As Int32 = Start - OpeningTag - 1
  146.                 Dim QuoteOpened As Boolean = False
  147.  
  148.                 For I As Int32 = 0 To Tag.Length - 1
  149.                     If Tag(I) = Chr(34) Then
  150.                         QuoteOpened = Not QuoteOpened
  151.                         Tag = Tag.Insert(I, "|")
  152.                         Tag = Tag.Remove(I + 1, 1)
  153.                     End If
  154.                     If QuoteOpened Then
  155.                         Tag = Tag.Insert(I, "|")
  156.                         Tag = Tag.Remove(I + 1, 1)
  157.                     End If
  158.                 Next
  159.  
  160.                 'Quindi, se all'indice corrente c'è un pipe, significa che il cursore
  161.                 'è dentro una stringa
  162.                 If Tag(TagCursor) = "|" Then
  163.                     'Nasconde la lista
  164.                     lstSuggest.Visible = False
  165.                 Else
  166.                     'Ottiene la parola che si sta scrivendo
  167.                     Dim PrevSpace As Int32 = Tag.LastIndexOf(" ", TagCursor, TagCursor + 1)
  168.                     Dim TagName, PartialAttrName As String
  169.  
  170.                     'Trova il nome del tag, togliendo il < iniziale e lo spazio finale
  171.                     TagName = Tag.Substring(0, Tag.IndexOf(" ")).Remove(0, 1)
  172.                     PartialAttrName = Tag.Substring(PrevSpace + 1, TagCursor - PrevSpace)
  173.                     'Ottiene solo gli attributi che iniziano con la stringa data
  174.                     If Tags.Contains(TagName) AndAlso Attributes.ContainsKey(TagName) Then
  175.                         Me.AddItemsToList(Attributes(TagName), PartialAttrName)
  176.                         Me.ShowListAtCursor()
  177.                     Else
  178.                         lstSuggest.Visible = False
  179.                     End If
  180.                 End If
  181.             End If
  182.         End If
  183.     End Sub
  184.  
  185.     Private Sub rtbCode_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles rtbCode.KeyPress
  186.         If (e.KeyChar = ">" Or e.KeyChar = vbCr) And My.Settings.ISEnabled Then
  187.             lstSuggest.Visible = False
  188.             If e.KeyChar = ">" Then
  189.                 Dim OpeningTag As Int32 = rtbCode.Text.LastIndexOf("<", rtbCode.SelectionStart, rtbCode.SelectionStart + 1)
  190.                 Try
  191.                     Dim Str As String = rtbCode.Text.Substring(OpeningTag, rtbCode.SelectionStart - OpeningTag)
  192.                     Dim TagName As String
  193.                     If Not Str.Contains(" ") Then
  194.                         TagName = rtbCode.Text.Substring(OpeningTag + 1, rtbCode.SelectionStart - OpeningTag - 1)
  195.                     Else
  196.                         TagName = rtbCode.Text.Substring(OpeningTag + 1, rtbCode.Text.IndexOf(" ", OpeningTag) - OpeningTag - 1)
  197.                     End If
  198.                     Me.AppendTextAtCursor("></" & TagName & ">")
  199.                     e.Handled = True
  200.                 Catch Ex As Exception
  201.                 End Try
  202.             End If
  203.         End If
  204.     End Sub
  205.  
  206.     Private Sub lstSuggest_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstSuggest.DoubleClick
  207.         'Aggiunge la parte di parola mancante
  208.         If lstSuggest.SelectedIndex >= 0 Then
  209.             Try
  210.                 Dim Str As String = CStr(lstSuggest.SelectedItem).Remove(0, lstSuggest.Tag.ToString.Length)
  211.                 Me.AppendTextAtCursor(Str)
  212.             Catch Ex As Exception
  213.             End Try
  214.         End If
  215.     End Sub
  216.  
  217.     Private Sub rtbCode_PreviewKeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles rtbCode.PreviewKeyDown
  218.         If e.KeyCode = Keys.Space Then
  219.             If lstSuggest.Visible And lstSuggest.Items.Count > 0 Then
  220.                 Dim Str As String = CStr(lstSuggest.Items(0)).Remove(0, lstSuggest.Tag.ToString.Length)
  221.                 Me.AppendTextAtCursor(Str)
  222.             End If
  223.         ElseIf e.KeyCode = Keys.Down Then
  224.             If lstSuggest.Visible Then
  225.                 lstSuggest.SelectedIndex = 0
  226.                 lstSuggest.Focus()
  227.             End If
  228.         End If
  229.     End Sub
  230.  
  231.     Private Sub strOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strOpen.Click
  232.         If TextModified Then
  233.             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
  234.                 strSave_Click(Me, EventArgs.Empty)
  235.                 TextModified = False
  236.             End If
  237.         End If
  238.  
  239.         Dim Open As New OpenFileDialog
  240.         Open.Filter = "Pagine web|*.htm;*.html;*.php"
  241.         If Open.ShowDialog = Windows.Forms.DialogResult.OK Then
  242.             rtbCode.Text = IO.File.ReadAllText(Open.FileName)
  243.         End If
  244.     End Sub
  245.  
  246.     Private Sub strSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strSave.Click
  247.         If String.IsNullOrEmpty(LastSavingPath) Then
  248.             Dim Save As New SaveFileDialog
  249.             Save.Filter = "Pagine web|*.htm;*.html|Pagina PHP|*.php"
  250.             If Save.ShowDialog = Windows.Forms.DialogResult.OK Then
  251.                 IO.File.WriteAllText(Save.FileName, rtbCode.Text)
  252.                 LastSavingPath = Save.FileName
  253.                 lblStatus.Text = "File salvato con successo"
  254.                 TextModified = False
  255.             End If
  256.         Else
  257.             IO.File.WriteAllText(LastSavingPath, rtbCode.Text)
  258.             lblStatus.Text = "File salvato con successo"
  259.             TextModified = False
  260.         End If
  261.     End Sub
  262.  
  263.     Private Sub strSaveAs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strSaveAs.Click
  264.         'Annulla la stringa, e richiama l'altra funzione, risparmiando spazio
  265.         LastSavingPath = Nothing
  266.         strSave_Click(Me, EventArgs.Empty)
  267.     End Sub
  268.  
  269.     Private Sub strExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strExit.Click
  270.         If TextModified Then
  271.             If MessageBox.Show("Salvare le modifiche apportate al sorgente prima di uscire?", Me.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
  272.                 strSave_Click(Me, EventArgs.Empty)
  273.             End If
  274.         End If
  275.         Me.Close()
  276.     End Sub
  277.  
  278.     Private Sub strFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strFind.Click
  279.         Dim Word As String = InputBox("Immettere la stringa da cercare:", Me.Text)
  280.  
  281.         If String.IsNullOrEmpty(Word) Then
  282.             Exit Sub
  283.         End If
  284.  
  285.         If Word.Length < 2 Then
  286.             MessageBox.Show("Inserire una stringa di almeno due caratteri!", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  287.             Exit Sub
  288.         End If
  289.  
  290.         Dim Index As Int32
  291.         Index = rtbCode.Find(Word)
  292.         If Index > -1 Then
  293.             WordToSeek = Word
  294.             LastValidIndex = Index
  295.             rtbCode.Select(Index, Word.Length)
  296.             rtbCode.ScrollToCaret()
  297.             lblStatus.Text = "Trovata istanza di """ & Word & """ alla posizione " & Index
  298.         End If
  299.     End Sub
  300.  
  301.     Private Sub strFindNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strFindNext.Click
  302.         If Not String.IsNullOrEmpty(WordToSeek) Then
  303.             Dim Index As Int32
  304.  
  305.             If LastValidIndex < rtbCode.TextLength - 1 Then
  306.                 Index = rtbCode.Find(WordToSeek, LastValidIndex + 1, RichTextBoxFinds.None)
  307.                 If Index > -1 Then
  308.                     LastValidIndex = Index
  309.                     rtbCode.Select(Index, WordToSeek.Length)
  310.                     rtbCode.ScrollToCaret()
  311.                     lblStatus.Text = "Trovata istanza di """ & WordToSeek & """ alla posizione " & Index
  312.                 Else
  313.                     MessageBox.Show("Nessun'altra occorenza di """ & WordToSeek & """ è stata trovata!", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  314.                 End If
  315.             Else
  316.                 MessageBox.Show("Nessun'altra occorenza di """ & WordToSeek & """ è stata trovata!", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  317.             End If
  318.         End If
  319.     End Sub
  320.  
  321.     Private Sub strReplace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strReplace.Click
  322.         Dim Word As String = InputBox("Inserire la stringa da sotituire:", Me.Text)
  323.         Dim Replace As String = InputBox("Inserire la stringa sostituto:", Me.Text)
  324.  
  325.         If (Not String.IsNullOrEmpty(Word)) And (Not String.IsNullOrEmpty(Replace)) Then
  326.             lblStatus.Text = "Attendere, sostituzione in corso..."
  327.             rtbCode.Enabled = False
  328.             Application.DoEvents()
  329.             rtbCode.Text = rtbCode.Text.Replace(Word, Replace)
  330.             Application.DoEvents()
  331.             rtbCode.Enabled = True
  332.             lblStatus.Text = "Sostituzione effettuata"
  333.         End If
  334.     End Sub
  335.  
  336.     Private Sub strFont_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strFont.Click
  337.         Dim Font As New FontDialog
  338.         Font.AllowSimulations = True
  339.         If Font.ShowDialog = Windows.Forms.DialogResult.OK Then
  340.             rtbCode.Font = Font.Font
  341.             My.Settings.TextFont = Font.Font
  342.         End If
  343.     End Sub
  344.  
  345.     Private Sub strEnabled_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strEnabled.Click
  346.         My.Settings.ISEnabled = strEnabled.Checked
  347.         If strEnabled.Checked Then
  348.             strEnabled.Text = "Attivato"
  349.         Else
  350.             strEnabled.Text = "Disattivato"
  351.         End If
  352.     End Sub
  353.  
  354.     Private Sub strAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strAbout.Click
  355.         My.Forms.The_Lair_AboutBox1.Show()
  356.     End Sub
  357.  
  358.     Private Sub ExceptionHandler(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs)
  359.         MessageBox.Show(String.Format( _
  360.         "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.", _
  361.         e.Exception.GetType.FullName, vbCrLf, e.Exception.Message), Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
  362.     End Sub
  363. End Class