
Public Class Form1
    Dim rnd As New Random
    Dim Card As New Mitul.MoneyCard

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With Me.mpbBar
            Me.btnBackground.BackColor = .BackColor
            Me.btnForground.BackColor = .ForeColor
            Me.btnBarStart.BackColor = .BarColor1
            Me.btnBarEnd.BackColor = .BarColor2
            Me.cmbGrdStyle.Text = .Gradient.ToString
            Me.cmbHatch.Text = .Hatch.ToString
            Me.nudGrdAngle.Value = .GradientAngle
            Me.chkAngle.Checked = .IsAngleScaleable
            Me.chkDisplayText.Checked = .DisplayText
            Me.chkPercentage.Checked = .ShowPercentage
            Me.chkWholebar.Checked = .GradientWholeBar
            Me.nudAlpha.Value = .Alpha
            .Text = "0:12:34.5"
            .Minimum = 0
            .Value = 0
        End With

        Me.cmbGrdStyle.Items.Clear()
        Me.cmbGrdStyle.Items.AddRange([Enum].GetNames(GetType(Mitul.Win.GradientEnum)))
        Me.cmbGrdStyle.Text = Me.mpbBar.Gradient.ToString
        Me.cmbHatch.Items.Clear()
        Me.cmbHatch.Items.AddRange([Enum].GetNames(GetType(Mitul.Win.HatchEnum)))
        Me.cmbHatch.Text = Me.mpbBar.Hatch.ToString
        If Me.mpbBar.Width > 5 Then Me.mpbBar.Maximum = Me.mpbBar.Width - 5

        Me.btnClear.PerformClick()

        Me.lstTimeServer.Items.Clear()
        Me.lstTimeServer.Items.AddRange(Mitul.SNTPClient.TimeServers)
        Me.lstTimeServer.SelectedIndex = 1
    End Sub

    Private Sub picBackground_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBackground.Click
        Me.dlgColor.Color = Me.mpbBar.BackColor
        If Me.dlgColor.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.btnBackground.BackColor = Me.dlgColor.Color
            Me.mpbBar.BackColor = Me.btnBackground.BackColor
        End If
    End Sub

    Private Sub picForground_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnForground.Click
        Me.dlgColor.Color = Me.mpbBar.ForeColor
        If Me.dlgColor.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.btnForground.BackColor = Me.dlgColor.Color
            Me.mpbBar.ForeColor = Me.btnForground.BackColor
        End If
    End Sub

    Private Sub picBarStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBarStart.Click
        Me.dlgColor.Color = Me.mpbBar.BarColor1
        If Me.dlgColor.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.btnBarStart.BackColor = Me.dlgColor.Color
            Me.mpbBar.BarColor1 = Me.btnBarStart.BackColor
        End If
    End Sub

    Private Sub picBarEnd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBarEnd.Click
        Me.dlgColor.Color = Me.mpbBar.BarColor2
        If Me.dlgColor.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.btnBarEnd.BackColor = Me.dlgColor.Color
            Me.mpbBar.BarColor2 = Me.btnBarEnd.BackColor
        End If
    End Sub

    Private Sub cmbGrdStyle_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbGrdStyle.SelectedIndexChanged
        Me.mpbBar.Gradient = CType([Enum].Parse(GetType(Mitul.Win.GradientEnum), Me.cmbGrdStyle.Text), Mitul.Win.GradientEnum)
    End Sub

    Private Sub nudGrdAngle_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudGrdAngle.ValueChanged, nudGrdAngle.Leave
        Me.mpbBar.GradientAngle = Me.nudGrdAngle.Value
    End Sub

    Private Sub nudAlpha_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudAlpha.ValueChanged, nudAlpha.Leave
        Me.mpbBar.Alpha = Me.nudAlpha.Value
    End Sub

    Private Sub cmbHatch_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbHatch.SelectedIndexChanged
        Me.mpbBar.Hatch = CType([Enum].Parse(GetType(Mitul.Win.HatchEnum), Me.cmbHatch.Text), Mitul.Win.HatchEnum)
    End Sub

    Private Sub chkWholebar_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkWholebar.CheckedChanged
        Me.mpbBar.GradientWholeBar = Me.chkWholebar.Checked
    End Sub

    Private Sub chkAngle_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkAngle.CheckedChanged
        Me.mpbBar.isAngleScaleable = Me.chkAngle.Checked
    End Sub

    Private Sub chkDisplayText_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkDisplayText.CheckedChanged
        Me.mpbBar.DisplayText = Me.chkDisplayText.Checked
    End Sub

    Private Sub chkPercentage_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkPercentage.CheckedChanged
        Me.mpbBar.ShowPercentage = Me.chkPercentage.Checked
    End Sub

    Private Sub btnFont_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFont.Click
        Me.dlgFont.Font = Me.mpbBar.Font
        Me.dlgFont.Color = Me.mpbBar.ForeColor
        If Me.dlgFont.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.mpbBar.Font = Me.dlgFont.Font
            Me.btnForground.BackColor = Me.dlgFont.Color
            Me.mpbBar.ForeColor = Me.dlgFont.Color
        End If
    End Sub

    Private Sub tmrDemo_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrDemo.Tick
        Dim ts As TimeSpan
        If Me.mpbBar.Value < Me.mpbBar.Maximum Then
            Me.mpbBar.PerformStep()
        Else
            Me.mpbBar.Value = Me.mpbBar.Minimum
        End If
        ts = New TimeSpan(0, 0, 0, Me.mpbBar.Value, Rnd.Next(0, 10) * 100)
        Me.mpbBar.Text = String.Format("{0:00}:{1:00}:{2:00}.{3:0}", ts.Hours, ts.Minutes, ts.Seconds, Math.Floor(ts.Milliseconds / 100))
    End Sub

    Private Sub chkContinuous_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkContinuous.CheckedChanged
        Me.lblScroll.Continuous = Me.chkContinuous.Checked
    End Sub

    Private Sub chkIsBlinking_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkIsBlinking.CheckedChanged
        Me.lblBlink.IsBlinking = Me.chkIsBlinking.Checked
    End Sub

    Private Sub chkBlinkImage_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkBlinkImage.CheckedChanged
        Me.lblBlink.BlinkImage = Me.chkBlinkImage.Checked
    End Sub

    Private Sub chkIsScrolling_CheckStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkIsScrolling.CheckStateChanged
        If Me.chkIsScrolling.CheckState = CheckState.Checked Then
            Me.lblScroll.IsScrolling = True
        ElseIf Me.chkIsScrolling.CheckState = CheckState.Indeterminate Then
            Me.lblScroll.Pause()
        ElseIf Me.chkIsScrolling.CheckState = CheckState.Unchecked Then
            Me.lblScroll.Stop()
        End If
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        Me.txtSmtpServer.ResetText()
        Me.txtTo.ResetText()
        Me.txtCC.ResetText()
        Me.txtBcc.ResetText()
        Me.lblEmail.ResetText()
        Me.lblDomain.ResetText()
        Me.txtMessage.ResetText()
        Me.txtSubject.ResetText()
        Me.lstAttachments.Items.Clear()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Me.dlgOpenFile.ShowDialog(Me)
        If Me.dlgOpenFile.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.lstAttachments.Items.AddRange(Me.dlgOpenFile.FileNames)
        End If
    End Sub

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        If Me.lstAttachments.SelectedIndex >= 0 Then
            Me.lstAttachments.Items.RemoveAt(Me.lstAttachments.SelectedIndex)
        End If
    End Sub

    'Email address is changed so let update the labels
    Private Sub txtTo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTo.TextChanged
        Dim rgx As System.Text.RegularExpressions.Match

        'Remember to ignore case.
        rgx = System.Text.RegularExpressions.Regex.Match(Me.txtTo.Text, Mitul.eMail.rgxEmail, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        If rgx.Success Then
            Me.lblEmail.Text = rgx.Groups(1).Value
            Me.lblDomain.Text = rgx.Groups(2).Value
        Else
            Me.lblEmail.ResetText()
            Me.lblDomain.ResetText()
        End If
    End Sub

    Private Sub btnQuickSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuickSend.Click
        Dim attachments As New List(Of String)
        Dim l_from_email As String

        'We are not doing any error cheking here, I would recommnd that you do that.

        'Let's see what we got
        For Each file As Object In Me.lstAttachments.Items
            attachments.Add(file.ToString)
        Next

        'We will send email using the same addess as it is going to.
        l_from_email = Me.txtTo.Text

        If attachments.Count > 0 Then
            Mitul.eMail.QuickSend(Me.txtSmtpServer.Text, Me.txtTo.Text, l_from_email, _
                    Me.txtSubject.Text, Me.txtMessage.Text, _
                    Me.txtCC.Text, Me.txtBcc.Text, attachments.ToArray)
            'You can also do this
            'Mitul.eMail.QuickSend("smtp.xyz.com", "qwer&gamil.com", "akjh@hotmail.com", "Subject", "Body", Nothing, Nothing, "C:\File1.txt", "E:\File2.doc", "D:\File3.jpg")
        Else
            Mitul.eMail.QuickSend(Me.txtSmtpServer.Text, Me.txtTo.Text, l_from_email, _
                    Me.txtSubject.Text, Me.txtMessage.Text, _
                    Me.txtCC.Text, Me.txtBcc.Text)
        End If

        'There are other overloads available too.
        ' - For example you can pass user name and password for you smtp server along with port number.
        ' - You can say body text is in html or not, specify encoding to use
        ' - You can also specify a reply to address
        ' - You can also set the priority
    End Sub

    Private Sub btnBulkSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBulkSend.Click
        ' It's same thing as quick send but send it to multiple people and see the differance.
        ' Also include CC and BCC and you will see the differance.

        Dim attachments As New List(Of String)
        Dim l_from_email As String

        'We are not doing any error cheking here, I would recommnd that you do that.

        'Let's see what we got
        For Each file As Object In Me.lstAttachments.Items
            attachments.Add(file.ToString)
        Next

        'We will send email using the same addess as it is going to.
        l_from_email = Me.txtTo.Text

        If attachments.Count > 0 Then
            Mitul.eMail.BulkSend(Me.txtSmtpServer.Text, Me.txtTo.Text, l_from_email, _
                    Me.txtSubject.Text, Me.txtMessage.Text, _
                    Me.txtCC.Text, Me.txtBcc.Text, attachments.ToArray)
            'You can also do this
            'Mitul.eMail.BulkSend("smtp.xyz.com", "qwer&gamil.com", "akjh@hotmail.com", "Subject", "Body", Nothing, Nothing, "C:\File1.txt", "E:\File2.doc", "D:\File3.jpg")
        Else
            Mitul.eMail.BulkSend(Me.txtSmtpServer.Text, Me.txtTo.Text, l_from_email, _
                    Me.txtSubject.Text, Me.txtMessage.Text, _
                    Me.txtCC.Text, Me.txtBcc.Text)
        End If

        'There are other overloads available too.
        ' - For example you can pass user name and password for you smtp server along with port number.
        ' - You can say body text is in html or not, specify encoding to use
        ' - You can also specify a reply to address
        ' - You can also set the priority
    End Sub

    Private Sub txtCard_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCard.TextChanged
        'We have defined MoneyCard object at the top 

        Card.CardNumber = Me.txtCard.Text
        lblValid.Text = IIf(Card.IsValid, "Valid", "NOT Valid").ToString
        Me.lblCardType.Text = Card.CardType
    End Sub

    Private Sub btnGetTime_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetTime.Click
        Dim sntp As New Mitul.SNTPClient(Me.lstTimeServer.Text)

        'We just want to get the time from the SNTP server and do not want to set the time of the computer. 
        ' We are using background worker to do this work because SNTP Clinet gets time in synchronize mode and 
        ' it does gets out of sync and may never return this way your application will not hang
        Me.btnGetTime.Enabled = False
        Me.bgwTimer.RunWorkerAsync(sntp)
    End Sub

    Private Sub bgwTimer_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwTimer.DoWork
        Dim sntp As Mitul.SNTPClient = DirectCast(e.Argument, Mitul.SNTPClient)
        sntp.Connect(False)
        e.Result = sntp
    End Sub

    Private Sub bgwTimer_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgwTimer.RunWorkerCompleted
        Dim sntp As Mitul.SNTPClient = DirectCast(e.Result, Mitul.SNTPClient)

        If Not sntp.IsResponseValid Then
            Me.txtTime.Text = "Not Valid Response"
        Else
            Me.txtTime.Text = sntp.ToString
        End If
        Me.btnGetTime.Enabled = True
    End Sub

    Private Sub chkRightToLeft_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkRightToLeft.CheckedChanged
        If Me.chkRightToLeft.Checked Then
            Me.lblScroll.RightToLeft = Windows.Forms.RightToLeft.Yes
        Else
            Me.lblScroll.RightToLeft = Windows.Forms.RightToLeft.No
        End If
    End Sub
End Class
