Imports System.ComponentModel
Imports DataLibrary.DataObjects

Public Class MainForm
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

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

        'Add any initialization after the InitializeComponent() call

    End Sub

    '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 labTotal As System.Windows.Forms.Label
    Friend WithEvents label4 As System.Windows.Forms.Label
    Friend WithEvents label2 As System.Windows.Forms.Label
    Friend WithEvents btnClose As System.Windows.Forms.Button
    Friend WithEvents btnRefresh As System.Windows.Forms.Button
    Friend WithEvents btnCommit As System.Windows.Forms.Button
    Friend WithEvents btnEdit As System.Windows.Forms.Button
    Friend WithEvents btnDelete As System.Windows.Forms.Button
    Friend WithEvents btnNew As System.Windows.Forms.Button
    Friend WithEvents label1 As System.Windows.Forms.Label
    Friend WithEvents viewProducts As System.Windows.Forms.ListView
    Friend WithEvents columnHeader1 As System.Windows.Forms.ColumnHeader
    Friend WithEvents columnHeader2 As System.Windows.Forms.ColumnHeader
    Friend WithEvents columnHeader3 As System.Windows.Forms.ColumnHeader
    Friend WithEvents imageList1 As System.Windows.Forms.ImageList
    Friend WithEvents C1DataSet1 As C1.Data.C1DataSet
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(MainForm))
        Me.labTotal = New System.Windows.Forms.Label()
        Me.label4 = New System.Windows.Forms.Label()
        Me.label2 = New System.Windows.Forms.Label()
        Me.btnClose = New System.Windows.Forms.Button()
        Me.btnRefresh = New System.Windows.Forms.Button()
        Me.btnCommit = New System.Windows.Forms.Button()
        Me.btnEdit = New System.Windows.Forms.Button()
        Me.btnDelete = New System.Windows.Forms.Button()
        Me.btnNew = New System.Windows.Forms.Button()
        Me.label1 = New System.Windows.Forms.Label()
        Me.viewProducts = New System.Windows.Forms.ListView()
        Me.columnHeader1 = New System.Windows.Forms.ColumnHeader()
        Me.columnHeader2 = New System.Windows.Forms.ColumnHeader()
        Me.columnHeader3 = New System.Windows.Forms.ColumnHeader()
        Me.imageList1 = New System.Windows.Forms.ImageList(Me.components)
        Me.C1DataSet1 = New C1.Data.C1DataSet()
        CType(Me.C1DataSet1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'labTotal
        '
        Me.labTotal.Location = New System.Drawing.Point(424, 12)
        Me.labTotal.Name = "labTotal"
        Me.labTotal.Size = New System.Drawing.Size(28, 16)
        Me.labTotal.TabIndex = 25
        Me.labTotal.Text = "0"
        Me.labTotal.TextAlign = System.Drawing.ContentAlignment.TopRight
        '
        'label4
        '
        Me.label4.Location = New System.Drawing.Point(384, 12)
        Me.label4.Name = "label4"
        Me.label4.Size = New System.Drawing.Size(40, 16)
        Me.label4.TabIndex = 24
        Me.label4.Text = "Total:"
        '
        'label2
        '
        Me.label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.label2.Location = New System.Drawing.Point(12, 285)
        Me.label2.Name = "label2"
        Me.label2.Size = New System.Drawing.Size(572, 2)
        Me.label2.TabIndex = 15
        '
        'btnClose
        '
        Me.btnClose.Location = New System.Drawing.Point(508, 299)
        Me.btnClose.Name = "btnClose"
        Me.btnClose.Size = New System.Drawing.Size(76, 23)
        Me.btnClose.TabIndex = 18
        Me.btnClose.Text = "Close"
        '
        'btnRefresh
        '
        Me.btnRefresh.Location = New System.Drawing.Point(468, 250)
        Me.btnRefresh.Name = "btnRefresh"
        Me.btnRefresh.Size = New System.Drawing.Size(116, 23)
        Me.btnRefresh.TabIndex = 23
        Me.btnRefresh.TabStop = False
        Me.btnRefresh.Text = "F3 - Refresh"
        '
        'btnCommit
        '
        Me.btnCommit.Location = New System.Drawing.Point(468, 218)
        Me.btnCommit.Name = "btnCommit"
        Me.btnCommit.Size = New System.Drawing.Size(116, 23)
        Me.btnCommit.TabIndex = 22
        Me.btnCommit.TabStop = False
        Me.btnCommit.Text = "F2 - Commit"
        '
        'btnEdit
        '
        Me.btnEdit.Location = New System.Drawing.Point(468, 94)
        Me.btnEdit.Name = "btnEdit"
        Me.btnEdit.Size = New System.Drawing.Size(116, 23)
        Me.btnEdit.TabIndex = 21
        Me.btnEdit.TabStop = False
        Me.btnEdit.Text = "Enter - Edit Product"
        '
        'btnDelete
        '
        Me.btnDelete.Location = New System.Drawing.Point(468, 62)
        Me.btnDelete.Name = "btnDelete"
        Me.btnDelete.Size = New System.Drawing.Size(116, 23)
        Me.btnDelete.TabIndex = 20
        Me.btnDelete.TabStop = False
        Me.btnDelete.Text = "Del - Delete Rows"
        '
        'btnNew
        '
        Me.btnNew.Location = New System.Drawing.Point(468, 32)
        Me.btnNew.Name = "btnNew"
        Me.btnNew.Size = New System.Drawing.Size(116, 23)
        Me.btnNew.TabIndex = 19
        Me.btnNew.TabStop = False
        Me.btnNew.Text = "Ins - New Product"
        '
        'label1
        '
        Me.label1.Font = New System.Drawing.Font("Tahoma", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.label1.Location = New System.Drawing.Point(12, 9)
        Me.label1.Name = "label1"
        Me.label1.Size = New System.Drawing.Size(68, 19)
        Me.label1.TabIndex = 13
        Me.label1.Text = "Products:"
        '
        'viewProducts
        '
        Me.viewProducts.AllowColumnReorder = True
        Me.viewProducts.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.viewProducts.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.columnHeader1, Me.columnHeader2, Me.columnHeader3})
        Me.viewProducts.FullRowSelect = True
        Me.viewProducts.GridLines = True
        Me.viewProducts.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable
        Me.viewProducts.HideSelection = False
        Me.viewProducts.Location = New System.Drawing.Point(12, 30)
        Me.viewProducts.Name = "viewProducts"
        Me.viewProducts.Size = New System.Drawing.Size(442, 244)
        Me.viewProducts.SmallImageList = Me.imageList1
        Me.viewProducts.Sorting = System.Windows.Forms.SortOrder.Ascending
        Me.viewProducts.TabIndex = 14
        Me.viewProducts.View = System.Windows.Forms.View.Details
        '
        'columnHeader1
        '
        Me.columnHeader1.Text = "Product Name"
        Me.columnHeader1.Width = 190
        '
        'columnHeader2
        '
        Me.columnHeader2.Text = "Supplier Company"
        Me.columnHeader2.Width = 162
        '
        'columnHeader3
        '
        Me.columnHeader3.Text = "Country"
        Me.columnHeader3.Width = 70
        '
        'imageList1
        '
        Me.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit
        Me.imageList1.ImageSize = New System.Drawing.Size(16, 16)
        Me.imageList1.ImageStream = CType(resources.GetObject("imageList1.ImageStream"), System.Windows.Forms.ImageListStreamer)
        Me.imageList1.TransparentColor = System.Drawing.Color.Transparent
        '
        'C1DataSet1
        '
        Me.C1DataSet1.BindingContextCtrl = Me
        Me.C1DataSet1.DataLibrary = "DataLibrary"
        Me.C1DataSet1.DataLibraryUrl = ""
        Me.C1DataSet1.DataSetDef = "DataSet1"
        Me.C1DataSet1.Locale = New System.Globalization.CultureInfo("en-US")
        Me.C1DataSet1.Name = "C1DataSet1"
        Me.C1DataSet1.SchemaClassName = "DataLibrary.DataClass"
        Me.C1DataSet1.SchemaDef = Nothing
        '
        'MainForm
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 14)
        Me.ClientSize = New System.Drawing.Size(596, 335)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.labTotal, Me.label4, Me.label2, Me.btnClose, Me.btnRefresh, Me.btnCommit, Me.btnEdit, Me.btnDelete, Me.btnNew, Me.label1, Me.viewProducts})
        Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.MaximizeBox = False
        Me.Name = "MainForm"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = " ComponentOne DataObjects - Using Objects To Access Data"
        CType(Me.C1DataSet1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RefreshProductList()
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Close()
    End Sub

    Private Sub MakeItemVisible(ByVal lvi As ListViewItem)
        viewProducts.SelectedItems.Clear()
        lvi.Focused = True
        lvi.Selected = True
        lvi.EnsureVisible()
    End Sub

    Private Sub UpdateTotalLabel()
        labTotal.Text = viewProducts.Items.Count.ToString()
    End Sub

    Private Sub AddProduct()
        Dim rowProduct As C1.Data.C1DataRow = Nothing
        Dim id As Integer = 0
        Dim frm As ProductForm = New ProductForm(" Add Product", C1DataSet1, imageList1)
        rowProduct = C1DataSet1.TableViews("Products").AddNew()
        frm.SetCurrentRow(rowProduct)
        Dim res As DialogResult = frm.ShowDialog()
        If rowProduct Is Nothing Then
            rowProduct = C1DataSet1.TableViews("Products").Rows.Find(id)
        End If
        If res = System.Windows.Forms.DialogResult.OK Then
            Dim lvi As ListViewItem
            Dim product As ProductsRow_tableView = ProductsRow_tableView.Obj(rowProduct)
            If Not product.IsCategoryIDNull() And product.CategoryID > 0 And _
             product.CategoryID <= imageList1.Images.Count Then
                lvi = New ListViewItem(product.ProductName, product.CategoryID - 1)
            Else
                lvi = New ListViewItem(product.ProductName)
            End If
            lvi.Tag = product.ProductID
            If Not product.IsSupplierIDNull() Then
                lvi.SubItems.Add(product.GetSuppliersRow().CompanyName)
                lvi.SubItems.Add(product.GetSuppliersRow().Country)
            Else
                lvi.SubItems.Add("")
                lvi.SubItems.Add("")
            End If
            viewProducts.Items.Add(lvi)
            MakeItemVisible(lvi)
            UpdateTotalLabel()
        ElseIf Not rowProduct Is Nothing Then
            rowProduct.Delete()
        End If
    End Sub

    Private Sub EditProduct()
        Dim lvi As ListViewItem = viewProducts.FocusedItem
        If Not lvi Is Nothing Then
            Dim rowProduct As C1.Data.C1DataRow = C1DataSet1. _
             TableViews("Products").Rows.Find(CType(lvi.Tag, Integer))
            Dim frm As ProductForm = New ProductForm(" Edit Product", C1DataSet1, imageList1)
            frm.SetCurrentRow(rowProduct)
            If frm.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                Dim product As ProductsRow_tableView = ProductsRow_tableView.Obj(rowProduct)
                lvi.Text = product.ProductName
                If Not product.IsCategoryIDNull() And product.CategoryID > 0 And _
                 product.CategoryID <= imageList1.Images.Count Then
                    lvi.ImageIndex = product.CategoryID - 1
                Else
                    lvi.ImageIndex = -1
                End If
                If Not product.IsSupplierIDNull() Then
                    lvi.SubItems(1).Text = product.GetSuppliersRow().CompanyName
                    lvi.SubItems(2).Text = product.GetSuppliersRow().Country
                Else
                    lvi.SubItems(1).Text = ""
                    lvi.SubItems(2).Text = ""
                End If
                viewProducts.Sort()
                MakeItemVisible(lvi)
            End If
        Else
            MessageBox.Show("There is no selected item.", "UsingObjects", _
             MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If
    End Sub

    Private Sub DeleteRows()
        If viewProducts.SelectedItems.Count > 0 Then
            If MessageBox.Show("Are you sure you want to delete the item(s)?", "Confirmation", _
             MessageBoxButtons.YesNo, MessageBoxIcon.Question) = System.Windows.Forms.DialogResult.Yes Then
                Dim lvi As ListViewItem
                For Each lvi In viewProducts.SelectedItems
                    C1DataSet1.TableViews("Products").Rows.Find(CType(lvi.Tag, Integer)).Delete()
                    lvi.Remove()
                Next
                If Not viewProducts.FocusedItem Is Nothing Then
                    viewProducts.FocusedItem.Selected = True
                End If
                UpdateTotalLabel()
            End If
        Else
            MessageBox.Show("There are no items selected.", "UsingObjects", _
             MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If
    End Sub

    Private Sub CommitChanges()
        C1DataSet1.Update()
        RefreshProductList()
    End Sub

    Private Sub RefreshProductList()
        C1DataSet1.Fill()
        Dim focusindex As Integer
        If Not viewProducts.FocusedItem Is Nothing Then
            focusindex = viewProducts.FocusedItem.Index
        Else
            focusindex = -1
        End If
        viewProducts.BeginUpdate()
        viewProducts.Items.Clear()
        Dim rows As C1.Data.C1DataRowCollection = C1DataSet1.TableViews("Products").Rows
        Dim lvi As ListViewItem
        Dim product As ProductsRow_tableView
        Dim i As Integer
        For i = 0 To rows.Count - 1
            product = ProductsRow_tableView.Obj(rows(i))
            If Not product.IsCategoryIDNull() And product.CategoryID > 0 And _
             product.CategoryID <= imageList1.Images.Count Then
                lvi = New ListViewItem(product.ProductName, product.CategoryID - 1)
            Else
                lvi = New ListViewItem(product.ProductName)
            End If
            lvi.Tag = product.ProductID
            If Not product.IsSupplierIDNull() Then
                lvi.SubItems.Add(product.GetSuppliersRow().CompanyName)
                lvi.SubItems.Add(product.GetSuppliersRow().Country)
            Else
                lvi.SubItems.Add("")
                lvi.SubItems.Add("")
            End If
            viewProducts.Items.Add(lvi)
        Next
        viewProducts.EndUpdate()
        If focusindex <> -1 And focusindex < viewProducts.Items.Count Then
            MakeItemVisible(viewProducts.Items(focusindex))
        End If
        UpdateTotalLabel()
    End Sub

    Private Sub viewProducts_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles viewProducts.DoubleClick
        EditProduct()
    End Sub

    Private Sub viewProducts_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles viewProducts.KeyDown
        Select Case e.KeyCode
            Case Keys.Return
                EditProduct()
            Case Keys.Insert
                AddProduct()
            Case Keys.Delete
                DeleteRows()
            Case Keys.F2
                CommitChanges()
            Case Keys.F3
                RefreshProductList()
            Case Else
                Return
        End Select
        e.Handled = True
    End Sub

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        viewProducts.Focus()
        AddProduct()
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        viewProducts.Focus()
        DeleteRows()
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        viewProducts.Focus()
        EditProduct()
    End Sub

    Private Sub btnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommit.Click
        viewProducts.Focus()
        CommitChanges()
    End Sub

    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
        viewProducts.Focus()
        RefreshProductList()
    End Sub
End Class
