Imports System.IO
Imports System.Windows.Forms.VisualStyles.VisualStyleElement

Public Class Form2

    Public DatotekaSpemembe As Boolean = False
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Activated
        ' PropertyGrid1.SelectedObject = My.Settings
        ' ' Attribute for the user-scope settings.
        ' Dim userAttr As New System.Configuration.UserScopedSettingAttribute
        ' Dim attrs As New System.ComponentModel.AttributeCollection(userAttr)
        ' PropertyGrid1.BrowsableAttributes = attrs
        Form1.Hide()
        RichTextBoxEdit.Clear()

        ' prepare StreamReader
        Dim streamReader As StreamReader = New StreamReader(My.Settings.ZadnjaMapa)
        Dim i As Integer = 0

        ' loop untill end of stream
        Do Until streamReader.EndOfStream
            ' Beri vrstico po vrstico
            Dim line As String = streamReader.ReadLine()
            RichTextBoxEdit.AppendText(line + vbCrLf)
            i = i + 1
        Loop
        ToolStripStatusLabel1.Text = "Prebrano: " + i.ToString + " | Datoteka: " + My.Settings.ZadnjaMapa
        DatotekaSpemembe = False
        BtnShraniF2.Enabled = False
        streamReader.Close()
        streamReader.Dispose()
    End Sub

    Private Sub Form2_Close(sender As Object, e As EventArgs) Handles MyBase.Closing
        ' If DatotekaSpemembe = True Then
        '     MessageBox.Show("Shranim spremembe?", "Podatki spremenjeni", MessageBoxButtons.YesNo)
        '     If MessageBoxButtons.YesNo = vbYes Then
        '         RichTextBoxEdit.SaveFile(My.Settings.ZadnjaMapa, RichTextBoxStreamType.PlainText)
        '         BtnShraniF2.Enabled = False
        '         DatotekaSpemembe = False
        '     End If
        ' End If
        Form1.RichTextBox1.Clear()
        Form1.streamLines = 0
        Dim streamReader As StreamReader = New StreamReader(Form1.OpenFileDialog1.FileName)
        Do Until streamReader.EndOfStream
            Form1.RichTextBox1.AppendText(streamReader.ReadLine + vbCrLf)
            Form1.streamLines = Form1.streamLines + 1
        Loop
        Form1.ToolStripProgressBar1.Maximum = Form1.streamLines
        streamReader.Close()
        streamReader.Dispose()

        Form1.Show()
    End Sub

    Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBoxEdit.TextChanged
        ToolStripStatusLabel1.Text = "Spremenjeni podatki... datoteka je bila urejana."
        BtnShraniF2.Enabled = True
        DatotekaSpemembe = True
    End Sub

    Private Sub BtnShraniF2_Click(sender As Object, e As EventArgs) Handles BtnShraniF2.Click
        ' SaveFileDialog2.FileName = My.Settings.ZadnjaMapa
        ' If SaveFileDialog2.ShowDialog = DialogResult.OK Then
        RichTextBoxEdit.SaveFile(My.Settings.ZadnjaMapa, RichTextBoxStreamType.PlainText)
        ToolStripStatusLabel1.Text = "Shranjeno: " + SaveFileDialog2.FileName.ToString
        BtnShraniF2.Enabled = False
        DatotekaSpemembe = False
        ' End If
    End Sub

    Private Sub ToolStripButton2_Click(sender As Object, e As EventArgs) Handles ToolStripButton2.Click
        RichTextBoxEdit.Focus()
        RichTextBoxEdit.SelectionStart = RichTextBoxEdit.GetFirstCharIndexOfCurrentLine
        SendKeys.Send("+{HOME}")
        If ComBoxKoda.SelectedIndex = 0 Then
            RichTextBoxEdit.SelectedText = "*410000+0000000000000010 " + vbCrLf
            BtnShraniF2.Enabled = True
            DatotekaSpemembe = True
        ElseIf ComBoxKoda.SelectedIndex = 1 Then
            RichTextBoxEdit.SelectedText = "*410000+0000000000000020 " + vbCrLf
            BtnShraniF2.Enabled = True
            DatotekaSpemembe = True
        ElseIf ComBoxKoda.SelectedIndex = 2 Then
            RichTextBoxEdit.SelectedText = "*410000+0000000000000030 " + vbCrLf
            BtnShraniF2.Enabled = True
            DatotekaSpemembe = True
        ElseIf ComBoxKoda.SelectedIndex = 3 Then
            RichTextBoxEdit.SelectedText = "*410000+0000000000000040 " + vbCrLf
            BtnShraniF2.Enabled = True
            DatotekaSpemembe = True
        ElseIf ComBoxKoda.SelectedIndex = 4 Then
            RichTextBoxEdit.SelectedText = "*410000+0000000000000050 " + vbCrLf
            BtnShraniF2.Enabled = True
            DatotekaSpemembe = True
        ElseIf ComBoxKoda.SelectedIndex = 5 Then
            RichTextBoxEdit.SelectedText = "*410000+0000000000000090 " + vbCrLf
            BtnShraniF2.Enabled = True
            DatotekaSpemembe = True
        ElseIf ComBoxKoda.SelectedIndex = 6 Then
            RichTextBoxEdit.SelectedText = "*410000+0000000000000099 " + vbCrLf
            BtnShraniF2.Enabled = True
            DatotekaSpemembe = True
        End If

    End Sub

    Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
        RichTextBoxEdit.Undo()
    End Sub

    Private Sub ToolStripButton3_Click(sender As Object, e As EventArgs) Handles ToolStripButton3.Click
        Me.Close()
    End Sub
End Class