Public Class NetComExampleCommands
    Inherits System.Windows.Forms.Form
#Region "Private Members"
    Private mNetComClient As MNetCom.MNetComClient
#End Region

#Region "Constructor"
    Public Sub New()
        MyBase.New()

        'initialize class vars
        Me.mNetComClient = New MNetCom.MNetComClient


        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

        'extra UI setup
        Me.cmbCommandList.SelectedIndex = 0
        Me.btnDisconnect.Enabled = False
        Me.pnlCommands.Enabled = False

        'Set the logfile name
        If Not Me.mNetComClient.SetLogFileName(Application.StartupPath.ToString() & "\NetComExampleCommandsLogfile.txt") Then
            MessageBox.Show(Me, "Call to set the logfile name failed", "NetCom Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
        End If

    End Sub
#End Region

#Region " Windows Form Designer generated code "



    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents lblServerName As System.Windows.Forms.Label
    Friend WithEvents btnConnect As System.Windows.Forms.Button
    Friend WithEvents btnDisconnect As System.Windows.Forms.Button
    Friend WithEvents txtServerName As System.Windows.Forms.TextBox
    Friend WithEvents lblServerNameComment As System.Windows.Forms.Label
    Friend WithEvents pnlCommands As System.Windows.Forms.Panel
    Friend WithEvents lblCommandListLabel As System.Windows.Forms.Label
    Friend WithEvents btnSendCommand As System.Windows.Forms.Button
    Friend WithEvents lblReplyStringValue As System.Windows.Forms.Label
    Friend WithEvents lblReplyStringLabel As System.Windows.Forms.Label
    Friend WithEvents cmbCommandList As System.Windows.Forms.ComboBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.lblServerName = New System.Windows.Forms.Label
        Me.btnConnect = New System.Windows.Forms.Button
        Me.btnDisconnect = New System.Windows.Forms.Button
        Me.txtServerName = New System.Windows.Forms.TextBox
        Me.lblServerNameComment = New System.Windows.Forms.Label
        Me.pnlCommands = New System.Windows.Forms.Panel
        Me.lblCommandListLabel = New System.Windows.Forms.Label
        Me.btnSendCommand = New System.Windows.Forms.Button
        Me.cmbCommandList = New System.Windows.Forms.ComboBox
        Me.lblReplyStringLabel = New System.Windows.Forms.Label
        Me.lblReplyStringValue = New System.Windows.Forms.Label
        Me.pnlCommands.SuspendLayout()
        Me.SuspendLayout()
        '
        'lblServerName
        '
        Me.lblServerName.Location = New System.Drawing.Point(16, 11)
        Me.lblServerName.Name = "lblServerName"
        Me.lblServerName.Size = New System.Drawing.Size(72, 16)
        Me.lblServerName.TabIndex = 0
        Me.lblServerName.Text = "Server Name"
        '
        'btnConnect
        '
        Me.btnConnect.Location = New System.Drawing.Point(344, 8)
        Me.btnConnect.Name = "btnConnect"
        Me.btnConnect.Size = New System.Drawing.Size(75, 23)
        Me.btnConnect.TabIndex = 1
        Me.btnConnect.Text = "Connect"
        '
        'btnDisconnect
        '
        Me.btnDisconnect.Location = New System.Drawing.Point(432, 8)
        Me.btnDisconnect.Name = "btnDisconnect"
        Me.btnDisconnect.Size = New System.Drawing.Size(75, 23)
        Me.btnDisconnect.TabIndex = 2
        Me.btnDisconnect.Text = "Disconnect"
        '
        'txtServerName
        '
        Me.txtServerName.Location = New System.Drawing.Point(96, 9)
        Me.txtServerName.Name = "txtServerName"
        Me.txtServerName.Size = New System.Drawing.Size(232, 20)
        Me.txtServerName.TabIndex = 3
        '
        'lblServerNameComment
        '
        Me.lblServerNameComment.Location = New System.Drawing.Point(108, 32)
        Me.lblServerNameComment.Name = "lblServerNameComment"
        Me.lblServerNameComment.Size = New System.Drawing.Size(208, 16)
        Me.lblServerNameComment.TabIndex = 5
        Me.lblServerNameComment.Text = "(User may enter pc name or IP address)"
        '
        'pnlCommands
        '
        Me.pnlCommands.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.pnlCommands.Controls.Add(Me.lblReplyStringValue)
        Me.pnlCommands.Controls.Add(Me.lblReplyStringLabel)
        Me.pnlCommands.Controls.Add(Me.lblCommandListLabel)
        Me.pnlCommands.Controls.Add(Me.btnSendCommand)
        Me.pnlCommands.Controls.Add(Me.cmbCommandList)
        Me.pnlCommands.Location = New System.Drawing.Point(16, 57)
        Me.pnlCommands.Name = "pnlCommands"
        Me.pnlCommands.Size = New System.Drawing.Size(488, 70)
        Me.pnlCommands.TabIndex = 6
        '
        'lblCommandListLabel
        '
        Me.lblCommandListLabel.Location = New System.Drawing.Point(13, 17)
        Me.lblCommandListLabel.Name = "lblCommandListLabel"
        Me.lblCommandListLabel.Size = New System.Drawing.Size(77, 20)
        Me.lblCommandListLabel.TabIndex = 24
        Me.lblCommandListLabel.Text = "Command List"
        Me.lblCommandListLabel.TextAlign = System.Drawing.ContentAlignment.MiddleRight
        '
        'btnSendCommand
        '
        Me.btnSendCommand.Location = New System.Drawing.Point(392, 17)
        Me.btnSendCommand.Name = "btnSendCommand"
        Me.btnSendCommand.Size = New System.Drawing.Size(96, 23)
        Me.btnSendCommand.TabIndex = 23
        Me.btnSendCommand.Text = "Send Command"
        '
        'cmbCommandList
        '
        Me.cmbCommandList.Items.AddRange(New Object() {"-PostEvent ""Test Event"" 0 0", "-StartAcquisition"})
        Me.cmbCommandList.Location = New System.Drawing.Point(96, 17)
        Me.cmbCommandList.Name = "cmbCommandList"
        Me.cmbCommandList.Size = New System.Drawing.Size(288, 21)
        Me.cmbCommandList.TabIndex = 22
        '
        'lblReplyStringLabel
        '
        Me.lblReplyStringLabel.AutoSize = True
        Me.lblReplyStringLabel.Location = New System.Drawing.Point(26, 47)
        Me.lblReplyStringLabel.Name = "lblReplyStringLabel"
        Me.lblReplyStringLabel.Size = New System.Drawing.Size(64, 13)
        Me.lblReplyStringLabel.TabIndex = 25
        Me.lblReplyStringLabel.Text = "Reply String"
        '
        'lblReplyStringValue
        '
        Me.lblReplyStringValue.AutoSize = True
        Me.lblReplyStringValue.Location = New System.Drawing.Point(95, 47)
        Me.lblReplyStringValue.Name = "lblReplyStringValue"
        Me.lblReplyStringValue.Size = New System.Drawing.Size(0, 13)
        Me.lblReplyStringValue.TabIndex = 26
        Me.lblReplyStringValue.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'NetComExampleCommands
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(520, 139)
        Me.Controls.Add(Me.pnlCommands)
        Me.Controls.Add(Me.lblServerNameComment)
        Me.Controls.Add(Me.txtServerName)
        Me.Controls.Add(Me.btnDisconnect)
        Me.Controls.Add(Me.btnConnect)
        Me.Controls.Add(Me.lblServerName)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.Name = "NetComExampleCommands"
        Me.Text = "NetCom Example - Commands"
        Me.pnlCommands.ResumeLayout(False)
        Me.pnlCommands.PerformLayout()
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

#End Region


#Region "UI Event Handlers"

    '****************************************************************************************************************
    '****************************************************************************************************************
    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        If Not Me.mNetComClient.AreWeConnected Then
            If Me.mNetComClient.ConnectToServer(Me.txtServerName.Text) Then
                Me.mNetComClient.SetApplicationName("NetCom Commands Example")

                'change ui to show connected status
                Me.txtServerName.Enabled = False
                Me.btnConnect.Enabled = False
                Me.btnDisconnect.Enabled = True
                Me.pnlCommands.Enabled = True

            Else
                MessageBox.Show(Me, "Connection to server failed", "NetCom Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
            End If
        End If
    End Sub

    '****************************************************************************************************************
    '****************************************************************************************************************
    Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
        If Me.mNetComClient.AreWeConnected Then
            If Me.mNetComClient.DisconnectFromServer Then
                'change ui to show disconnected status
                Me.txtServerName.Enabled = True
                Me.btnConnect.Enabled = True
                Me.btnDisconnect.Enabled = False
                Me.pnlCommands.Enabled = False
            Else
                MessageBox.Show(Me, "Disconnection from server failed", "NetCom Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
            End If
        End If
    End Sub

    '****************************************************************************************************************
    '****************************************************************************************************************
    Private Sub btnSendCommand_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendCommand.Click
        Dim reply As String = ""
        Dim cmd As String = Me.cmbCommandList.Text
        If Not mNetComClient.SendCommand(Me.cmbCommandList.Text, reply) Then
            MessageBox.Show(Me, "Send command to server failed", "NetCom Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
        Else
            Dim parsedReplyString As String() = Strings.Split(reply, " ")
            If (0 < parsedReplyString.GetLength(0)) Then
                If parsedReplyString(0) = "-1" Then
                    MessageBox.Show(Me, "Cheetah could not process your command.", "NetCom Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                End If

                Me.lblReplyStringValue.Text = reply

            End If

        End If
    End Sub

#End Region

End Class
