Guest User

Untitled

a guest
Sep 19th, 2023
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. Imports System.Data.OleDb
  2.  
  3. Public Class Form5
  4. Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\mohjn\Desktop\test1\1.accdb")
  5. Dim prices(13) As Integer ' Declare an array to store prices
  6. Dim comboBoxList As New List(Of ComboBox) From {PRODUCT1, PRODUCT2, PRODUCT3, PRODUCT4, PRODUCT5, PRODUCT6, PRODUCT7, PRODUCT8, PRODUCT9, PRODUCT10, PRODUCT11, PRODUCT12, PRODUCT13, PRODUCT14}
  7.  
  8. Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  9. ' Create a list to store ComboBoxes
  10.  
  11. Try
  12. conn.Open()
  13. Dim query As String = "SELECT productname, price FROM Table1"
  14. Dim cmd As New OleDbCommand(query, conn)
  15. Dim reader As OleDbDataReader = cmd.ExecuteReader()
  16.  
  17. ' Loop through the ComboBox list and populate them with product names
  18. Dim index As Integer = 0 ' Initialize index for the price array
  19. For Each comboBox As ComboBox In comboBoxList
  20. While reader.Read()
  21. comboBox.Items.Add(reader("productname").ToString())
  22. prices(index) = Integer.Parse(reader("price").ToString()) ' Store price in the array
  23. index += 1 ' Increment the index for the next ComboBox
  24. End While
  25.  
  26. ' Add event handler for SelectedIndexChanged
  27. AddHandler comboBox.SelectedIndexChanged, AddressOf ComboBox_SelectedIndexChanged
  28. ' Move the reader back to the start
  29. reader.Close()
  30. reader = cmd.ExecuteReader()
  31. Next
  32. Catch ex As Exception
  33. MessageBox.Show("Error: " & ex.Message)
  34. Finally
  35. conn.Close()
  36. End Try
  37. End Sub
  38.  
  39. ' Event handler for ComboBox SelectedIndexChanged
  40. Private Sub ComboBox_SelectedIndexChanged(sender As Object, e As EventArgs)
  41. Dim selectedComboBox As ComboBox = DirectCast(sender, ComboBox)
  42. Dim selectedProductName As String = selectedComboBox.SelectedItem.ToString()
  43. Dim selectedIndex As Integer = comboBoxList.IndexOf(selectedComboBox) ' Get the index of the selected ComboBox
  44.  
  45. If selectedIndex >= 0 AndAlso selectedIndex < prices.Length Then
  46. MessageBox.Show("PRICE of " & selectedProductName & ": " & prices(selectedIndex).ToString())
  47. Else
  48. MessageBox.Show("PRICE not found for " & selectedProductName)
  49. End If
  50. End Sub
  51. End Class
  52.  
Advertisement
Add Comment
Please, Sign In to add comment