簡體   English   中英

如何從另一個表單 vb.net 獲取 SelectedIndex

[英]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

您應該對這段代碼進行大量重構。 從這個開始,它會幫助你到達你想去的地方:

  • 放棄過時的匈牙利符號。
  • 讓您的工作選擇器表單公開兩個屬性:SelectedJobName 和 SelectedJobID
  • 給作業選擇器表單一個更有意義的名稱,因為它不只返回作業名稱。 例如JobPickerForm
  • 實例化作業選擇器表單的一個實例: Using f As New frmSelectJobName
  • 從屬性中獲取選定的值:例如selectedJobID = f.SelectedJobID
  • 驗證和連接檢查之前詢問用戶是否真的要添加 Employee。
  • 您正在使用 SQL Server。 使用 SqlClient 命名空間中的對象而不是 OleDb。
  • 將您在數據庫中的員工 ID 字段聲明為IDENTITY字段,這樣您就不必使用hacky MAX(ID) + 1
  • 使用存儲過程進行查詢。 當前方法對 SQL 注入開放。 雖然您仍然可以使用內聯 SQL,但我發現自己更喜歡使用 SP。
  • 返回添加到員工表的員工的SCOPE_IDENTITY 這為您提供了添加的員工的主鍵值。
  • Using構造實例化所有 SqlClient 對象。 在本地聲明並打開 SqlConnection 到 Sub/Function,而不是其他地方。
  • 無論 Form_Load() 中有什么,都將其放入單獨的 Sub 中,然后調用該 sub。 您正在傳遞一個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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM