单击另一个按钮后删除按钮

时间:2018-11-07 00:44:28

标签: mysql vb.net

我一直在使用VB.NET开发POS系统,并且需要一些帮助来解决我所面临的问题的解决方案。
基本上,我使用一个函数,该函数使用SQL表来查找与项目相关的信息,这些信息应对应于Button形式的某个类别。

本质上,所有类别都是使用Form表自动创建到SQL上的。

我想找到一种方法,通过该方法,我可以显示已分配给数据库中每个类别的Buttons形式的项目,而这已经在代码中完成了。
唯一的问题是按钮形式的旧项目不会被删除,而是会留在屏幕上,从而导致无法看到类别Button所调用的较新项目的问题。

我有一个下面的代码示例:

Public Sub Form13_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim point As Integer
    Dim point1 As Integer
    point = 20
    point1 = 100

    Label2.Text = Form12.selected_value
    Dim connection As New MySqlConnection
    connection.ConnectionString = ("host=localhost;user=root;password=;database=pos;")
    connection.Open()
    Dim command As New MySqlCommand("select * from categories", connection)
    Dim reader As MySqlDataReader
    reader = command.ExecuteReader
    Dim perm As String
    While reader.Read()
        Dim button As New Button

        Dim value As String = reader.GetString("category_name")
        perm = reader.GetString("category_name")
        button.Name = value
        button.Text = value
        button.Height = 50
        button.Width = 190
        button.Font = New Font("arial", 11)
        button.Location = New Point(point, point1)
        Me.Controls.Add(button)
        point1 = point1 + 50

        'showing items based on the button that the user clicks
        AddHandler button.Click,
        Sub()
            reader.Close()
            Dim command1 As New MySqlCommand("select * from items where category=@name", connection)
            command1.Parameters.AddWithValue("@name", value)
            reader = command1.ExecuteReader

            'new look for the items to be converted into physical buttons
            Dim point2 As Integer
            Dim point3 As Integer

            point2 = 235
            point3 = 100
            Dim counter As Integer
            counter = 1
            While reader.Read()
                Dim item As String
                Dim category_type As String
                category_type = reader.GetString("category")
                item = reader.GetString("name")

                Dim button1 As New Button
                button1.Name = item
                button1.Text = item
                button1.Height = 50
                button1.Width = 160
                button1.Font = New Font("Arial", 11)
                button1.Location = New Point(point2, point3)
                Me.Controls.Add(button1)
                counter = counter + 1
                Me.Refresh()

                'figure out how to remove buttons when they are not neeeded aka find a way to check when a button is being clicked
                If point2 > 700 Then
                    point2 = 235
                    point3 = point3 + 55
                Else
                    point2 = point2 + 168
                End If
            End While
        End Sub
    End While
End Sub

请帮助我,因为我已经在这个问题上困扰了很长时间。
我认为可以通过找出用户何时单击其他类别来解决该问题,以便可以使用button1.Dispose来处理表单上放置的先前项目。

包含项的表的图像已附在下面:

image of table containing items

0 个答案:

没有答案