Visual Studio 2010 - 消息框弹出两次,为什么以及如何修复?

时间:2017-01-11 14:51:42

标签: vb.net messagebox

我正在编写一个小应用程序,允许用户根据图表编号,订单编号或产品ID进行搜索,以返回和更新三种类型的标签。设计的工作原理是有一个输入框,您可以在其中输入图表编号/订单编号/产品编号,并通过选择与上述三个输入选项对应的三个单选按钮中的一个来指定。输入框右侧有三个框,用于返回每种标签类型的标签ID,可以通过单击更新按钮直接编辑和更新这些框的内容。问题是返回消息在更新后弹出两次,这是一个麻烦,所以我想知道我的代码应该在哪里更改以解决此问题?我会粘贴下面的完整代码,以防问题出现在我没想到的地方:

Imports MySql.Data.MySqlClient

Public Class Form1

    Dim dbconn As New MySqlConnection
    Dim sql As String
    Dim mycommand As New MySqlCommand
    Dim mydata As MySqlDataReader

    Public Sub myConnection()

        dbconn = New MySqlConnection("Server=###;Database=###;Uid=###;Pwd=###;")
        Try
            dbconn.Open()
        Catch ex As Exception
            MsgBox("Connection Error: " & ex.Message.ToString)
        End Try


    End Sub

    Private Sub chartNoSub()

        myConnection()

        sql = "SELECT pltlbl, cuslbl, boxlbl from formats as a join ordtab as b join chart as c where a.prodid=b.cusled and concat(b.ordno, b.orditm)=concat(c.ord01, c.oitm01) and c.chart='" & inputBox.Text & "'"

        mycommand.Connection = dbconn
        mycommand.CommandText = sql

        mydata = mycommand.ExecuteReader
        While (mydata.Read())
            pltLBL.Text = mydata.Item("pltlbl")
            cusLBL.Text = mydata.Item("cuslbl")
            boxLBL.Text = mydata.Item("boxlbl")
        End While

        mydata.Close()
        dbconn.Close()

    End Sub

    Private Sub ordNoSub()

        myConnection()

        sql = "SELECT pltlbl, cuslbl, boxlbl from formats as a join ordtab as b where a.prodid=b.cusled and concat(b.ordno, '-', b.orditm)='" & inputBox.Text & "'"

        mycommand.Connection = dbconn
        mycommand.CommandText = sql

        mydata = mycommand.ExecuteReader
        While (mydata.Read())
            pltLBL.Text = mydata.Item("pltlbl")
            cusLBL.Text = mydata.Item("cuslbl")
            boxLBL.Text = mydata.Item("boxlbl")
        End While

        mydata.Close()
        dbconn.Close()

    End Sub

    Private Sub prodIDsub()

        myConnection()

        sql = "select pltlbl, cuslbl, boxlbl from formats where prodid='" & inputBox.Text & "'"

        mycommand.Connection = dbconn
        mycommand.CommandText = sql

        mydata = mycommand.ExecuteReader
        While (mydata.Read())
            pltLBL.Text = mydata.Item("pltlbl")
            cusLBL.Text = mydata.Item("cuslbl")
            boxLBL.Text = mydata.Item("boxlbl")
        End While

        mydata.Close()
        dbconn.Close()

    End Sub

    Private Sub Input_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles inputBox.TextChanged

        pltLBL.Clear()
        cusLBL.Clear()
        boxLBL.Clear()

        If (inputBox.Text <> "" And prodID.Checked = False And ordNo.Checked = False And chart.Checked = False) Then MsgBox("Please select an option below before inputting.")

        If prodID.Checked = True Then
            prodIDsub()
        End If
        If ordNo.Checked = True Then
            ordNoSub()
        End If
        If chart.Checked = True Then
            chartNoSub()
        End If

    End Sub

    Private Sub update_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles updateLBL.Click, updateLBL.Click

        myConnection()

        sql = "update formats as a inner join ordtab as b on a.prodid = b.cusled inner join chart as c on b.ordno=c.ord01 and b.orditm=c.oitm01 set a.pltlbl = '" & pltLBL.Text & "', a.cuslbl = '" & cusLBL.Text & "', a.boxlbl = '" & boxLBL.Text & "' where a.prodid = '" & inputBox.Text & "' or concat(b.ordno, '-', b.orditm)='" & inputBox.Text & "' or c.chart='" & inputBox.Text & "'"

        mycommand.Connection = dbconn
        mycommand.CommandText = sql
        mycommand.CommandType = CommandType.Text
        Dim rows = mycommand.ExecuteNonQuery()

        If rows <> 0 Then
            MessageBox.Show("Update successful!")
        Else
            MessageBox.Show("Check input.")
        End If

        dbconn.Close()

    End Sub


    Private Sub clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearLBL.Click

        prodID.Checked = False
        ordNo.Checked = False
        chart.Checked = False
        inputBox.Text() = ""

    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

只是将其标记为已回答;

@GlorinOakenfoot发现了一些令人难以置信的解决方案。

MsgBox中调用了Private Sub update_Click,但Handles每次点击都会发生两次事件,因此一切都会递归。