![](/img/trans.png)
[英]How do I get the SelectedItem or SelectedIndex of ListView in vb.net?
[英]How to get SelectedIndex from another form vb.net
我正在嘗試同時添加員工和他們的工作。 我有一個 ID 表,我試圖將兩條記錄都插入其中。 我有一個 FIND 按鈕,可以在另一個填充有工作的表單上打開一個組合框。 一旦做出選擇,它就會填充第一個表單上的 txtJobName 文本框。 像它應該的那樣填充作業名稱,但我無法拉出 SelectedIndex 以插入到 TJobEmployees 表中。
'Variables
Dim strSelect As String
Dim strInsert As String
Dim strFirstName As String = ""
Dim strLastName As String = ""
Dim strJob As String = frmSelectJobName.strSelectedJob
Dim intJobID As Integer
Dim cmdSelect As OleDb.OleDbCommand
Dim cmdInsert As OleDb.OleDbCommand
Dim dr As OleDb.OleDbDataReader
Dim intNextHighestRecordID As Integer
Dim intRowsAffected As Integer
Dim result As DialogResult
strFirstName = txtFirstName.Text
strLastName = txtLastName.Text
strJob = txtJobName.Text
intJobID = CInt(frmSelectJobName.lstJobs.SelectedValue)
If Validation() = True Then
If OpenDatabaseConnectionSQLServer() = False Then
' No, warn the user ...
MessageBox.Show(Me, "Database connection error." & vbNewLine &
"The application will now close.",
Me.Text + " Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
' and close the form/application
Me.Close()
End If
' always ask before adding!!!!
result = MessageBox.Show("Are you sure you want to Add Employee: Job-" & txtJobName.Text & "?", "Confirm Submission", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
' this will figure out which button was selected. Cancel and No does nothing, Yes will allow insert
Select Case result
Case DialogResult.Cancel
MessageBox.Show("Action Canceled")
Case DialogResult.No
MessageBox.Show("Action Canceled")
Case DialogResult.Yes
strSelect = "SELECT MAX(intEmployeeID) + 1 AS intNextHighestRecordID " &
" FROM TEmployees"
' Execute command
cmdSelect = New OleDb.OleDbCommand(strSelect, m_conAdministrator)
dr = cmdSelect.ExecuteReader
' Read result( highest ID )
dr.Read()
' Null? (empty table)
If dr.IsDBNull(0) = True Then
' Yes, start numbering at 1
intNextHighestRecordID = 1
Else
' No, get the next highest ID
intNextHighestRecordID = CInt(dr.Item(0))
End If
' add the child record
strInsert = "Insert into TEmployees (intEmployeeID, strFirstName, strLastName)" &
" Values (" & intNextHighestRecordID & ",'" & strFirstName & "'," & "'" & strLastName & "')"
cmdInsert = New OleDb.OleDbCommand(strInsert, m_conAdministrator)
intRowsAffected = cmdInsert.ExecuteNonQuery()
'add the parent record
strInsert = "Insert into TJobEmployees (intJobID, intEmployeeID)" &
" Values (" & intJobID & ",'" & intNextHighestRecordID & "')"
' Insert the record(s)
cmdInsert = New OleDb.OleDbCommand(strInsert, m_conAdministrator)
intRowsAffected = cmdInsert.ExecuteNonQuery()
If intRowsAffected > 0 Then
MessageBox.Show("Job has been added")
Me.Close()
End If
End Select
CloseDatabaseConnection()
Form1_Load(sender, e)
End If
您應該對這段代碼進行大量重構。 從這個開始,它會幫助你到達你想去的地方:
JobPickerForm
。Using f As New frmSelectJobName
selectedJobID = f.SelectedJobID
IDENTITY
字段,這樣您就不必使用hacky MAX(ID) + 1
。SCOPE_IDENTITY
。 這為您提供了添加的員工的主鍵值。Using
構造實例化所有 SqlClient 對象。 在本地聲明並打開 SqlConnection 到 Sub/Function,而不是其他地方。sender
,並且從您的代碼片段中,我不知道此代碼處於什么事件中。執行這些操作,如果您的問題仍然存在,請使用重構后的代碼更新您的原始帖子,我將繼續通過編輯我的答案來提供幫助。
這是重構的 JobPickerForm 屬性的示例。
Public Class JobPickerForm
Public ReadOnly Property SelectedJobID As Integer = 0
Public ReadOnly Property SelectedJobName As String = String.Empty
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
' Whatever code you have that loads the JobsListBox.....
End Sub
Private Sub SelectButtonClickEvent(sender As Object, e As EventArgs) Handles SelectButton.Click
_SelectedJobID = CInt(JobsListBox.ValueMember)
_SelectedJobName = JobsListBox.DisplayMember.ToString
End Sub
End Class
以下是使用新 JobPickerForm 的示例:
Dim selectedJobName As String = String.Empty
Dim selectedJobID As Integer = 0
Using f As New JobPickerForm
f.ShowDialog()
selectedJobID = f.SelectedJobID
selectedJobName = f.SelectedJobName
End Using
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.