Imports System.ComponentModel
Imports DataLibrary.DataObjects

Public Class ProductForm
    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 label12 As System.Windows.Forms.Label
    Friend WithEvents label11 As System.Windows.Forms.Label
    Friend WithEvents labSupplierCountry As System.Windows.Forms.Label
    Friend WithEvents labSupplierCity As System.Windows.Forms.Label
    Friend WithEvents picCategory As System.Windows.Forms.PictureBox
    Friend WithEvents btnCancel As System.Windows.Forms.Button
    Friend WithEvents btnOk As System.Windows.Forms.Button
    Friend WithEvents label10 As System.Windows.Forms.Label
    Friend WithEvents edUnitsOnOrder As System.Windows.Forms.NumericUpDown
    Friend WithEvents label9 As System.Windows.Forms.Label
    Friend WithEvents edUnitsInStock As System.Windows.Forms.NumericUpDown
    Friend WithEvents label8 As System.Windows.Forms.Label
    Friend WithEvents edUnitPrice As System.Windows.Forms.TextBox
    Friend WithEvents label7 As System.Windows.Forms.Label
    Friend WithEvents edReorderLevel As System.Windows.Forms.NumericUpDown
    Friend WithEvents label6 As System.Windows.Forms.Label
    Friend WithEvents edQuantityPerUnit As System.Windows.Forms.TextBox
    Friend WithEvents label5 As System.Windows.Forms.Label
    Friend WithEvents chkDiscontinued As System.Windows.Forms.CheckBox
    Friend WithEvents cbSupplier As System.Windows.Forms.ComboBox
    Friend WithEvents label4 As System.Windows.Forms.Label
    Friend WithEvents edProductName As System.Windows.Forms.TextBox
    Friend WithEvents label3 As System.Windows.Forms.Label
    Friend WithEvents label2 As System.Windows.Forms.Label
    Friend WithEvents cbCategory As System.Windows.Forms.ComboBox
    Friend WithEvents label1 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.label12 = New System.Windows.Forms.Label()
        Me.label11 = New System.Windows.Forms.Label()
        Me.labSupplierCountry = New System.Windows.Forms.Label()
        Me.labSupplierCity = New System.Windows.Forms.Label()
        Me.picCategory = New System.Windows.Forms.PictureBox()
        Me.btnCancel = New System.Windows.Forms.Button()
        Me.btnOk = New System.Windows.Forms.Button()
        Me.label10 = New System.Windows.Forms.Label()
        Me.edUnitsOnOrder = New System.Windows.Forms.NumericUpDown()
        Me.label9 = New System.Windows.Forms.Label()
        Me.edUnitsInStock = New System.Windows.Forms.NumericUpDown()
        Me.label8 = New System.Windows.Forms.Label()
        Me.edUnitPrice = New System.Windows.Forms.TextBox()
        Me.label7 = New System.Windows.Forms.Label()
        Me.edReorderLevel = New System.Windows.Forms.NumericUpDown()
        Me.label6 = New System.Windows.Forms.Label()
        Me.edQuantityPerUnit = New System.Windows.Forms.TextBox()
        Me.label5 = New System.Windows.Forms.Label()
        Me.chkDiscontinued = New System.Windows.Forms.CheckBox()
        Me.cbSupplier = New System.Windows.Forms.ComboBox()
        Me.label4 = New System.Windows.Forms.Label()
        Me.edProductName = New System.Windows.Forms.TextBox()
        Me.label3 = New System.Windows.Forms.Label()
        Me.label2 = New System.Windows.Forms.Label()
        Me.cbCategory = New System.Windows.Forms.ComboBox()
        Me.label1 = New System.Windows.Forms.Label()
        CType(Me.edUnitsOnOrder, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.edUnitsInStock, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.edReorderLevel, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'label12
        '
        Me.label12.Location = New System.Drawing.Point(193, 88)
        Me.label12.Name = "label12"
        Me.label12.Size = New System.Drawing.Size(52, 16)
        Me.label12.TabIndex = 51
        Me.label12.Text = "Country:"
        '
        'label11
        '
        Me.label11.Location = New System.Drawing.Point(77, 88)
        Me.label11.Name = "label11"
        Me.label11.Size = New System.Drawing.Size(32, 16)
        Me.label11.TabIndex = 50
        Me.label11.Text = "City:"
        '
        'labSupplierCountry
        '
        Me.labSupplierCountry.Location = New System.Drawing.Point(245, 88)
        Me.labSupplierCountry.Name = "labSupplierCountry"
        Me.labSupplierCountry.Size = New System.Drawing.Size(84, 16)
        Me.labSupplierCountry.TabIndex = 49
        Me.labSupplierCountry.Text = "Country"
        '
        'labSupplierCity
        '
        Me.labSupplierCity.Location = New System.Drawing.Point(109, 88)
        Me.labSupplierCity.Name = "labSupplierCity"
        Me.labSupplierCity.Size = New System.Drawing.Size(80, 16)
        Me.labSupplierCity.TabIndex = 48
        Me.labSupplierCity.Text = "City"
        '
        'picCategory
        '
        Me.picCategory.Location = New System.Drawing.Point(345, 12)
        Me.picCategory.Name = "picCategory"
        Me.picCategory.Size = New System.Drawing.Size(16, 16)
        Me.picCategory.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage
        Me.picCategory.TabIndex = 47
        Me.picCategory.TabStop = False
        '
        'btnCancel
        '
        Me.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.btnCancel.Location = New System.Drawing.Point(193, 256)
        Me.btnCancel.Name = "btnCancel"
        Me.btnCancel.TabIndex = 46
        Me.btnCancel.Text = "Cancel"
        '
        'btnOk
        '
        Me.btnOk.Location = New System.Drawing.Point(109, 256)
        Me.btnOk.Name = "btnOk"
        Me.btnOk.TabIndex = 45
        Me.btnOk.Text = "OK"
        '
        'label10
        '
        Me.label10.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.label10.Location = New System.Drawing.Point(13, 244)
        Me.label10.Name = "label10"
        Me.label10.Size = New System.Drawing.Size(352, 2)
        Me.label10.TabIndex = 44
        '
        'edUnitsOnOrder
        '
        Me.edUnitsOnOrder.Location = New System.Drawing.Point(301, 204)
        Me.edUnitsOnOrder.Maximum = New Decimal(New Integer() {1000, 0, 0, 0})
        Me.edUnitsOnOrder.Name = "edUnitsOnOrder"
        Me.edUnitsOnOrder.Size = New System.Drawing.Size(64, 21)
        Me.edUnitsOnOrder.TabIndex = 43
        Me.edUnitsOnOrder.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
        '
        'label9
        '
        Me.label9.Location = New System.Drawing.Point(205, 208)
        Me.label9.Name = "label9"
        Me.label9.Size = New System.Drawing.Size(84, 16)
        Me.label9.TabIndex = 42
        Me.label9.Text = "Units On Order:"
        '
        'edUnitsInStock
        '
        Me.edUnitsInStock.Location = New System.Drawing.Point(301, 176)
        Me.edUnitsInStock.Maximum = New Decimal(New Integer() {1000, 0, 0, 0})
        Me.edUnitsInStock.Name = "edUnitsInStock"
        Me.edUnitsInStock.Size = New System.Drawing.Size(64, 21)
        Me.edUnitsInStock.TabIndex = 41
        Me.edUnitsInStock.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
        '
        'label8
        '
        Me.label8.Location = New System.Drawing.Point(205, 180)
        Me.label8.Name = "label8"
        Me.label8.Size = New System.Drawing.Size(80, 16)
        Me.label8.TabIndex = 40
        Me.label8.Text = "Units In Stock:"
        '
        'edUnitPrice
        '
        Me.edUnitPrice.Location = New System.Drawing.Point(97, 204)
        Me.edUnitPrice.Name = "edUnitPrice"
        Me.edUnitPrice.Size = New System.Drawing.Size(88, 21)
        Me.edUnitPrice.TabIndex = 37
        Me.edUnitPrice.Text = ""
        '
        'label7
        '
        Me.label7.Location = New System.Drawing.Point(13, 208)
        Me.label7.Name = "label7"
        Me.label7.Size = New System.Drawing.Size(64, 16)
        Me.label7.TabIndex = 36
        Me.label7.Text = "Unit Price:"
        '
        'edReorderLevel
        '
        Me.edReorderLevel.Location = New System.Drawing.Point(301, 148)
        Me.edReorderLevel.Maximum = New Decimal(New Integer() {1000, 0, 0, 0})
        Me.edReorderLevel.Name = "edReorderLevel"
        Me.edReorderLevel.Size = New System.Drawing.Size(64, 21)
        Me.edReorderLevel.TabIndex = 39
        Me.edReorderLevel.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
        '
        'label6
        '
        Me.label6.Location = New System.Drawing.Point(205, 152)
        Me.label6.Name = "label6"
        Me.label6.Size = New System.Drawing.Size(80, 16)
        Me.label6.TabIndex = 38
        Me.label6.Text = "Reorder Level:"
        '
        'edQuantityPerUnit
        '
        Me.edQuantityPerUnit.Location = New System.Drawing.Point(13, 168)
        Me.edQuantityPerUnit.Name = "edQuantityPerUnit"
        Me.edQuantityPerUnit.Size = New System.Drawing.Size(172, 21)
        Me.edQuantityPerUnit.TabIndex = 35
        Me.edQuantityPerUnit.Text = ""
        '
        'label5
        '
        Me.label5.Location = New System.Drawing.Point(13, 148)
        Me.label5.Name = "label5"
        Me.label5.Size = New System.Drawing.Size(100, 16)
        Me.label5.TabIndex = 34
        Me.label5.Text = "Quantity Per Unit:"
        '
        'chkDiscontinued
        '
        Me.chkDiscontinued.Location = New System.Drawing.Point(269, 112)
        Me.chkDiscontinued.Name = "chkDiscontinued"
        Me.chkDiscontinued.Size = New System.Drawing.Size(92, 20)
        Me.chkDiscontinued.TabIndex = 33
        Me.chkDiscontinued.Text = "Discontinued"
        '
        'cbSupplier
        '
        Me.cbSupplier.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cbSupplier.Location = New System.Drawing.Point(13, 112)
        Me.cbSupplier.MaxDropDownItems = 6
        Me.cbSupplier.Name = "cbSupplier"
        Me.cbSupplier.Size = New System.Drawing.Size(230, 21)
        Me.cbSupplier.TabIndex = 32
        '
        'label4
        '
        Me.label4.Location = New System.Drawing.Point(13, 92)
        Me.label4.Name = "label4"
        Me.label4.Size = New System.Drawing.Size(60, 16)
        Me.label4.TabIndex = 31
        Me.label4.Text = "Supplier:"
        '
        'edProductName
        '
        Me.edProductName.Location = New System.Drawing.Point(13, 52)
        Me.edProductName.Name = "edProductName"
        Me.edProductName.Size = New System.Drawing.Size(220, 21)
        Me.edProductName.TabIndex = 28
        Me.edProductName.Text = ""
        '
        'label3
        '
        Me.label3.Location = New System.Drawing.Point(13, 32)
        Me.label3.Name = "label3"
        Me.label3.Size = New System.Drawing.Size(80, 16)
        Me.label3.TabIndex = 27
        Me.label3.Text = "Product Name:"
        '
        'label2
        '
        Me.label2.Location = New System.Drawing.Point(249, 32)
        Me.label2.Name = "label2"
        Me.label2.Size = New System.Drawing.Size(68, 16)
        Me.label2.TabIndex = 29
        Me.label2.Text = "Category:"
        '
        'cbCategory
        '
        Me.cbCategory.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cbCategory.Location = New System.Drawing.Point(249, 52)
        Me.cbCategory.MaxDropDownItems = 6
        Me.cbCategory.Name = "cbCategory"
        Me.cbCategory.Size = New System.Drawing.Size(116, 21)
        Me.cbCategory.TabIndex = 30
        '
        '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(125, 12)
        Me.label1.Name = "label1"
        Me.label1.Size = New System.Drawing.Size(124, 20)
        Me.label1.TabIndex = 26
        Me.label1.Text = "Product Information"
        '
        'ProductForm
        '
        Me.AcceptButton = Me.btnOk
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 14)
        Me.CancelButton = Me.btnCancel
        Me.ClientSize = New System.Drawing.Size(377, 295)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.label12, Me.label11, Me.labSupplierCountry, Me.labSupplierCity, Me.picCategory, Me.btnCancel, Me.btnOk, Me.label10, Me.edUnitsOnOrder, Me.label9, Me.edUnitsInStock, Me.label8, Me.edUnitPrice, Me.label7, Me.edReorderLevel, Me.label6, Me.edQuantityPerUnit, Me.label5, Me.chkDiscontinued, Me.cbSupplier, Me.label4, Me.edProductName, Me.label3, Me.label2, Me.cbCategory, Me.label1})
        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.MinimizeBox = False
        Me.Name = "ProductForm"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
        Me.Text = "ProductForm"
        CType(Me.edUnitsOnOrder, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.edUnitsInStock, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.edReorderLevel, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private _images As ImageList
    Private _c1DataSet As C1.Data.C1DataSet
    Private _c1DataRow As C1.Data.C1DataRow = Nothing
    Private _product As ProductsRow_tableView = Nothing

    Friend Sub New(ByVal wincaption As String, ByVal dataSet As C1.Data.C1DataSet, _
    ByVal images As ImageList)
        Me.New()
        Me.Text = wincaption
        _c1DataSet = dataSet
        _images = images
        cbCategory.DataSource = dataSet
        cbCategory.DisplayMember = "Categories.CategoryName"
        cbCategory.ValueMember = "Categories.CategoryID"
        cbSupplier.DataSource = dataSet
        cbSupplier.DisplayMember = "Suppliers.CompanyName"
        cbSupplier.ValueMember = "Suppliers.SupplierID"
    End Sub

    Private Sub CheckString(ByVal sender As Object, ByVal cevent As ConvertEventArgs)
        If Not cevent.DesiredType Is GetType(String) Then
            Return
        End If
        If Not TypeOf cevent.Value Is System.DBNull Then
            cevent.Value = CType(cevent.Value, String)
        Else
            cevent.Value = ""
        End If
    End Sub

    Private Sub CheckBoolean(ByVal sender As Object, ByVal cevent As ConvertEventArgs)
        If Not cevent.DesiredType Is GetType(Boolean) Then
            Return
        End If
        If Not TypeOf cevent.Value Is System.DBNull Then
            cevent.Value = CType(cevent.Value, Boolean)
        Else
            cevent.Value = False
        End If
    End Sub

    Private Sub DecimalToString(ByVal sender As Object, ByVal cevent As ConvertEventArgs)
        If Not cevent.DesiredType Is GetType(String) Then
            Return
        End If
        If Not TypeOf cevent.Value Is System.DBNull Then
            cevent.Value = CType(cevent.Value, Decimal).ToString()
        Else
            cevent.Value = ""
        End If
    End Sub

    Private Sub CheckDecimal(ByVal sender As Object, ByVal cevent As ConvertEventArgs)
        If Not cevent.DesiredType Is GetType(Decimal) Then
            Return
        End If
        If Not TypeOf cevent.Value Is System.DBNull Then
            cevent.Value = CType(cevent.Value, Short)
        Else
            cevent.Value = 0
        End If
    End Sub

    Private Sub ProductForm_Layout(ByVal sender As Object, ByVal e As System.Windows.Forms.LayoutEventArgs) Handles MyBase.Layout
        If TypeOf _c1DataRow("CategoryID") Is System.DBNull Then
            cbCategory.SelectedIndex = -1
        End If
        If TypeOf _c1DataRow("SupplierID") Is System.DBNull Then
            cbSupplier.SelectedIndex = -1
        End If
    End Sub

    Friend Sub SetCurrentRow(ByVal dataRow As C1.Data.C1DataRow)
        _product = ProductsRow_tableView.Obj(dataRow)
        _c1DataRow = dataRow
        If Not _product.IsProductNameNull() Then
            edProductName.Text = _product.ProductName
        Else
            edProductName.Text = ""
        End If
        If Not _product.IsCategoryIDNull() Then
            cbCategory.SelectedValue = _product.CategoryID
        End If
        If Not _product.IsSupplierIDNull() Then
            cbSupplier.SelectedValue = _product.SupplierID
        End If
        If Not _product.IsDiscontinuedNull() Then
            chkDiscontinued.Checked = _product.Discontinued
        Else
            chkDiscontinued.Checked = False
        End If
        If Not _product.IsQuantityPerUnitNull() Then
            edQuantityPerUnit.Text = _product.QuantityPerUnit
        Else
            edQuantityPerUnit.Text = ""
        End If
        If Not _product.IsUnitPriceNull() Then
            edUnitPrice.Text = _product.UnitPrice.ToString()
        Else
            edUnitPrice.Text = ""
        End If
        If Not _product.IsReorderLevelNull() Then
            edReorderLevel.Value = _product.ReorderLevel
        Else
            edReorderLevel.Value = 0
        End If
        If Not _product.IsUnitsInStockNull() Then
            edUnitsInStock.Value = _product.UnitsInStock
        Else
            edUnitsInStock.Value = 0
        End If
        If Not _product.IsUnitsOnOrderNull() Then
            edUnitsOnOrder.Value = _product.UnitsOnOrder
        Else
            edUnitsOnOrder.Value = 0
        End If
    End Sub

    Private Sub cbCategory_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbCategory.SelectedIndexChanged
        Dim index As Integer = cbCategory.SelectedIndex
        If index >= 0 And index < _images.Images.Count Then
            picCategory.Image = _images.Images(index)
        Else
            picCategory.Image = Nothing
        End If
    End Sub

    Private Sub cbSupplier_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbSupplier.SelectedIndexChanged
        If cbSupplier.SelectedIndex <> -1 Then
            Dim row As C1.Data.C1DataRow = _c1DataSet.TableViews("Suppliers"). _
             Rows(cbSupplier.SelectedIndex)
            labSupplierCity.Text = row("City")
            labSupplierCountry.Text = row("Country")
        Else
            labSupplierCity.Text = ""
            labSupplierCountry.Text = ""
        End If
    End Sub

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        If edProductName.Text.Length > 0 Then
            _product.ProductName = edProductName.Text
        Else
            _product.SetProductNameNull()
        End If
        If cbCategory.SelectedIndex <> -1 Then
            _product.CategoryID = CType(cbCategory.SelectedValue, Integer)
        Else
            _product.SetCategoryIDNull()
        End If
        If cbSupplier.SelectedIndex <> -1 Then
            _product.SupplierID = CType(cbSupplier.SelectedValue, Integer)
        Else
            _product.SetSupplierIDNull()
        End If
        _product.Discontinued = chkDiscontinued.Checked
        If edQuantityPerUnit.Text.Length > 0 Then
            _product.QuantityPerUnit = edQuantityPerUnit.Text
        Else
            _product.SetQuantityPerUnitNull()
        End If
        If edUnitPrice.Text.Length > 0 Then
            _product.UnitPrice = Decimal.Parse(edUnitPrice.Text)
        Else
            _product.SetUnitPriceNull()
        End If
        _product.ReorderLevel = CType(edReorderLevel.Value, Short)
        _product.UnitsInStock = CType(edUnitsInStock.Value, Short)
        _product.UnitsOnOrder = CType(edUnitsOnOrder.Value, Short)
        _c1DataRow.EndEdit()
        DialogResult = System.Windows.Forms.DialogResult.OK
    End Sub

End Class
