Imports System.IO

Public Class Form1

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        Dim celyText As String = ""
        OpenFileDialog1.Filter = "Textové soubory (*.txt)|*.txt"
        'zobrazeni dialogu Otevrit
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            'otevreni souboru a zachyceni veskerych chyb pomoci zpracovatele chyb
            Try
                celyText = My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName)
                'aktualizace popisku
                PopisekProgramu.Text = OpenFileDialog1.FileName
                'zobrazeni souboru
                TextovePole1.Text = celyText
                'zobrazeni ukazatele v textovem poli
                TextovePole1.Enabled = True
                'povoleni prikazu Zavrit
                CloseToolStripMenuItem.Enabled = True
                'zakazani prikazu Otevrit
                OpenToolStripMenuItem.Enabled = False
            Catch ex As Exception
                MsgBox("V programu došlo k chybě." & vbCrLf & ex.Message)
            End Try
        End If
    End Sub

    Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
        'vyprazdneni textoveho pole
        TextovePole1.Text = ""
        PopisekProgramu.Text = "Načtení textového souboru pomocí příkazu Otveřít."
        'zakazani prikazu Zavrit
        CloseToolStripMenuItem.Enabled = False
        'povoleni prikazu Otevrit
        OpenToolStripMenuItem.Enabled = True
    End Sub

    Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
        SaveFileDialog1.Filter = "Textové soubory (*.txt)|*.txt"
        If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
            'ulozeni souboru na disk
            My.Computer.FileSystem.WriteAllText( _
                SaveFileDialog1.FileName, TextovePole1.Text, False)
        End If
    End Sub

    Private Sub InsertDateToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InsertDateToolStripMenuItem.Click
        TextovePole1.Text = My.Computer.Clock.LocalTime & vbCrLf & TextovePole1.Text
        'odstraneni vyberu textu
        TextovePole1.Select(0, 0)
    End Sub

    Private Sub SortTextToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SortTextToolStripMenuItem.Click

        Dim poleRazeni() As String
        Dim textovyRetezec As String
        Dim pocitadlo As Integer

        textovyRetezec = TextovePole1.Text
        'nahrazeni ruznych znaku prechodu na novy radek pouze jednim
        textovyRetezec = textovyRetezec.Replace(vbCrLf, vbCr)
        textovyRetezec = textovyRetezec.Replace(vbLf, vbCr)
        'odstraneni posledního znaku navratu voziku, pokud existuje
        If textovyRetezec.EndsWith(vbCr) Then
            textovyRetezec = textovyRetezec.Substring(0, textovyRetezec.Length - 1)
        End If

        'rozdeleni retezce na jednotlive podretezce a jejich ulozeni do pole
        poleRazeni = textovyRetezec.Split(vbCr)

        'razeni pole
        ShellSort(poleRazeni, poleRazeni.Length)

        'nasleduje zobrazeni serazeneho pole v textovem poli
        textovyRetezec = ""
        For pocitadlo = 0 To poleRazeni.Length - 1
            textovyRetezec = textovyRetezec & poleRazeni(pocitadlo) & vbCrLf
        Next pocitadlo
        TextovePole1.Text = textovyRetezec
        'odstraneni vyberu textu
        TextovePole1.Select(0, 0)
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        End
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
End Class
