Files
cameramanager/CameraMng/SetConfigForm.vb
T

201 lines
8.5 KiB
VB.net

Imports System.Globalization
Imports System.IO
Public Class SetConfigForm
Private CameraListCfg As New List(Of String)
Private myImage As Image
Sub New()
' La chiamata è richiesta dalla finestra di progettazione.
InitializeComponent()
' Aggiungere le eventuali istruzioni di inizializzazione dopo la chiamata a InitializeComponent().
' PopolateDataGrid()
End Sub
Private Sub LoadWindows() Handles Me.Load
PopolateDataGrid()
End Sub
Private Sub PopolateDataGrid()
DataGridView1.Rows.Clear()
CameraListCfg.Clear()
ComboCamera.Items.Clear()
Dim DirToReadCfg As String = FrmMain.sDataRoot
' Popolo combobox con idcamera
For Each CameraItem As String In FrmMain.ComboBoxCameras.Items
ComboCamera.Items.Add(CameraItem)
Next
' Popolo text con configurazione
For Each CfgItem As String In FrmMain.ComboBoxCameraCfg.Items
Dim NomeFileCfg As String = DirToReadCfg & If(CfgItem = "Default", String.Empty, CfgItem) & "\CameraMng.cfg"
Dim riga As New DataGridViewRow()
' Crea le celle corrispondenti alle colonne
riga.CreateCells(DataGridView1)
riga.Cells(0).Value = CfgItem
Dim id As String = ReadIdCameraFromCfg(NomeFileCfg)
If Not String.IsNullOrEmpty(id) Then
' Icone CFG
If CameraListCfg.IndexOf(id) < 0 Then
CameraListCfg.Add(id)
riga.Cells(1).Value = GetCurrIco(2)
Else
riga.Cells(1).Value = GetCurrIco(0)
End If
' Icone CAMERA
If ComboCamera.Items.IndexOf(id) > -1 Then
riga.Cells(3).Value = GetCurrIco(2)
riga.Cells(2).Value = id
Else
riga.Cells(3).Value = GetCurrIco(0)
End If
Else
riga.Cells(1).Value = GetCurrIco(-1)
riga.Cells(3).Value = GetCurrIco(-1)
End If
riga.Cells(4).Value = "Save"
riga.Cells(5).Value = "Apply"
' Aggiungi la riga completa al DataGridView
DataGridView1.Rows.Add(riga)
Next
End Sub
Private Function ReadIdCameraFromCfg(NomeFileCfg As String) As String
Dim IDCamera As String = String.Empty
Dim reader As New StreamReader(NomeFileCfg)
Dim nfi As NumberFormatInfo = New CultureInfo("en-US", False).NumberFormat
nfi.NumberDecimalSeparator = "."
Dim TmpString As String = ""
Try
While (Not reader.EndOfStream)
TmpString = reader.ReadLine()
If TmpString.StartsWith("CameraID=") Then
IDCamera = Mid(TmpString, 10)
Exit While
End If
End While
reader.Close()
reader.Dispose()
Catch ex As Exception
MsgBox("Error in Cfg File " & TmpString)
End Try
Return IDCamera
End Function
Private Function GetCurrIco(Index As Integer) As Image
Select Case Index
Case -1
myImage = My.Resources.cross
Case 0
myImage = My.Resources.warning
Case Else
myImage = My.Resources.accept
End Select
Return myImage
End Function
Public Sub SaveIDCamera(IDConfing As String, IDCamera As String)
Dim NomeFileCfg, TmpString As String
Dim NomeFileTempCfg, DirToReadCfg As String
Dim SaveFileCfg As String
Dim nf As Integer
Dim writer As StreamWriter
Try
DirToReadCfg = FrmMain.sDataRoot & IDConfing
NomeFileCfg = DirToReadCfg & "\CameraMng.cfg"
NomeFileTempCfg = DirToReadCfg & "\CameraMngTmp.cfg"
SaveFileCfg = DirToReadCfg & "\CameraMng" & Format(Now, "yyyyMMddhhmmss") & ".cfg"
If (File.Exists(NomeFileTempCfg)) Then
File.Delete(NomeFileTempCfg)
End If
nf = FreeFile()
FileOpen(nf, NomeFileCfg, OpenMode.Input)
writer = New StreamWriter(NomeFileTempCfg)
TmpString = ""
While Not EOF(nf)
TmpString = LineInput(nf)
If Not TmpString.StartsWith("CameraID=") Then
writer.WriteLine(TmpString, CultureInfo.InvariantCulture)
End If
End While
writer.WriteLine("CameraID=" & IDCamera, CultureInfo.InvariantCulture)
FileClose(nf)
writer.Close()
File.Copy(NomeFileCfg, SaveFileCfg)
If (File.Exists(NomeFileCfg)) Then
File.Delete(NomeFileCfg)
End If
File.Copy(NomeFileTempCfg, NomeFileCfg)
File.Delete(NomeFileTempCfg)
MessageBox.Show("New ID succesfully written on '" & NomeFileCfg & "'.", "Avviso", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
MsgBox("Error in Writing Cfg File " & ex.Message)
End Try
End Sub
Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
' Controlla se la cella corrente è nella colonna della ComboBox e se il controllo è una ComboBox
If DataGridView1.CurrentCell.ColumnIndex = DataGridView1.Columns("ComboCamera").Index AndAlso TypeOf e.Control Is ComboBox Then
Dim comboBox As ComboBox = TryCast(e.Control, ComboBox)
If comboBox IsNot Nothing Then
' Rimuovi eventuali gestori eventi precedenti per evitare eventi duplicati
' Questo è importante se l'utente modifica più volte la stessa cella
RemoveHandler comboBox.SelectedIndexChanged, AddressOf DataGridViewComboBox_SelectedIndexChanged
' Aggiungi il gestore eventi per l'evento SelectedIndexChanged della ComboBox
AddHandler comboBox.SelectedIndexChanged, AddressOf DataGridViewComboBox_SelectedIndexChanged
End If
End If
End Sub
' Questo è il gestore eventi che si attiverà quando l'utente seleziona un elemento nella ComboBox
Private Sub DataGridViewComboBox_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim comboBox As ComboBox = TryCast(sender, ComboBox)
If comboBox IsNot Nothing Then
Dim selectedValue As String = comboBox.SelectedItem.ToString()
Dim currentRowIndex As Integer = DataGridView1.CurrentCell.RowIndex
Dim IDCamera As String = If(Not IsNothing(DataGridView1.Rows(currentRowIndex).Cells("ComboCamera").Value), DataGridView1.Rows(currentRowIndex).Cells("ComboCamera").Value.ToString(), String.Empty)
If IDCamera <> selectedValue Then
DataGridView1.Rows(currentRowIndex).Cells(4).Value = "Save*"
End If
End If
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
' Controlla che il click non sia sull'intestazione della colonna
If e.RowIndex < 0 Then Return
Dim IDConfig As String = DataGridView1.Rows(e.RowIndex).Cells("Config").Value.ToString()
Dim IDCamera As String = If(Not IsNothing(DataGridView1.Rows(e.RowIndex).Cells("ComboCamera").Value), DataGridView1.Rows(e.RowIndex).Cells("ComboCamera").Value.ToString(), String.Empty)
' Controlla se il click è avvenuto nella colonna dei bottoni (Usa il nome che hai dato alla colonna)
If Me.DataGridView1.Columns(e.ColumnIndex).Name = "SaveConfig" Then
If Not String.IsNullOrEmpty(IDConfig) And Not String.IsNullOrEmpty(IDCamera) Then
SaveIDCamera(IDConfig, IDCamera)
DataGridView1.Rows(e.RowIndex).Cells(4).Value = "Save"
Else
MessageBox.Show("Non è possibile salvare l'ID di una camera non connessa.", "Avviso", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
End If
PopolateDataGrid()
ElseIf Me.DataGridView1.Columns(e.ColumnIndex).Name = "ApplyConfig" Then
If Not (String.IsNullOrEmpty(IDConfig) And String.IsNullOrEmpty(IDCamera)) Then
' verifico che non sia stato salvato l'id indicato
If DataGridView1.Rows(e.RowIndex).Cells(4).Value.ToString().Contains("*"c) Then
MessageBox.Show("Salvare la configurazione prima di applicarla.", "Informazione", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
End If
Dim IndCamera As Integer = FrmMain.ComboBoxCameras.Items.IndexOf(IDCamera)
FrmMain.ComboBoxCameras.SelectedIndex = IndCamera
Dim IndCameraCfg As Integer = FrmMain.ComboBoxCameraCfg.Items.IndexOf(IDConfig)
FrmMain.ComboBoxCameraCfg.SelectedIndex = IndCameraCfg
End If
End If
End Sub
End Class