Imports System.IO

Public Class Form_SENT_RESP_LOG

    Protected Overrides ReadOnly Property CreateParams() As CreateParams
        Get
            Dim cp As CreateParams = MyBase.CreateParams
            Const CS_DBLCLKS As Int32 = &H8
            Const CS_NOCLOSE As Int32 = &H200
            cp.ClassStyle = CS_DBLCLKS Or CS_NOCLOSE
            Return cp
        End Get
    End Property


    Private Sub Form_SENT_RESP_LOG_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' Make sure form is on the screen
        If Me.Location.X < 0 Or Me.Location.Y < 0 Then
            Me.Location = New Point(0, 0)
        End If

        ' Make sure window is not off of screen
        If Me.Left + Me.Width > Form1.ScreenWidth Then
            Me.Left = Form1.ScreenWidth - Me.Width - 100
        End If
        If Me.Top + Me.Height > Form1.ScreenHeight Then
            Me.Top = Form1.ScreenHeight - Me.Height - 100
        End If

        RichTextBox2.BorderStyle = BorderStyle.None
        RichTextBox2.Anchor = AnchorStyles.None
        RichTextBox2.Top = RichTextBox1.Top
        RichTextBox2.Left = RichTextBox1.Left
        RichTextBox2.Width = RichTextBox1.Width
        RichTextBox2.Height = RichTextBox1.Height
        RichTextBox2.Anchor = AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top

    End Sub

    Public Sub ResetLog()
        RichTextBox1.Text = ""
        Form1.LogScope(Now.Date & " AP Mini Control " & Form1.Version & " Command Log", Color.Black, "")
        RichTextBox2.Text = ""

    End Sub

    Private Sub RichTextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
        If RichTextBox1.Text.Length > 0 Then
            RichTextBox1.SelectedText = vbCrLf
        End If

    End Sub

    Private Sub RichTextBox2_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox2.TextChanged
        If RichTextBox2.Text.Length > 0 Then
            RichTextBox2.SelectedText = vbCrLf
        End If

    End Sub

    Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        Dim LogFile As String
        Dim LogMsg As String

        Dim DateTime As String = Now.Year & "-" _
            & Format(Now.Month, "00") & "-" & Format(Now.Day, "00") _
            & "_" & Format(Now.Hour, "00") & Format(Now.Minute, "00") & Format(Now.Second, "00")
        LogFile = "AP-Mini_Command_Log " & DateTime & ".rtf"
        LogFile = LogFile.Replace("/", "-")
        SaveFileDialog1.FileName = LogFile
        SaveFileDialog1.Filter = "RTF log file rtf|*.rtf;"
        If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            LogFile = SaveFileDialog1.FileName
            If ShowOnlyErrors.Checked = False Then
                File.WriteAllText(LogFile, RichTextBox1.Rtf)
                LogMsg = "Log saved to: "
            Else
                File.WriteAllText(LogFile, RichTextBox2.Rtf)
                LogMsg = "Errors Only Log saved to: "
            End If
            Form1.StatusLabel.Text = LogMsg & LogFile
        End If

    End Sub

    Private Sub CopyRTFToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyRTFToolStripMenuItem.Click
        Clipboard.Clear()
        If ShowOnlyErrors.Checked = False Then
            Clipboard.SetText(RichTextBox1.Rtf, TextDataFormat.Rtf)
        Else
            Clipboard.SetText(RichTextBox2.Rtf, TextDataFormat.Rtf)
        End If

    End Sub

    Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
        Clipboard.Clear()
        If ShowOnlyErrors.Checked = False Then
            Clipboard.SetText(RichTextBox1.Text)
        Else
            Clipboard.SetText(RichTextBox2.Text)
        End If

    End Sub

    Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
        Form1.LogW = Me.Width
        Form1.LogH = Me.Height
        Me.Visible = False

    End Sub

    Private Sub ClearToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearToolStripMenuItem.Click
        ResetLog()

    End Sub

    Private Sub ShowOnlyErrors_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ShowOnlyErrors.Click
        If ShowOnlyErrors.Checked = False Then
            ShowOnlyErrors.Checked = True
            RichTextBox1.Visible = False
            RichTextBox2.Visible = True
        Else
            ShowOnlyErrors.Checked = False
            RichTextBox2.Visible = False
            RichTextBox1.Visible = True
        End If

    End Sub

End Class