Imports System
Imports System.Collections.Generic
Imports System.Text
Imports DevExpress.XtraEditors
Imports System.Windows.Forms
Imports DevExpress.ExpressApp.Demos
Imports System.Drawing
Public Class ProgressForm
    Implements IProgressControl
    Public Const FormName As String = "ProgressForm"
    Private _form As XtraForm
    Private _progressbar As ProgressBarControl = New ProgressBarControl()
    Private _cancelbutton As SimpleButton = New SimpleButton()
    Private _label As Label = New Label()
    Private _longOperation As LongOperation
    Private _minimumProgressValue As Long
    Private _maximumProgressValue As Long
    Private _progressFormCaption As String
    Private Delegate Sub UpdateProgressFormDelegate(value As Long, message As String)
    Private Sub CreateProgressForm()
        _form = New XtraForm()
        _form.Name = FormName
        _form.Width = 350
        _form.Height = 125
        _form.StartPosition = FormStartPosition.CenterScreen
        _form.FormBorderStyle = FormBorderStyle.FixedDialog
        _form.MinimizeBox = False
        _form.MaximizeBox = False
        _form.ControlBox = False
        _form.ShowInTaskbar = False

        _label.Parent = _form
        _label.Location = New Point(10, 10)
        _label.Size = New Size(_form.ClientSize.Width - 20, 13)

        _progressbar.Parent = _form
        _progressbar.Location = New Point(10, 30)
        _progressbar.Name = "progressBar"
        _progressbar.Size = New Size(_form.ClientSize.Width - 20, 15)
        _progressbar.Properties.Minimum = _minimumProgressValue
        _progressbar.Properties.Maximum = _maximumProgressValue
        _progressbar.Properties.Step = 1

        _cancelbutton.Parent = _form
        _cancelbutton.DialogResult = DialogResult.Cancel
        _cancelbutton.Size = New Size(75, 23)
        _cancelbutton.Location = New Point((_form.Width - _cancelbutton.Width) / 2, 55)
        _cancelbutton.Text = "&Cancel"
        AddHandler _cancelbutton.Click, AddressOf cancelButton_Click
        AddHandler _cancelbutton.LostFocus, AddressOf DoOnFormLostFocus
        _form.CancelButton = _cancelbutton
        _form.Text = _progressFormCaption
    End Sub
    Public Sub ShowProgress(longOperation As DevExpress.ExpressApp.Demos.LongOperation) Implements IProgressControl.ShowProgress
        _form.Show()
        Me._longOperation = longOperation
        AddHandler Me._longOperation.ProgressChanged, AddressOf LongOperation_ProgressChanged
        AddHandler Me._longOperation.Completed, AddressOf LongOperation_Completed
    End Sub
    Private Sub DoOnFormLostFocus(sender As Object, e As EventArgs)
        _form.Focus()
    End Sub
    Private Sub LongOperation_Completed(sender As Object, e As LongOperationCompletedEventArgs)
        If _form IsNot Nothing Then
            _form.Invoke(New MethodInvoker(AddressOf _form.Close))
        End If
    End Sub
    Private Sub UpdateProgressForm(value As Long, message As String)
        _progressbar.EditValue = value
        _progressbar.Update()
        _label.Text = message
    End Sub
    Private Sub LongOperation_ProgressChanged(sender As Object, e As LongOperationProgressChangedEventArgs)
        Dim value As Long = e.ProgressPercentage
        If _form IsNot Nothing Then
            _form.Invoke(New UpdateProgressFormDelegate(AddressOf UpdateProgressForm), value, e.Message)
        End If
    End Sub
    Private Sub cancelButton_Click(sender As Object, e As EventArgs)
        _longOperation.CancelAsync()
    End Sub
    Public Sub ProgressForm(caption As String, minimum As Integer, maximum As Integer)
        _progressFormCaption = caption
        _minimumProgressValue = minimum
        _maximumProgressValue = maximum
        CreateProgressForm()
    End Sub
#Region "IDisposable Support"
    Private disposedValue As Boolean ' To detect redundant calls

    ' IDisposable
    Protected Overridable Sub Dispose(disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: dispose managed state (managed objects).
            End If

            _label = Nothing
            RemoveHandler _cancelbutton.LostFocus, AddressOf DoOnFormLostFocus
            RemoveHandler _longOperation.ProgressChanged, AddressOf LongOperation_ProgressChanged
            RemoveHandler _longOperation.Completed, AddressOf LongOperation_Completed
            _longOperation = Nothing
            If _form IsNot Nothing Then
                _form.Invoke(New MethodInvoker(AddressOf _form.Dispose))
                _form = Nothing
            End If
        End If
        Me.disposedValue = True
    End Sub

    ' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
    'Protected Overrides Sub Finalize()
    '    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
    '    Dispose(False)
    '    MyBase.Finalize()
    'End Sub

    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class
