防止项目多次加载(For 循环查看数据库并为数据库中的每个项目创建按钮) [英] Prevent items from loading more than once (For loop looks at database and creates button for each item in database)

查看:23
本文介绍了防止项目多次加载(For 循环查看数据库并为数据库中的每个项目创建按钮)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下过程为我的数据库中的每个项目创建一个按钮:

I have the following procedure that creates a button for each item on my database:

Public Sub AddButtons()

            'Procedure creates a button for each category
        'stored in the database and adds them to the
        'main panel

        Dim ta As New ContactsAndInventoryDataSetTableAdapters.PRODUCT_CATEGORYTableAdapter
        Dim dt As DataTable = ta.GetData


        For Each row As DataRow In dt.Rows

            Dim btn As New btnCategoryTabs()

            btn.lblCategoryName.Name = DirectCast(row("Category_Name"), String)
            btn.lblCategoryName.Text = btn.lblCategoryName.Name
            btn.lblCategoryID.Text = CStr(row("Category_ID"))

            Using STREAM As New MemoryStream(DirectCast(row("image"), Byte()))
                btn.picPCategoryPicture.Image = Image.FromStream(STREAM)
            End Using

            'Add categories to the Panel
            frmManageStore.flpMainPanel.Controls.Add(btn)

        Next

End Sub

这很有效,但是,每次用户单击按钮时,我的面板都会复制我的项目,因为程序会再次运行.为了防止这种情况,我写了以下内容:

That works well, however, every time user clicks the button my panel duplicates my items since the procedure runs again. To prevent that I wrote the following:

    Public Sub removeButtons()

    'This procedure is used to remove buttons from the panel
    'after each category is clicked--

       Dim btnList As List(Of btnCategoryTabs) = frmManageStore.flpMainPanel.Controls.OfType(Of btnCategoryTabs).ToList()

    'Remove these Buttons
    For Each btn As btnCategoryTabs In btnList

        btn.Dispose()

    Next

End Sub

我点击按钮事件如下:

    Private Sub btnCategories_Click(sender As Object, e As EventArgs) Handles btnCategories.Click

    PublicSubs.removeButtons()
    PublicSubs.addCategories()

End Sub

关于上一个问题 一些用户建议不要使用 .Dispose 所以我的问题是,如果用户多次单击按钮,是否有更好的方法防止我的项目重复.下图显示了当用户多次单击类别"按钮并且 PublicSubs.removeButtons() 未运行时会发生什么.

On a previous question some of the users recommended not using .Dispose So my question is, is there a better way keep my items from duplicating if the user clicks on the button more than once. The picture below shows what happens when the user clicks the Category button more than once and the PublicSubs.removeButtons() does not run.

推荐答案

只需调用 frmManageStore.flpMainPanel.Controls.Clear() 即可删除面板上的所有按钮.

Simply call frmManageStore.flpMainPanel.Controls.Clear() in order to remove all the buttons from the panel.

请注意,btn.Dispose() 确实从 Controls 集合中删除了按钮;但这并不明显,可以被认为是Dispose 方法.因此最好明确删除按钮.这会让你的意图更清晰.

Note that btn.Dispose() indeed removes the button from the Controls collection; however this is not obvious and can be considered of being a side effect of the Dispose method. Therefore preferably remove the button explicitly. This makes your intention clearer.

这篇关于防止项目多次加载(For 循环查看数据库并为数据库中的每个项目创建按钮)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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