Imports System.Collections.ObjectModel

Public Class EditValueWD

   ' Riferimento alla MainWindow
   Private m_MainWindow As MainWindow = DirectCast(Application.Current.MainWindow, MainWindow)
   Private m_sMsg As String = "Value"
   Private m_sValue As String = ""
   Private m_sList As ObservableCollection(Of String)
   Private m_IsText As Boolean = True

   ' avvia la visualizzazione della textbox
   Sub New(Owner As Window, Optional sMsg As String = "")
      Me.Owner = Owner
      m_sMsg = sMsg
      m_IsText = True
      InitializeComponent()
   End Sub

   Friend Function SetVal(dVal As Double, Optional ByVal bLen As Boolean = False) As Boolean
      If bLen Then
         m_sValue = LenToString(dVal, 3)
      Else
         m_sValue = DoubleToString(dVal, 3)
      End If
      ValueTxBx.Text = m_sValue
      Return True
   End Function

   ' avvia la visualizzazione della combobox
   Sub New(Owner As Window, sList As ObservableCollection(Of String), Optional sMsg As String = "")
      Me.Owner = Owner
      m_sMsg = sMsg
      m_IsText = False
      InitializeComponent()
      m_sList = sList
      SetComboBox(m_sList)
   End Sub

   Friend Function GetVal(Optional ByVal bLen As Boolean = False) As Double
      Dim dVal As Double = 0
      If bLen Then
         StringToLen(m_sValue, dVal)
      Else
         StringToDouble(m_sValue, dVal)
      End If
      Return dVal
   End Function

   Friend Function SetComboBox(sList As ObservableCollection(Of String)) As Boolean
      ValueCmBx.ItemsSource = sList
      Return True
   End Function

   Friend Function SetItemComboBox(sItem As String) As Boolean
      For Each MyItem As String In ValueCmBx.ItemsSource
         If MyItem = sItem Then
            ValueCmBx.SelectedItem = MyItem
         End If
      Next
      Return True
   End Function

   Friend Function GetItemComboBox() As String
      Dim sVal As String = ValueCmBx.SelectedItem
      Return sVal
   End Function

   Private Sub EditValueWD_Initialized(sender As Object, e As EventArgs) Handles Me.Initialized
      Me.Top = Owner.Top + Owner.Height / 2 - Me.Height / 2
      Me.Left = Owner.Left + Owner.Width / 2 - Me.Width / 2
      ValueTxbl.Text = m_sMsg
      If m_IsText Then
         ValueCmBx.Visibility = Visibility.Collapsed
         ValueTxBx.Visibility = Visibility.Visible
      Else
         ValueCmBx.Visibility = Visibility.Visible
         ValueTxBx.Visibility = Visibility.Collapsed
      End If
   End Sub

   Private Sub EditValueWD_Rendered(sender As Object, e As EventArgs) Handles Me.ContentRendered
      ' ValueTxBx.Focus()
      ' ValueTxBx.OpenKeyboard(ValueTxBx, ValueTxBx)
   End Sub

   'Private Sub ValueTxBx_Closed(Sender As Object, e As EventArgs) Handles ValueTxBx.EgtClosed
   '   m_sValue = ValueTxBx.Text
   '   DialogResult = True
   'End Sub

   Private Sub OkBtn_Click(sender As Object, e As RoutedEventArgs) Handles OkBtn.Click
      m_sValue = ValueTxBx.Text
      DialogResult = True
   End Sub

   Private Sub ExitBtn_Click(sender As Object, e As RoutedEventArgs) Handles ExitBtn.Click
      m_sValue = ""
      DialogResult = False
   End Sub

End Class
