Advertisement
ficho2604

Untitled

Apr 18th, 2023
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 24.56 KB | None | 0 0
  1. Imports System.IO
  2. Imports System.Runtime.InteropServices
  3. Imports System.Text
  4. Imports System.Windows.Forms
  5. Imports Excel = Microsoft.Office.Interop.Excel
  6.  
  7. Public Class Form1
  8.     Private savedFilePaths As New Dictionary(Of String, String)()
  9.     Private folderPaths As New Dictionary(Of String, String)()
  10.     Private fileDict As New Dictionary(Of String, String)()
  11.     Private excelDataCache As New Dictionary(Of String, List(Of List(Of String)))()
  12.     Private WithEvents excelApp As Excel.Application
  13.     Private openExcelInstances As New List(Of Excel.Application)()
  14.  
  15.  
  16.     Private Sub SaveFilePathsToFile()
  17.         Dim filePath As String = Path.Combine(Application.StartupPath, "selected_files.txt")
  18.  
  19.         Using sw As New StreamWriter(filePath)
  20.             For Each item As String In ListBox1.Items
  21.                 Dim filePathForItem As String = savedFilePaths.FirstOrDefault(Function(kvp) kvp.Key = item).Value
  22.  
  23.                 If filePathForItem IsNot Nothing Then
  24.                     Dim line As New StringBuilder()
  25.                     line.Append(item).Append("|").Append(filePathForItem)
  26.                     sw.WriteLine(line.ToString())
  27.                 End If
  28.             Next
  29.         End Using
  30.     End Sub
  31.     Private Function GetFormattedRowData(row As Excel.Range, columnIndices As Dictionary(Of String, Integer), maxColumnWidths As Dictionary(Of String, Integer)) As String
  32.         Dim rowData As String = ""
  33.         For Each header As String In maxColumnWidths.Keys
  34.             If columnIndices.ContainsKey(header) Then
  35.                 Dim cell As Excel.Range = row.Cells(1, columnIndices(header))
  36.                 Dim cellValue As String = If(cell.Value IsNot Nothing, cell.Value.ToString(), "")
  37.                 rowData += cellValue.PadRight(maxColumnWidths(header) + 2)
  38.             End If
  39.         Next
  40.         Return rowData
  41.     End Function
  42.     Private Function PadCellValue(value As String, width As Integer) As String
  43.         Return value.PadRight(width)
  44.     End Function
  45.  
  46.     Private Sub DisplayExcelData(excelData As List(Of List(Of String)), dataGridView As DataGridView)
  47.         dataGridView.Rows.Clear()
  48.         For Each row As List(Of String) In excelData
  49.             If row.Count >= 4 Then
  50.                 dataGridView.Rows.Add(row(0), row(1), row(2), row(3))
  51.             End If
  52.         Next
  53.     End Sub
  54.  
  55.  
  56.     Private Function ReadAndCacheExcelData(filePath As String) As List(Of List(Of String))
  57.         Dim app As Excel.Application = New Excel.Application()
  58.         Dim wb As Excel.Workbook = app.Workbooks.Open(filePath)
  59.         Dim ws As Excel.Worksheet = Nothing
  60.  
  61.         For Each sheet As Excel.Worksheet In wb.Sheets
  62.             If sheet.Name = "Kabelliste" Then
  63.                 ws = sheet
  64.                 Exit For
  65.             End If
  66.         Next
  67.  
  68.         If ws Is Nothing Then
  69.             MessageBox.Show("Sheet 'data' not found in the selected Excel file.")
  70.             Return New List(Of List(Of String))()
  71.         End If
  72.  
  73.         Dim columnLetters As String() = {"K", "C", "A", "D"}
  74.         Dim cachedData As New List(Of List(Of String))()
  75.  
  76.         For i As Integer = 2 To ws.UsedRange.Rows.Count
  77.             Dim rowData As New List(Of String)()
  78.             For Each columnLetter As String In columnLetters
  79.                 Dim cell As Excel.Range = ws.Range(columnLetter & i.ToString())
  80.                 Dim cellValue As String = If(cell.Value IsNot Nothing, cell.Value.ToString(), "")
  81.                 rowData.Add(cellValue)
  82.             Next
  83.             cachedData.Add(rowData)
  84.         Next
  85.  
  86.         wb.Close(False)
  87.         app.Quit()
  88.  
  89.         ReleaseObject(ws)
  90.         ReleaseObject(wb)
  91.         ReleaseObject(app)
  92.  
  93.         Return cachedData
  94.     End Function
  95.  
  96.  
  97.  
  98.  
  99.     Private Sub SaveExcelPaths()
  100.         Dim lines As New List(Of String)
  101.         For Each key As String In fileDict.Keys
  102.             lines.Add($"{key}={fileDict(key)}")
  103.         Next
  104.         File.WriteAllLines("excel.txt", lines)
  105.     End Sub
  106.     Private Sub LoadExcelPaths()
  107.         If Not File.Exists("excel.txt") Then
  108.             Return
  109.         End If
  110.  
  111.         Dim lines As String() = File.ReadAllLines("excel.txt")
  112.         For Each line As String In lines
  113.             Dim parts As String() = line.Split("=")
  114.             If parts.Length = 2 Then
  115.                 fileDict(parts(0)) = parts(1)
  116.             End If
  117.         Next
  118.     End Sub
  119.     Private Sub SaveExcelFilePath(key As String, filePath As String)
  120.         fileDict(key) = filePath
  121.         SaveExcelPaths()
  122.     End Sub
  123.  
  124.     Private selectedItem As String = ""
  125.  
  126.  
  127.     Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
  128.         If ListBox1.SelectedIndex <> -1 Then
  129.             selectedItem = ListBox1.SelectedItem.ToString()
  130.         End If
  131.     End Sub
  132.  
  133.     Private Sub ListBox1_DoubleClick(sender As Object, e As EventArgs) Handles ListBox1.DoubleClick
  134.         If ListBox1.SelectedIndex <> -1 Then
  135.             OpenExcelFileDialog(ListBox1.SelectedItem.ToString())
  136.         End If
  137.     End Sub
  138.  
  139.  
  140.  
  141.  
  142.     Private Sub OpenExcelFileDialog(selectedItem As String)
  143.         Using openFileDialog As New OpenFileDialog()
  144.             openFileDialog.Filter = "Excel Files|*.xlsx;*.xls"
  145.             openFileDialog.Title = "Select an Excel File"
  146.  
  147.             If openFileDialog.ShowDialog() = DialogResult.OK Then
  148.                 Dim filePath As String = openFileDialog.FileName
  149.                 SaveExcelFilePath(selectedItem, filePath)
  150.                 excelDataCache(selectedItem) = ReadAndCacheExcelData(filePath)
  151.                 DisplayExcelData(excelDataCache(selectedItem), DataGridView1)
  152.             End If
  153.         End Using
  154.     End Sub
  155.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  156.         LoadPaths()
  157.         LoadFiles()
  158.         LoadExcelPaths()
  159.         Label1.Text = "Select harness from the list to show KAB."
  160.         For Each column As DataGridViewColumn In DataGridView1.Columns
  161.             column.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
  162.         Next
  163.  
  164.         DataGridView1.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
  165.         ProgressBar1.Visible = False
  166.     End Sub
  167.     Private Sub RadioButton1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles RadioButton1.Click
  168.         LoadFile("C:\Users\filip\Desktop\Harnesses\c2.txt")
  169.     End Sub
  170.  
  171.     Private Sub RadioButton2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles RadioButton2.Click
  172.         LoadFile("C:\Users\filip\Desktop\Harnesses\PF.txt")
  173.     End Sub
  174.  
  175.     Private Sub RadioButton3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles RadioButton3.Click
  176.         LoadFile("C:\Users\filip\Desktop\Harnesses\COMPONENTS.txt")
  177.     End Sub
  178.  
  179.     Private Sub radioButton1_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles RadioButton1.CheckedChanged
  180.         If RadioButton1.Checked Then
  181.             RadioButton1_Click(sender, e)
  182.         End If
  183.     End Sub
  184.  
  185.     Private Sub radioButton2_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles RadioButton2.CheckedChanged
  186.         If RadioButton2.Checked Then
  187.             RadioButton2_Click(sender, e)
  188.         End If
  189.     End Sub
  190.  
  191.     Private Sub radioButton3_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles RadioButton3.CheckedChanged
  192.         If RadioButton3.Checked Then
  193.             RadioButton3_Click(sender, e)
  194.         End If
  195.     End Sub
  196.  
  197.     Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
  198.         If e.KeyCode = Keys.Enter Then
  199.             Dim searchText As String = TextBox1.Text.ToLower()
  200.             For i As Integer = 0 To ListBox1.Items.Count - 1
  201.                 Dim currentItem As String = ListBox1.Items(i).ToString().ToLower()
  202.                 If currentItem.Contains(searchText) Then
  203.                     ListBox1.SelectedIndex = i
  204.                     Exit For
  205.                 End If
  206.             Next
  207.         End If
  208.     End Sub
  209.     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  210.         Dim selectedItem As String = ListBox1.SelectedItem.ToString()
  211.  
  212.         ' Check if folder path is already set
  213.         If folderPaths.ContainsKey(selectedItem) Then
  214.             ' Ask user if they want to change the default folder location
  215.             Dim result As DialogResult = MessageBox.Show($"Are you sure you want to change the default folder location for {selectedItem}?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
  216.  
  217.             If result = DialogResult.Yes Then
  218.                 ' Prompt user to select a new folder location
  219.                 Using folderBrowser As New FolderBrowserDialog()
  220.                     folderBrowser.Description = $"Select a folder for {selectedItem}"
  221.  
  222.                     If folderBrowser.ShowDialog() = DialogResult.OK Then
  223.                         ' Update folder path and save to file
  224.                         folderPaths(selectedItem) = folderBrowser.SelectedPath
  225.                         SavePaths()
  226.                         MessageBox.Show($"Default folder location updated for {selectedItem}")
  227.                     End If
  228.                 End Using
  229.             End If
  230.         Else
  231.             ' If folder path is not set, inform user to double-click on the item
  232.             MessageBox.Show($"Please double-click on {selectedItem} to set the default folder location.")
  233.         End If
  234.     End Sub
  235.  
  236.     Private Sub OpenFolder(ByVal folderPath As String)
  237.         If folderPath <> "" Then
  238.             Try
  239.                 Process.Start("explorer.exe", folderPath)
  240.             Catch ex As Exception
  241.                 MessageBox.Show($"Error opening folder: {ex.Message}")
  242.             End Try
  243.         End If
  244.     End Sub
  245.  
  246.     Private Sub SavePaths()
  247.         Dim lines As New List(Of String)
  248.         For Each key As String In folderPaths.Keys
  249.             lines.Add($"{key}={folderPaths(key)}")
  250.         Next
  251.         File.WriteAllLines("paths.txt", lines)
  252.     End Sub
  253.  
  254.     Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
  255.         Dim selectedItem As String = ListBox1.SelectedItem.ToString()
  256.  
  257.         ' Check if folder path is already set
  258.         If folderPaths.ContainsKey(selectedItem) Then
  259.             ' Open folder in File Explorer
  260.             Try
  261.                 Process.Start("explorer.exe", folderPaths(selectedItem))
  262.             Catch ex As Exception
  263.                 MessageBox.Show($"Error opening folder: {ex.Message}")
  264.             End Try
  265.             Return
  266.         End If
  267.  
  268.         ' If folder path is not set, ask user to select folder
  269.         Using folderBrowser As New FolderBrowserDialog()
  270.             folderBrowser.Description = $"Select a folder for {selectedItem}"
  271.  
  272.             If folderBrowser.ShowDialog() = DialogResult.OK Then
  273.                 ' Set folder path and open folder in File Explorer
  274.                 folderPaths(selectedItem) = folderBrowser.SelectedPath
  275.                 SavePaths()
  276.                 Try
  277.                     Process.Start("explorer.exe", folderBrowser.SelectedPath)
  278.                 Catch ex As Exception
  279.                     MessageBox.Show($"Error opening folder: {ex.Message}")
  280.                 End Try
  281.             End If
  282.         End Using
  283.     End Sub
  284.     Private Function GetSelectedRadioButton() As RadioButton
  285.         If RadioButton1.Checked Then
  286.             Return RadioButton1
  287.         ElseIf RadioButton2.Checked Then
  288.             Return RadioButton2
  289.         ElseIf RadioButton3.Checked Then
  290.             Return RadioButton3
  291.         Else
  292.             Return Nothing
  293.         End If
  294.     End Function
  295.     Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
  296.         Dim selectedRadioButton As RadioButton = GetSelectedRadioButton()
  297.  
  298.         If selectedRadioButton Is Nothing Then
  299.             MessageBox.Show("Please select a radio button")
  300.             Return
  301.         End If
  302.  
  303.         Dim filePath As String = Path.Combine("C:\Users\filip\Desktop\Harnesses", selectedRadioButton.Text & ".txt")
  304.         Dim textToWrite As String = TextBox1.Text & vbCrLf
  305.         File.AppendAllText(filePath, textToWrite)
  306.  
  307.         MessageBox.Show("Harness added to " & selectedRadioButton.Text)
  308.  
  309.         ListBox1.Items.Clear()
  310.         LoadFile(filePath)
  311.     End Sub
  312.  
  313.  
  314.     Private Sub SaveFiles()
  315.         ' Write the contents of the dictionary to the files.txt file
  316.         Dim lines As New List(Of String)()
  317.         For Each kvp As KeyValuePair(Of String, String) In fileDict
  318.             lines.Add(kvp.Key & "," & kvp.Value)
  319.         Next
  320.         File.WriteAllLines("files.txt", lines)
  321.     End Sub
  322.  
  323.     Private Sub LoadFiles()
  324.         ' Check if the files.txt file exists
  325.         If Not File.Exists("files.txt") Then
  326.             Return
  327.         End If
  328.  
  329.         ' Read the contents of the files.txt file and add the entries to the dictionary
  330.         Dim lines As String() = File.ReadAllLines("files.txt")
  331.         For Each line As String In lines
  332.             Dim parts As String() = line.Split(",")
  333.             If parts.Length = 2 Then
  334.                 fileDict(parts(0)) = parts(1)
  335.             End If
  336.         Next
  337.     End Sub
  338.     Private Sub LoadFile(filePath As String)
  339.         If Not File.Exists(filePath) Then
  340.             MessageBox.Show("File not found: " & filePath)
  341.             Return
  342.         End If
  343.         Dim lines As String() = File.ReadAllLines(filePath)
  344.         ListBox1.Items.Clear()
  345.         ListBox1.Items.AddRange(lines)
  346.     End Sub
  347.  
  348.     Private Sub LoadPaths()
  349.         If File.Exists("paths.txt") Then
  350.             Dim lines() As String = File.ReadAllLines("paths.txt")
  351.             For Each line As String In lines
  352.                 Dim parts() As String = line.Split("=")
  353.                 If parts.Length = 2 Then
  354.                     folderPaths(parts(0)) = parts(1)
  355.                 End If
  356.             Next
  357.         End If
  358.     End Sub
  359.     Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
  360.         ' Get the selected item from ListBox1
  361.         Dim selectedItem As String = ListBox1.SelectedItem.ToString()
  362.  
  363.         ' Check if file location is already set
  364.         If fileDict.ContainsKey(selectedItem) Then
  365.             ' Open file using stored location
  366.             Try
  367.                 Dim startInfo As New ProcessStartInfo()
  368.                 startInfo.FileName = fileDict(selectedItem)
  369.                 startInfo.UseShellExecute = True
  370.                 Process.Start(startInfo)
  371.             Catch ex As Exception
  372.                 MessageBox.Show("Error opening file: " & ex.Message)
  373.             End Try
  374.             Return
  375.         End If
  376.  
  377.         ' If file location is not set, ask user to select file
  378.         Dim selectedFile As String = ""
  379.  
  380.         Using openFileDialog As New OpenFileDialog()
  381.             openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
  382.             openFileDialog.Filter = "All Files (*.*)|*.*"
  383.             openFileDialog.FilterIndex = 1
  384.  
  385.             If openFileDialog.ShowDialog() = DialogResult.OK Then
  386.                 selectedFile = openFileDialog.FileName
  387.             End If
  388.         End Using
  389.  
  390.         ' Check if a file was selected
  391.         If String.IsNullOrEmpty(selectedFile) Then
  392.             MessageBox.Show("No file selected.")
  393.             Return
  394.         End If
  395.  
  396.         ' Set file location and save to file
  397.         fileDict(selectedItem) = selectedFile
  398.         SaveFiles()
  399.  
  400.         ' Open the file using the default application
  401.         Try
  402.             Dim startInfo As New ProcessStartInfo()
  403.             startInfo.FileName = selectedFile
  404.             startInfo.UseShellExecute = True
  405.             Process.Start(startInfo)
  406.         Catch ex As Exception
  407.             MessageBox.Show("Error opening file: " & ex.Message)
  408.         End Try
  409.     End Sub
  410.     Private Sub ListBox3_DragEnter(sender As Object, e As DragEventArgs) Handles ListBox3.DragEnter
  411.         If e.Data.GetDataPresent(DataFormats.FileDrop) Then
  412.             e.Effect = DragDropEffects.Copy
  413.         Else
  414.             e.Effect = DragDropEffects.None
  415.         End If
  416.     End Sub
  417.  
  418.     Private Sub ListBox3_DragDrop(sender As Object, e As DragEventArgs) Handles ListBox3.DragDrop
  419.         Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
  420.         For Each file In files
  421.             If Path.GetExtension(file) = ".xls" Or Path.GetExtension(file) = ".xlsx" Then
  422.                 ListBox3.Items.Add(file)
  423.             End If
  424.         Next
  425.     End Sub
  426.  
  427.     Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
  428.         If ListBox3.Items.Count < 1 Then
  429.             MessageBox.Show("Ubaci MACH liste!")
  430.             Return
  431.         End If
  432.  
  433.         ProgressBar1.Value = 0 ' Reset the progress bar
  434.         ProgressBar1.Maximum = ListBox3.Items.Count
  435.         ProgressBar1.Step = 1
  436.         ProgressBar1.Visible = True
  437.  
  438.  
  439.         Dim app As Excel.Application = New Excel.Application()
  440.         app.Visible = False ' Make the Excel application visible
  441.  
  442.         Dim wb As Excel.Workbook = app.Workbooks.Add()
  443.         Dim ws As Excel.Worksheet = wb.Worksheets(1)
  444.  
  445.         ' Copy header row
  446.         Dim headerRange As Excel.Range = ws.Range("A1:CD1")
  447.         headerRange.Value = "Header"
  448.  
  449.         Dim rowCount As Integer = 2 ' Start copying data from second row
  450.         Dim headerCopied As Boolean = False
  451.         For Each file As String In ListBox3.Items
  452.             If Path.GetFileName(file) <> "Import-Export.xls" Then
  453.                 If Not headerCopied Then
  454.                     Dim wbHeader As Excel.Workbook = app.Workbooks.Open(file)
  455.                     Dim wsHeader As Excel.Worksheet = wbHeader.Worksheets("Data")
  456.                     Dim headerToCopy As Excel.Range = wsHeader.Range("A1", "CD1")
  457.                     headerToCopy.Copy()
  458.                     headerRange.PasteSpecial(Excel.XlPasteType.xlPasteAll)
  459.                     wbHeader.Close(False)
  460.                     headerCopied = True
  461.                 End If
  462.                 Try
  463.                     Dim wbCopy As Excel.Workbook = app.Workbooks.Open(file)
  464.                     Dim wsCopy As Excel.Worksheet = wbCopy.Worksheets("Data")
  465.                     Dim rangeToCopy As Excel.Range = wsCopy.Range("A2", "CD" & wsCopy.Range("A2").End(Excel.XlDirection.xlDown).Row)
  466.  
  467.                     rangeToCopy.Copy()
  468.                     Dim nextRow As Excel.Range = ws.Range("A" & rowCount)
  469.                     nextRow.PasteSpecial(Excel.XlPasteType.xlPasteAll)
  470.  
  471.                     rowCount += rangeToCopy.Rows.Count
  472.  
  473.                     ' Copy a blank cell
  474.                     wsCopy.Range("A1").Copy()
  475.  
  476.                     wbCopy.Close(False)
  477.  
  478.                     ' Clear the clipboard
  479.                     app.CutCopyMode = False
  480.                 Catch comEx As System.Runtime.InteropServices.COMException
  481.                     MessageBox.Show("An error occurred while processing the file: " & file & vbCrLf & "Error: The 'Data' worksheet was not found in the file.")
  482.                     Return
  483.                 Catch ex As Exception
  484.                     MessageBox.Show("An error occurred while processing the file: " & file & vbCrLf & "Error message: " & ex.Message)
  485.                     Return
  486.                 End Try
  487.             End If
  488.             ProgressBar1.PerformStep()
  489.         Next
  490.         ProgressBar1.Value = 0
  491.         ProgressBar1.Visible = False
  492.         Dim columnRange As Excel.Range = ws.UsedRange.Columns
  493.         columnRange.AutoFit()
  494.         app.Visible = True
  495.         openExcelInstances.Add(app)
  496.         ExcelCheckTimer.Enabled = True
  497.         ExcelCheckTimer.Start()
  498.         AddHandler app.WorkbookBeforeClose, AddressOf WorkbookBeforeClose
  499.         excelApp = app
  500.         ' Release objects
  501.         ReleaseObject(ws)
  502.         ReleaseObject(wb)
  503.  
  504.  
  505.         ' Do not release the app object here since it will be released in the excelApp_WorkbookBeforeClose method
  506.     End Sub
  507.     Private Sub ExcelCheckTimer_Tick(sender As Object, e As EventArgs) Handles ExcelCheckTimer.Tick
  508.         If excelApp IsNot Nothing AndAlso IsNothing(excelApp.Workbooks) Then
  509.             ExcelCheckTimer.Stop()
  510.             ExcelCheckTimer.Enabled = False
  511.         Else
  512.             Return
  513.         End If
  514.  
  515.         ' If no Excel workbooks are open, quit the Excel application and release its resources
  516.         If excelApp IsNot Nothing Then
  517.             excelApp.Quit()
  518.             Marshal.ReleaseComObject(excelApp)
  519.             excelApp = Nothing
  520.         End If
  521.     End Sub
  522.  
  523.     Private Sub WorkbookBeforeClose(ByVal Wb As Excel.Workbook, ByRef Cancel As Boolean)
  524.         Dim app As Excel.Application = Wb.Application
  525.         Wb.Saved = True ' Mark the workbook as saved to prevent any save prompts
  526.         If app IsNot Nothing Then
  527.             app.Quit()
  528.             Marshal.ReleaseComObject(app)
  529.         End If
  530.     End Sub
  531.  
  532.     Private Sub App_WorkbookBeforeClose(ByVal Wb As Excel.Workbook, ByRef Cancel As Boolean)
  533.         Dim app As Excel.Application = Wb.Application
  534.         If app IsNot Nothing Then
  535.             app.Quit()
  536.             Marshal.ReleaseComObject(app)
  537.         End If
  538.     End Sub
  539.     Private Sub excelApp_WorkbookBeforeClose(ByVal Wb As Excel.Workbook, ByRef Cancel As Boolean) Handles excelApp.WorkbookBeforeClose
  540.         If excelApp IsNot Nothing Then
  541.             excelApp.Quit()
  542.             Marshal.ReleaseComObject(excelApp)
  543.             excelApp = Nothing
  544.         End If
  545.     End Sub
  546.  
  547.     Private Sub ReleaseObject(ByVal obj As Object)
  548.         Try
  549.             System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
  550.         Catch ex As Exception
  551.             Console.WriteLine("Error releasing object: " & ex.Message)
  552.         Finally
  553.             obj = Nothing
  554.             GC.Collect()
  555.         End Try
  556.     End Sub
  557.     Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
  558.         ListBox3.Items.Clear()
  559.         ProgressBar1.Value = 0
  560.     End Sub
  561.  
  562.     Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
  563.         If ListBox1.SelectedIndex <> -1 Then
  564.             Dim selectedItem As String = ListBox1.SelectedItem.ToString()
  565.  
  566.             If fileDict.ContainsKey(selectedItem) Then
  567.                 ListBox3.Items.Add(fileDict(selectedItem))
  568.             Else
  569.                 MessageBox.Show("Selected item does not have a file location.")
  570.             End If
  571.         Else
  572.             MessageBox.Show("Please select an item from the list.")
  573.         End If
  574.     End Sub
  575.  
  576.     Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
  577.         If ListBox1.SelectedIndex <> -1 Then
  578.             Dim selectedItem As String = ListBox1.SelectedItem.ToString()
  579.  
  580.             If fileDict.ContainsKey(selectedItem) Then
  581.                 Dim filePath As String = fileDict(selectedItem)
  582.                 If File.Exists(filePath) Then
  583.                     If Not excelDataCache.ContainsKey(selectedItem) Then
  584.                         excelDataCache(selectedItem) = ReadAndCacheExcelData(filePath)
  585.                     End If
  586.                     DisplayExcelData(excelDataCache(selectedItem), DataGridView1)
  587.                 Else
  588.                     MessageBox.Show("File has been removed or moved! Double-click on the harness to select a new KAB list.")
  589.                 End If
  590.  
  591.                 Label1.Visible = True
  592.                 Label1.Text = $"KAB lista za: {ListBox1.SelectedItem}"
  593.             Else
  594.                 MessageBox.Show("Ovaj harness nema KAB listu! Double-click on the harness to select a KAB list.")
  595.             End If
  596.         Else
  597.             MessageBox.Show("Please select an item from the list.")
  598.         End If
  599.     End Sub
  600.     Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
  601.         If ListBox1.SelectedIndex <> -1 Then
  602.             Dim selectedItem As String = ListBox1.SelectedItem.ToString()
  603.  
  604.             Dim openFileDialog As OpenFileDialog = New OpenFileDialog()
  605.             openFileDialog.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm"
  606.             openFileDialog.Title = "Select a KAB List Excel File"
  607.  
  608.             If openFileDialog.ShowDialog() = DialogResult.OK Then
  609.                 Dim newFilePath As String = openFileDialog.FileName
  610.  
  611.                 If fileDict.ContainsKey(selectedItem) Then
  612.                     fileDict(selectedItem) = newFilePath
  613.                 Else
  614.                     fileDict.Add(selectedItem, newFilePath)
  615.                 End If
  616.  
  617.                 MessageBox.Show("Location for " & selectedItem & " changed.")
  618.             End If
  619.         Else
  620.             MessageBox.Show("Please select a harness from the list.")
  621.         End If
  622.     End Sub
  623.  
  624. End Class
  625.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement