Gestione selezione configurazione corrente

This commit is contained in:
Nicola Pievani
2025-07-30 11:20:57 +02:00
parent 8b970aa3ba
commit a36e5e1323
5 changed files with 72 additions and 35 deletions
+71 -34
View File
@@ -6,17 +6,21 @@ Public Class SetConfigForm
Private CameraListCfg As New List(Of String)
Private myImage As Image
Private CurrIndexRow As Integer = 0
Private CurrAppliedConfig As String = String.Empty
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()
CurrAppliedConfig = FrmMain.ComboBoxCameraCfg.SelectedItem
End Sub
Private Sub PopolateDataGrid()
@@ -59,9 +63,58 @@ Public Class SetConfigForm
riga.Cells(5).Value = "Apply"
' Aggiungi la riga completa al DataGridView
DataGridView1.Rows.Add(riga)
If CfgItem = FrmMain.ComboBoxCameraCfg.SelectedItem Then
DataGridView1.ClearSelection()
' Mantengo la selezione sulla riga corrente
DataGridView1.Rows(DataGridView1.Rows.Count - 1).Selected = True
' Indico in grassetto il comando Apply
End If
Next
End Sub
Private Sub RefreshDataGrid(IndexRawDataGrid As Integer)
DataGridView1.ClearSelection()
Dim CfgItem As String = DataGridView1.Rows(IndexRawDataGrid).Cells("Config").Value.ToString()
' Aggiorno lo stato delle icone della lista
Dim DirToReadCfg As String = FrmMain.sDataRoot
Dim NomeFileCfg As String = DirToReadCfg & If(CfgItem = "Default", String.Empty, CfgItem) & "\CameraMng.cfg"
' Creo la nuova riga che sostituirà quella precedente
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
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"
' Sostituisco la riga corrente: rimuovo la precedente riga e ne inserisco una nuova
DataGridView1.Rows.RemoveAt(IndexRawDataGrid)
DataGridView1.Rows.Insert(IndexRawDataGrid, riga)
' Mantengo la selezione sulla riga corrente
DataGridView1.CurrentCell = DataGridView1.Rows(IndexRawDataGrid).Cells(4)
End Sub
Private Function ReadIdCameraFromCfg(NomeFileCfg As String) As String
Dim IDCamera As String = String.Empty
Dim reader As New StreamReader(NomeFileCfg)
@@ -137,40 +190,14 @@ Public Class SetConfigForm
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
' Tutti i click sulla datagrid passono da qui
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
' Controlla che il click non sia sull'intestazione della colonna
If e.RowIndex < 0 Then Return
DataGridView1.Rows(e.RowIndex).Selected = True
If e.ColumnIndex < 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)
@@ -179,10 +206,11 @@ Public Class SetConfigForm
If Not String.IsNullOrEmpty(IDConfig) And Not String.IsNullOrEmpty(IDCamera) Then
SaveIDCamera(IDConfig, IDCamera)
DataGridView1.Rows(e.RowIndex).Cells(4).Value = "Save"
RefreshDataGrid(e.RowIndex)
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
@@ -195,6 +223,15 @@ Public Class SetConfigForm
FrmMain.ComboBoxCameraCfg.SelectedIndex = IndCameraCfg
End If
ElseIf Me.DataGridView1.Columns(e.ColumnIndex).Name = "ComboCamera" Then
Dim comboBox As ComboBox = TryCast(sender, ComboBox)
If comboBox IsNot Nothing Then
Dim selectedValue As String = comboBox.SelectedItem.ToString()
If IDCamera <> selectedValue Then
DataGridView1.Rows(e.RowIndex).Cells(4).Value = "Save*"
End If
End If
End If
End Sub
@@ -1 +1 @@
2e81f805195d83a1f7262e8a96404099beb76326e9e80773b0d6eccdfc480373
69636ff66d10d465f87b29c6b3440534ee373b52aa51b9726c6ce7a90119bc6f