在VB.NET中删除条目时阻止SelectedIndexChanged

时间:2014-10-05 19:26:58

标签: vb.net listbox

因此,当我单击删除按钮并在列表框中选择某人时,此代码将运行

'check if a member is selected
    If LBmembers.SelectedItems.Count = 0 Then
        MessageBox.Show("You have not selected a member to delete.")
    ElseIf LBmembers.SelectedItems.Count > 0 Then
        Dim i As String = LBmembers.SelectedItem
        Dim dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
        Dim dbSource = "Data Source= C:\Users\Gotrek65\Documents\Visual Studio 2013\Projects\Ice Mining Calculator\Ice Mining Calculator\members.mdb "
        Dim SqlQuery As String =
            "DELETE FROM tblMembers WHERE Member = @ID;"
        'remove member(i) from database
        Using con = New OleDb.OleDbConnection(dbProvider & dbSource)
            Using cmd = New OleDb.OleDbCommand(SqlQuery, con)
                con.Open()
                cmd.Parameters.AddWithValue("@ID", OleDb.OleDbType.Variant).Value = i
                cmd.ExecuteNonQuery()
                con.Close()
            End Using
        End Using
        TBtimestart.Text = ""
        TBtimeend.Text = ""
        TBtimecycle.Text = ""
        RBpro.Checked = False
        RBret.Checked = False
        RBcov.Checked = False
        RBskiff.Checked = False
        RBmack.Checked = False
        RBhulk.Checked = False
        LBmembers.Items.Remove(LBmembers.SelectedItem)
    End If



Private Sub LBmembers_SelectedIndexChanged(sender As Object, e As EventArgs) Handles LBmembers.SelectedIndexChanged
        'unfinished
        'if the selected idex changes then the individual stats should be retrieved from database and updated
        Dim i As String = LBmembers.SelectedItem
        Dim dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
        Dim dbSource = "Data Source= C:\Users\Gotrek65\Documents\Visual Studio 2013\Projects\Ice Mining Calculator\Ice Mining Calculator\members.mdb "
        Dim SqlQuery As String = "SELECT StartTime, EndTime, ShipCode, CycleTime FROM tblMembers WHERE Member = @ID;"
        Using con = New OleDb.OleDbConnection(dbProvider & dbSource)
            Using cmd = New OleDb.OleDbCommand(SqlQuery, con)
                con.Open()
                cmd.Parameters.AddWithValue("'@ID'", OleDb.OleDbType.VarWChar).Value = i
                'EXCEPTION when deleting data from tblMembers function "Parameter @ID has no default value." 
                Using reader = cmd.ExecuteReader()
                    If reader.Read() Then
                        'reader is  outputing "System.Data.OleDb.OleDbDataReader"
                        'but running the same query in access just yields 4 blank values...
                        TBtimestart.Text = reader.ToString(0)
                        TBtimeend.Text = reader.ToString(1)
                        Dim j = Convert.ToInt32(reader.ToString(2))
                        TBtimecycle.Text = reader.ToString(3)
                        If j = 1 Then
                            RBpro.Checked = True
                        ElseIf j = 2 Then
                            RBret.Checked = True
                        ElseIf j = 3 Then
                            RBcov.Checked = True
                        ElseIf j = 4 Then
                            RBskiff.Checked = True
                        ElseIf j = 5 Then
                            RBmack.Checked = True
                        ElseIf j = 6 Then
                            RBhulk.Checked = True
                        Else
                            RBpro.Checked = False
                            RBret.Checked = False
                            RBcov.Checked = False
                            RBskiff.Checked = False
                            RBmack.Checked = False
                            RBhulk.Checked = False
                            Exit Sub
                        End If
                    End If
                    con.Close()
                End Using
            End Using
        End Using
    End Sub

尝试从列表框中删除条目/成员时阻止SelectedIndexChanged运行。好奇如果有可能这样做,因为这只是开始变得愚蠢。

有关该计划的其他信息。

LBmembers(列表框)用于从所述列表更新数据库。此外,当您在列表框中选择一个条目(LBmembers)时,它应该转到数据库(访问DB tblMembers(列:成员(文本),StartTime(文本),EndTime(文本),ShipCode(数字),CycleTime(数字),应从所述数据库中提取WorkPercent(数字),Share(数字))并填充到其特定的框中。

如果您需要更多代码信息,请与我们联系。

1 个答案:

答案 0 :(得分:2)

在需要时忽略更改的简单标志:

Private ignoreChange As Boolean
Private Sub DeleteBtn_Click(...
    ignoreChange = True
    '... lots of code
    LBmembers.Items.Remove(LBmembers.SelectedItem)

    ignoreChange = False
End Sub

Private Sub LBmembers_SelectedIndexChanged(sender ...
   If ignoreChange Then Exit Sub
   ' lots more code

End Sub