Imports System.Drawing.Printing

Public Class SampleDlg

    Private Delegate Sub AddMessage(ByVal strMsg As String)
    Private m_DLLFunc As DllFunc


    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        m_DLLFunc = New DllFunc()
        AddHandler m_DLLFunc.myCallbackEvent, AddressOf AddMsgCBStatus

        CreatePrinterList()

    End Sub

    Public Sub CloseSampleDlg()

        m_DLLFunc.CloseSamp()
    End Sub

    Private Sub btnDirctIOEx_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDirctIOEx.Click

        Dim msg As String
        msg = m_DLLFunc.DirectIOExSamp()
        txtDisp.Text = msg
    End Sub

    Private Sub btnGetPrnCapability_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetPrnCapability.Click

        Dim msg As String
        msg = m_DLLFunc.GetPrnCapabilitySamp()
        txtDisp.Text = msg
    End Sub

    Private Sub btnGetCounter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetCounter.Click

        Dim msg As String
        msg = m_DLLFunc.GetCounterSamp()
        txtDisp.Text = msg
    End Sub

    Private Sub chkCallbackStatus_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkCallbackStatus.CheckedChanged

        If chkCallbackStatus.Checked Then
            m_DLLFunc.CallbackSamp(True)
            lstCallbackStatus.Items.Clear()
            lstCallbackStatus.Enabled = True
        Else
            m_DLLFunc.CallbackSamp(False)
            lstCallbackStatus.Enabled = False
        End If

    End Sub

    Private Sub cboPrinter_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboPrinter.SelectedIndexChanged

        Dim printerName As String = ""

        chkCallbackStatus.Enabled = False
        btnGetPrnCapability.Enabled = False
        btnGetCounter.Enabled = False
        btnDirctIOEx.Enabled = False
        lstCallbackStatus.Items.Clear()
        chkCallbackStatus.Checked = False

        printerName = cboPrinter.SelectedItem.ToString()

        ' Open Printer
        If (m_DLLFunc.OpenSamp(printerName)) Then
            chkCallbackStatus.Enabled = True
            btnGetPrnCapability.Enabled = True
            btnGetCounter.Enabled = True
            btnDirctIOEx.Enabled = True
        End If

    End Sub

    Private Sub CreatePrinterList()

        Dim index As Integer = 0

        Do While index < PrinterSettings.InstalledPrinters.Count

            Dim printerName As String = PrinterSettings.InstalledPrinters(index).ToString()

            If printerName.StartsWith("SII") Then
                cboPrinter.Items.Add(printerName)
            End If

            index += 1
        Loop

        If (cboPrinter.Items.Count = 0) Then
            MessageBox.Show("Don't find the printer driver")
        Else
            cboPrinter.Enabled = True
            cboPrinter.Sorted = True
            cboPrinter.SelectedIndex = 0
        End If

    End Sub

    ' Display Message ( lstCallbackStatus )
    Private Sub AddMsgCBStatus(ByVal msg As String)

        If lstCallbackStatus.InvokeRequired Then
            Dim d As New AddMessage(AddressOf AddMsgCBStatus)
            Dim obj() As Object = {msg}
            Invoke(d, obj)
        Else
            lstCallbackStatus.Items.Insert(0, msg)
        End If
    End Sub

End Class
