如何在vb.net

时间:2015-07-23 04:50:00

标签: vb.net object properties

我尝试动态添加和删除表单中的对象。我一直坚持如何获取要删除的对象的唯一标识符。

        'Collection of controls
        For Each ctl In Me.Controls
            'Get control type
            If TypeOf ctl Is Label Then
                'Get control name/index id/text or any property of current ctl
                'How do I continue from here?
                'Me.Controls.Remove(ctl)
            End If
        Next

提前感谢您提供解决方案/建议。 如果没问题,我想知道解决方案的解释。

如果你们需要知道我是如何动态添加对象的,那么它就是:

        For i = 1 To Spots
            Dim newLabel As New Label
            Dim newLoc As Integer = iLoc + (i * 30)

            With newLabel
                .Name = "lblSpot" & i
                .Text = "Spot " & i
                .Size = New Size(100, 20)
                .Location = New Point(3, newLoc)
            End With

            AddHandler Me.Load, AddressOf frmParking_Load
            Me.Controls.Add(newLabel)
        Next

2 个答案:

答案 0 :(得分:1)

您可以将<html> <head> <title>World Wind Helicopters Online Hour Sheet </title> <style> </style> </head> <body> <form action="data.php" method="post"> <label for="name">Name:</label> <select id="name" name="slname"> <option>Vince Lopardo</option> <option>Jay Ferguson</option> <option>Kent Pierce</option> <option>Jason Reschke</option> <option>Dan Rudert</option> <option>David Kerr</option> <option>Rick Dominy</option> </select> <br> <label for="date">Date:</label> <input id="date" type="date" name="date"/> <br> <label for="Location">Location:</label> <input id="Location" type="text" name="txtlocation" value="Enter Location Here"/> <br> <label for="Type">Type:</label> <select id="Type" name="sltype"> <option>USFS</option> <option>91</option> <option>133</option> <option>135</option> </select> <br> <label for="Helicopter">Helicopter:</label> <select id="Helicopter" name="rdheli"> <option>N510WW</option> <option>N610WW</option> <option>N205WW</option> <option>N350WW</option> </select> <label for="DTO">Duty Time On:</label> <select id="DTO" name="sldutyon"> <option>1:00</option> <option>2:00</option> <option>3:00</option> <option>4:00</option> <option>5:00</option> <option>6:00</option> <option>7:00</option> <option>8:00</option> <option>9:00</option> <option>10:00</option> <option>11:00</option> <option>12:00</option> <option>13:00</option> <option>14:00</option> <option>15:00</option> <option>16:00</option> <option>17:00</option> <option>18:00</option> <option>19:00</option> <option>20:00</option> <option>21:00</option> <option>22:00</option> <option>23:00</option> <option>0:00</option> </select> <label for="DTOFF">Duty Time Off:</label> <select id="DTOFF" name="sldutyoff"> <option>1:00</option> <option>2:00</option> <option>3:00</option> <option>4:00</option> <option>5:00</option> <option>6:00</option> <option>7:00</option> <option>8:00</option> <option>9:00</option> <option>10:00</option> <option>11:00</option> <option>12:00</option> <option>13:00</option> <option>14:00</option> <option>15:00</option> <option>16:00</option> <option>17:00</option> <option>18:00</option> <option>19:00</option> <option>20:00</option> <option>21:00</option> <option>22:00</option> <option>23:00</option> <option>0:00</option> </select> <br> <label for="FT">Flight Time:</label> <input id="FT" type="text" name="txtflighttime"/> <br> <label for="Duty Time">Duty Time:</label> <input id="Duty Time" type="text" name="txtdutytime"/> <br> <label for="Day Landing">Day Landings:</label> <input id="Day Landing" type="text" name="txtdayland"/> <br> <label for="Night Landing">Night Landings:</label> <input id="Night Landing" type="text" name="txtnightland"/> <br> <label for="Night Time">Night Time:</label> <input id="Night Time" type="text" name="txtnighttime"/> <br> <label for="Long Line">Long Line:</label> <input id="Long Line" type="text" name="txtlongline"/> <br> <label for="Tanker Loads">Tanker Loads:</label> <input id="Tanker Loads" type="text" name="txttankload"/> <br> <label for="Mountain">Mountain:</label> <input id="Mountain" type="text" name="txtmtn"/> <br> <label for="Comments">Comments:</label> <textarea id="Comments" name="txtarcomments" cols="25" rows="5"></textarea> <br> <input type="submit" value="submit"> </form> </body> </html>投射到ctl,然后使用Label查找要删除的控件

答案 1 :(得分:0)

不建议在更改集合时使用For Each,因此这里是您想要的代码

Dim i = 0
While i < Me.Controls.Count
    Dim c = Me.Controls(i)
    If TypeOf c Is Label Then
        Dim Lbl As Label = CType(c, Label)
        If Lbl.Name.Contains("lblSpot") Then
            Me.Controls.Remove(c)
        End If
    End If
End While