我有一个Windows窗体,其中包含2个组合框和一个列表框。每个显示项目取决于在每个项目中选择的内容。
对于列表框,multiselect已启用并正在工作,但我希望能够多次选择列表框中的项目。允许用户多次选择同一项目。
我正在使用VS2015
这是我当前的代码:
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Public Class MainHome
Dim con As SqlConnection = New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=\\Galactic\TSC_TIME_MATRIX$\DEV\TMX.mdf;Integrated Security=True")
'Dim constr As String
Dim querystring As String
' "Please Select" doesn't work well in the ListBox, I added it as an option
Private Shared Function GetData(ByVal sql As String, insertPleaseSelect As Boolean) As DataTable
Dim constr As String = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=\\Galactic\TSC_TIME_MATRIX$\DEV\TMX.mdf;Integrated Security=True"
Using con As SqlConnection = New SqlConnection(constr)
Using sda As SqlDataAdapter = New SqlDataAdapter(sql, con)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
If insertPleaseSelect Then
Dim row As DataRow = dt.NewRow()
row(0) = 1
row(1) = "Please Select"
dt.Rows.InsertAt(row, 0)
End If
Return dt
End Using
End Using
End Function
Private Sub MainHome_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
ComboBox1.DataSource = GetData("SELECT [SizeId], [SizeName] FROM [Size]", True)
ComboBox1.DisplayMember = "SizeName"
ComboBox1.ValueMember = "SizeId"
ComboBox2.Enabled = False
ListBox1.SelectionMode = SelectionMode.MultiSimple ' allow multi-select
End Sub
Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
ComboBox2.DataSource = Nothing
ComboBox2.Enabled = False
If ComboBox1.SelectedValue.ToString() <> "0" Then
Dim sql As String = String.Format("SELECT [DetailLevelId], [DetailLevelName] FROM [DetailLevel] WHERE [SizeId] = {0}", ComboBox1.SelectedValue)
ComboBox2.DataSource = GetData(sql, True)
ComboBox2.DisplayMember = "DetailLevelName"
ComboBox2.ValueMember = "DetailLevelId"
ComboBox2.Enabled = True
End If
End Sub
Private Sub ComboBox2_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox2.SelectionChangeCommitted
ListBox1.DataSource = Nothing
ListBox1.Enabled = False
If ComboBox2.SelectedValue.ToString() <> "0" Then
Dim sql As String = String.Format("SELECT [OperationsId], [OperationsName], [OperationsTime] FROM [Operations] WHERE [DetailLevelId] = {0}", ComboBox2.SelectedValue)
ListBox1.DataSource = GetData(sql, False)
ListBox1.DisplayMember = "OperationsName" ' changed this from ValueMember to DisplayMember
ListBox1.ValueMember = "OperationsTime"
ListBox1.Enabled = True
ListBox1.ClearSelected() ' Every time the ListBox is populated, clear it
End If
End Sub
' Added this handler to respond to user input, not programmatic selection changes
Private Sub ListBox1_Click(sender As Object, e As EventArgs) Handles ListBox1.Click
' Here is the sum
'TotalMinutes
Dim TotalMinutes = ListBox1.SelectedItems.OfType(Of DataRowView).Sum(Function(o) CType(o("OperationsTime"), Double))
lbTotalMins.Text = TotalMinutes
'Total Hours
Dim TotalHours = ListBox1.SelectedItems.OfType(Of DataRowView).Sum(Function(o) CType(o("OperationsTime"), Double)) / 60.0
lbTotalHours.Text = Format(TotalHours, "0.00")
'Total Days
Dim TotalDays = ListBox1.SelectedItems.OfType(Of DataRowView).Sum(Function(o) CType(o("OperationsTime"), Double)) / 1440.0
lbTotalDays.Text = Format(TotalDays, "0.0")
'Oven TIme
Dim CureOvenSum = ListBox1.SelectedItems.OfType(Of DataRowView).Where(Function(o) o("OperationsName").ToString() = "CURE/OVEN").Sum(Function(o) CType(o("OperationsTime"), Double)) / 60
Dim postCureOvenSum = ListBox1.SelectedItems.OfType(Of DataRowView).Where(Function(o) o("OperationsName").ToString() = "POST CURE/OVEN").Sum(Function(o) CType(o("OperationsTime"), Double)) / 60
lbTotalOven.Text = Format(postCureOvenSum + CureOvenSum, "0.00")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Using sda As SqlDataAdapter = New SqlDataAdapter(sql, con)
' Dim constr As String
' Dim con As SqlConnection = New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\hartj\Documents\visual studio 2015\Projects\TIMEMATRIX2.0\TIMEMATRIX2.0\TMX.mdf;Integrated Security=True")
Try
con.Open()
querystring = "INSERT INTO ArchiveData(DATE,PART_NUMBER,JOB_NUMBER,REV,IMS_DATE,ASSEMBLY_SIZE,DETAIL_LEVEL,OPERATIONS,USER_NAME,TOTAL_TIME,TOTAL_HOURS,TOTAL_DAYS,TOTAL_OVEN,NOTES)VALUES(' " & MainStart.tbDateToday.Text & " ',' " & tbPartNum.Text & " ',' " & tbJobNum.Text & " ',' " & tbRev.Text & " ',' " & tbIMS.Text & " ',' " & ComboBox1.Text & " ',' " & ComboBox2.Text & " ',' " & ListBox1.Text & " ',' " & MainStart.tbUserName.Text & " ',' " & lbTotalMins.Text & " ',' " & lbTotalHours.Text & " ',' " & lbTotalDays.Text & " ',' " & lbTotalOven.Text & " ',' " & rtbNOTES.Text & " ')"
Dim Comm As SqlCommand = New SqlCommand(querystring, con)
Comm.ExecuteNonQuery()
MsgBox("Data Succesfully Saved!", MsgBoxStyle.Information, "add")
Catch ex As Exception
MessageBox.Show(ex.Message)
con.Close()
End Try
End Sub
谢谢!