EditingControlShowing事件多次触发 [英] EditingControlShowing events firing multiple times

查看:520
本文介绍了EditingControlShowing事件多次触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在VB.Net 2008中有一个DGV连接到Access DB表。 DGV不是只读,但是除了一个包含一个组合框的只读列外,它们是完整的。组合框允许用户选择该特定行的结果,然后根据组合框中选择的项目,程序将预先计算的值复制到利润列中。然后,用户点击保存按钮,数据库更新(目前通过XSD中的SQL方法)。

I have a DGV in VB.Net 2008 connected to an Access DB table. The DGV is not Read Only, but is full of read-only columns except for one, which contains a combo box. The combo box allows the user to select an outcome for that particular row, and then the program copies in a pre calculated value into the "Profit" column depending upon the item selected in the combobox. Then the user hits the Save button and the DB updates (currently via SQL methods in the XSD).

到目前为止还不够。

这是代码。

Private Sub DGUserBets_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DGUserBets.EditingControlShowing

    Dim combo As ComboBox = CType(e.Control, ComboBox)

    If (combo IsNot Nothing) Then

         // Remove an existing event-handler, if present, to avoid 
         // adding multiple handlers when the editing control is reused.
        RemoveHandler combo.SelectedIndexChanged, _
            New EventHandler(AddressOf DGUBStake_SelectedIndexChanged)

        // Add the event handler. 
        AddHandler combo.SelectedIndexChanged, _
            New EventHandler(AddressOf DGUBStake_SelectedIndexChanged)

    End If

End Sub


Private Sub DGUBStake_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)

    Dim myStatus As ComboBox = CType(sender, ComboBox)

    Dim row = DGUserBets.CurrentRow

    Select Case myStatus.SelectedIndex
        Case 0
            row.Cells("DGUBProfit").Value = 0
            // pending. no action
        Case 1
            row.Cells("DGUBProfit").Value = row.Cells("DGUBIfWin").Value
            // win
        Case 2
            // loses
            row.Cells("DGUBProfit").Value = row.Cells("DGUBIfLose").Value
        Case 3
            // void
            row.Cells("DGUBProfit").Value = 0
    End Select


End Sub

我遇到的问题是,如果用户从组合框中选择了所需的结果,但没有按Enter键,并且简单地将鼠标移到不同的组合框上,再次选择不同行的结果,第一个事件处理程序没有断开连接,因此事件多次触发。这会导致各种默认MsgBox错误,并在用户尝试将所有更改提交给DB /退出程序等时出现问题。

The problem I have is that it would seem that if a user selects the desired outcome from the combobox but does NOT hit Enter, and simply mouses on to a different combobox to again select the outcome for a different row, the first eventhandler is not disconnected and thus the events fire multiple times. This then causes various default MsgBox errors and brings up problems when the user tries to commit all changes to the DB/exit program etc etc.

我需要做什么?我需要.EndEdit在适当的地方强制行保存更改吗?我应该在哪里叫这个?

What do I need to do? Do I need to .EndEdit somewhere appropriate to force the row to save the changes? And where should I call this?

谢谢。

推荐答案

快速浏览代码会出现以下问题:
如果您在删除现有的EventHandler时创建新的EventHandler,那么它是相同的?

A quick glance at the code brings up this question: If you create a new EventHandler when removing the existing one is it the same one?

这篇关于EditingControlShowing事件多次触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆