如何在VB.NET中删除面板 [英] How do I remove a panel in VB.NET

查看:50
本文介绍了如何在VB.NET中删除面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧这似乎是一个简单的问题,但我似乎无法真正让它发挥作用,并想知道是否有人可以帮助我,或者指出我正确的方向,因为我已经寻找答案但已经绘制空白。



我所拥有的是一个流程布局面板,作为我表单上的主面板。流布局面板的原因是我可以轻松地向其添加其他面板而无需指定位置点(实际上可以充分利用我的Java布局管理器知识)。



添加到流程布局面板的这些其他面板由实际用户使用标有添加面板的按钮动态添加。按钮名称称为btnAddPanel。添加这些面板时,此面板中还包含一个按钮(btndeleteButton),当用户单击时,该按钮将删除该按钮所在的面板(按钮的父面板)。



问题是我似乎无法让删除按钮工作。我有时可以将最后一个已添加的面板移除,但即使这有时也会被击中。



当用户单击其中一个上的删除按钮时面板,父面板需要删除。



这是我已经编写的代码,注释掉的部分是我尝试过的一些代码但是没工作。为了让它发挥作用,我已经尝试了很多次,但每次尝试都失败了。



有什么建议吗?谢谢:)



Okay this may seem like an easy question but I can''t seem to actually get it to work, and wonder if anybody can help me, or point me in the right direction as I have searched for an answer already but have drawn up blanks.

What I have is a flow layout panel, as the main panel on my Form. The reason for a flow layout panel is so I can easily add other panels to it without specifying location points (actually putting my Java layout manager knowledge to good use).

These other panels that are added to the flow layout panel are added dynamically by the actual user with a button marked "Add Panel". The button name is called btnAddPanel. When these panels are added, a button is also included in this panel (btndeleteButton) which when the user clicks is suppose to delete the panel which the button resides on (the parent panel of the button).

The problem is I can''t seem to get the delete button to work. I sometimes can get the last panel that has been added to be removed, but even this is sometimes hit and miss.

When the user clicks the delete button on one of the panels, the parent panel needs to be removed.

This is the code I have already and the commented out parts are some left over pieces of code I have tried but failed to work. I have tried a lot more times to get it to work, but every attempt I have failed.

Any suggestions? Thanks :)

Public Class Form1

    Dim btndeleteButton As Button
    Dim dynamicPanel As Panel

    Private Sub btnAddPanel_Click(sender As System.Object, e As System.EventArgs) Handles btnAddPanel.Click

        dynamicPanel = New Panel
        dynamicPanel.Size = New System.Drawing.Size(357, 100)
        dynamicPanel.BackColor = Color.LightBlue

        btndeleteButton = New Button
        btndeleteButton.Width = 100
        btndeleteButton.Text = "Delete"
        AddHandler btndeleteButton.Click, AddressOf btndeleteButton_Click


        dynamicPanel.Controls.Add(btndeleteButton)

        FlowLayoutPanel1.Controls.Add(dynamicPanel)

    End Sub

    Private Sub btndeleteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        ''FlowLayoutPanel1().Controls.Remove(dynamicPanel)
        ''btndeleteButton.Parent.Dispose()

    End Sub

  
End Class

推荐答案

由于共享实例字段 Form1.dynamicPanel ,代码只是脏的。很肮脏。仔细看看。



当面板和删除按钮只有一个时,一切都很好,但是如果你尝试以蝴蝶方式添加和删除面板,那么共享的效果这个变量发挥作用。您认为删除了具有您点击的按钮的面板,但实际上使用了面板的另一个实例,最后添加到该行中的那个:

The code is just dirty, due to sharing of the instance field Form1.dynamicPanel. Very dirty. Just look carefully.

When the panel and deleting button is just one, everything is good, but if you try adding and removing panels in butterfly manner, the effect of sharing this variable comes into play. You think that you delete the panel having the button you are clicking on, but in fact the other instance of a panel is used, the one which was last added in this line:
FlowLayoutPanel1.Controls.Add(dynamicPanel) ' bad!

< br $> b $ b

你现在能看到问题吗?



所以,要解决这个问题,你应该避免传递参考到共享领域;删除字段 Form1.dynamicPanel 。您需要通过事件处理程序的参数传递事件句柄使用的数据。做这样的事情:



使用标签 property:



Can you see the problem now?

So, to resolve the problem, you should avoid passing reference to the shared field; remove the field Form1.dynamicPanel at all. You need to pass data used by an event handle through the arguments of the event handler. Do something like this:

Add a back reference to a panel to the instance of deleting button, using its Tag property:

' Dim btndeleteButton As Button ' remove form class fields
' Dim dynamicPanel As Panel ' remove form class fields

Private Sub btnAddPanel_Click(sender As System.Object, e As System.EventArgs) Handles btnAddPanel.Click

    Dim btndeleteButton As Button ' make former field a stack variable
    Dim dynamicPanel As Panel ' this one, too

    dynamicPanel = New Panel
    dynamicPanel.Size = New System.Drawing.Size(357, 100)
    dynamicPanel.BackColor = Color.LightBlue

    btndeleteButton = New Button

    btndeleteButton.Tag = dynamicPanel ' button will remember
                                       ' the panel to delete

    ' ... the rest of your code
    ' ...

End Sub





在处理程序代码中,键入-cast sender Button 。它也将是点击按钮的实例;现在,从按钮中提取 Panel 的实例:取 Button.Tag 并将其输入面板。删除这个面板的实例。



问题解决了。



另一个解决方案就是这样:全部完成在处理程序的代码中;通过类型转换 sender 提取 Button 的实例。取 Button.Parent 并输入 Control Panel :您可以确定在删除之前,此 Control 肯定是必需的 Panel 。删除它。



也可以。







还有一个注意事项:永远不要使用 btnAddPanel_Click Form1 等名称自动生成的名称。给一切语义名称。这就是您获得Visual Studio重构引擎的原因。没有下划线或数字(Label1,Label2等)。除了维护和可读性问题之外,您还违反了(良好的)Microsoft命名约定。是的,微软自动生成的代码也违反了它们,那么呢?这些名称并非旨在保留;你应该重命名它们,使它们在语义上合理。



-SA



In the handler code, type-cast sender to Button. It will also be the instance of the button clicked; now, extract an instance of the Panel from the button: takes Button.Tag and type-cast it to Panel. Remove this instance of a panel.

Problem solved.

Another solution is this: do it all in the code of the handler; extract an instance of Button by type-casting sender. Take Button.Parent and type-cast this Control to Panel: you can be sure that before deleting, this Control is certainly the required Panel. Remove it.

It will work, too.



One more note: never ever use names like btnAddPanel_Click, Form1 and other auto-generated names. Give everything some semantic names. This is why you are given the Visual Studio refactorization engine. No underscores or numerics ("Label1", "Label2" and the like). In addition to the problems of maintenance and readability, you are violating (good) Microsoft naming conventions. Yes, Microsoft auto-generated code violates them, too, so what? These names are not designed to be kept; you are supposed to rename them all, to make them semantically sensible.

—SA


这篇关于如何在VB.NET中删除面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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