Imports System.Collections.ObjectModel Imports EgtWPFLib5 Public Class UpdateProductPageVM Inherits VMBase #Region "FIELDS & PROPERTIES" Private m_Product As Product Public Property Product As Product Get Return m_Product End Get Set(value As Product) m_Product = value End Set End Property Private m_NewProductName As String Public Property NewProductName As String Get Return m_NewProductName End Get Set(value As String) m_NewProductName = value NotifyPropertyChanged("NewProductName") End Set End Property Private m_NewProductNumber As Integer Public Property NewProductNumber As Integer Get Return m_NewProductNumber End Get Set(value As Integer) m_NewProductNumber = value NotifyPropertyChanged("NewProductNumber") End Set End Property Private m_NewOption1 As New ObservableCollection(Of KeyOption) Public ReadOnly Property NewOption1 As ObservableCollection(Of KeyOption) Get Return m_NewOption1 End Get End Property Private m_NewOption2 As New ObservableCollection(Of KeyOption) Public ReadOnly Property NewOption2 As ObservableCollection(Of KeyOption) Get Return m_NewOption2 End Get End Property ' Definizione comandi Private m_cmdUpdate As Command Private m_cmdCancel As Command #Region "Messages" Public ReadOnly Property UpdateProductMsg As String Get Return "Update product" End Get End Property Public ReadOnly Property ProductNameMsg As String Get Return "Name" End Get End Property Public ReadOnly Property ProductNumberMsg As String Get Return "Number" End Get End Property Public ReadOnly Property Option1Msg As String Get Return "Option 1" End Get End Property Public ReadOnly Property Option2Msg As String Get Return "Option 2" End Get End Property Public ReadOnly Property UpdateMsg As String Get Return "Update" End Get End Property Public ReadOnly Property CancelMsg As String Get Return "Close" End Get End Property #End Region ' Messages #End Region ' FIELDS & PROPERTIES #Region "CONSTRUCTOR" Sub New() ' Imposto riferimento nella mappa Map.SetRefUpdateProductPageVM(Me) End Sub #End Region ' CONSTRUCTOR #Region "METHODS" Friend Sub InitUpdateProductPage() Dim Query As String = "SELECT * FROM " & DB_PRODUCT & " WHERE " & DB_PRODUCTID & " LIKE " & Product.ProductID NewProductName = ManageDb.ExecuteProductQuery(Query)(0).ProductName NotifyPropertyChanged("NewProductName") NewProductNumber = ManageDb.ExecuteProductQuery(Query)(0).ProductNumber NotifyPropertyChanged("NewProductNumber") '' Svuoto campi 'NewProductName = Nothing 'NotifyPropertyChanged("NewProductName") 'NewProductNumber = Nothing 'NotifyPropertyChanged("NewProductNumber") ' Cancello liste opzioni LoadOptions(1, NewOption1) LoadOptions(2, NewOption2) NotifyPropertyChanged("NewOption1") NotifyPropertyChanged("NewOption2") End Sub Private Sub LoadOptions(nIndex As Integer, ByRef OptionList As ObservableCollection(Of KeyOption)) ' Cancello opzioni OptionList.Clear() ' Carico opzioni Dim OptionIndex As Integer = 1 Dim OptionName As String = String.Empty GetMainPrivateProfileString(m_Product.ProductName & " - Option" & nIndex, "Option" & OptionIndex, "", OptionName) While Not String.IsNullOrWhiteSpace(OptionName) OptionList.Add(New KeyOption(False, True, OptionName)) OptionIndex += 1 GetMainPrivateProfileString(m_Product.ProductName & " - Option" & nIndex, "Option" & OptionIndex, "", OptionName) End While ' Se ho caricato delle opzioni If OptionList.Count > 0 Then ' Verifico se ci sono opzioni del prodotto Dim ProductOption As Integer = If(nIndex = 1, m_Product.ProductOption1, m_Product.ProductOption2) Dim nBinaryIndex As Integer = 1 For I = 0 To OptionList.Count - 1 If (ProductOption And nBinaryIndex) <> 0 Then OptionList(I).IsChecked = True OptionList(I).IsEnabled = True End If nBinaryIndex *= 2 Next End If End Sub Private Function CalcOptionDec(OptionList As ObservableCollection(Of KeyOption)) As Integer Dim nDecOption As Integer = 0 Dim nBinaryIndex As Integer = 1 For I As Integer = 0 To OptionList.Count() - 1 If OptionList(I).IsChecked Then nDecOption += nBinaryIndex End If nBinaryIndex *= 2 Next Return nDecOption End Function #End Region ' METHODS #Region "COMMANDS" #Region "Update" ' Returns a command that manage the MainWindow_Unloaded command Public ReadOnly Property UpdateProduct_Command As ICommand Get If m_cmdUpdate Is Nothing Then m_cmdUpdate = New Command(AddressOf Update) End If Return m_cmdUpdate End Get End Property Public Sub Update(ByVal param As Object) 'Calcolo valore decimale opzione1 Dim nDecOption1 As Integer = CalcOptionDec(m_NewOption1) Dim nDecOption2 As Integer = CalcOptionDec(m_NewOption2) If Not String.IsNullOrWhiteSpace(NewProductName) OrElse Not NewProductNumber = 0 OrElse Not nDecOption1 = Product.ProductOption1 OrElse Not nDecOption2 = Product.ProductOption2 Then ' Cerco nella tabella Product Dim Query As String = "UPDATE " & DB_PRODUCT Dim bFirstWhere As Boolean = True If Not String.IsNullOrWhiteSpace(NewProductName) OrElse Not NewProductNumber = 0 OrElse Not IsNothing(nDecOption1) OrElse Not IsNothing(nDecOption2) Then Query &= " SET " If Not String.IsNullOrWhiteSpace(NewProductName) Then EvalWhere(bFirstWhere, Query) Query &= DB_PRODUCTNAME & " = '" & NewProductName & "' " End If If Not NewProductNumber = 0 Then EvalWhere(bFirstWhere, Query) Query &= DB_PRODUCTNUMBER & " = '" & NewProductNumber & "' " End If If Not nDecOption1 = Product.ProductOption1 Then EvalWhere(bFirstWhere, Query) Query &= DB_PRODUCTOPTION1 & " = '" & nDecOption1 & "' " End If If Not nDecOption2 = Product.ProductOption2 Then EvalWhere(bFirstWhere, Query) Query &= DB_PRODUCTOPTION2 & " = '" & nDecOption2 & "' " End If Query &= "WHERE " & DB_PRODUCTID & " = " & Product.ProductID Query = Query.TrimEnd(","c, " "c) End If ManageDb.ExecuteQuery(Query) ' Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded. Map.refMainWindowVM.SelProjectMode = MainWindowVM.ProjectModeOpt.SEARCHPRODUCT Else MessageBox.Show("Completare almeno un campo presente") End If End Sub Private Sub EvalWhere(ByRef bFirst As Boolean, ByRef Query As String) If bFirst Then bFirst = False Else Query &= ", " End If End Sub #End Region ' Update #Region "Cancel" ' Returns a command that manage the MainWindow_Unloaded command Public ReadOnly Property Cancel_Command As ICommand Get If m_cmdCancel Is Nothing Then m_cmdCancel = New Command(AddressOf Cancel) End If Return m_cmdCancel End Get End Property Public Sub Cancel(ByVal param As Object) ' Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded. Map.refMainWindowVM.SelProjectMode = MainWindowVM.ProjectModeOpt.SEARCHPRODUCT End Sub #End Region ' Cancel #End Region 'COMMANDS End Class