0

我有一个LinkLabel我在我的表单中动态添加的。我LinkLabel只会在CheckBox选中 a 时显示。我用它在我的表单LinkLabel添加了一个TextBox,用户最多只能添加5 个 TextBox。在达到最大值后,LinkLabel它将被禁用(但尚未添加到我的编码中)。

这是我目前使用的编码。

'This is my CheckBox
Private Sub CheckBoxOthers_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBoxOthers.CheckedChanged
    If CheckBoxOthers.Checked = True Then
        PanelOthers.Visible = True 'My TextBox and LinkLabel are inside a Panel

        Dim count As Integer = PanelOthers.Controls.OfType(Of Label)().ToList().Count
        Dim textbox As New TextBox()
        Dim linklabel1 As New LinkLabel()

        count = PanelOthers.Controls.OfType(Of TextBox)().ToList().Count
        textbox.Location = New System.Drawing.Point(15, 40 * count)
        textbox.Size = New System.Drawing.Size(172, 20)
        textbox.Name = "textbox_" & (count + 1)
        AddHandler textbox.TextChanged, AddressOf TextBox_Changed
        PanelOthers.Controls.Add(textbox)

        'Adding LinkLabel dynamically
        linklabel1.Name = "lnkAddSubj"
        linklabel1.Text = "Add Subject"
        linklabel1.Location = New Point(300, 3)
        AddHandler linklabel1.Click, AddressOf linklabel1_Click
        PanelOthers.Controls.Add(linklabel1)
    Else
        PanelOthers.Visible = False
        PanelOthers.Controls.Clear()
    End If
End Sub

这是我LinkLabel点击时添加 TextBox 的事件,最多 5 次,但我还没有添加编码来设置限制

Private Sub linklabel1_Click(sender As Object, e As EventArgs)
    Dim count As Integer = PanelOthers.Controls.OfType(Of Label)().ToList().Count
    Dim textbox As New TextBox()

    count = PanelOthers.Controls.OfType(Of TextBox)().ToList().Count
    textbox.Location = New System.Drawing.Point(15, 40 * count)
    textbox.Size = New System.Drawing.Size(172, 20)
    textbox.Name = "textbox_" & (count + 1)
    AddHandler textbox.TextChanged, AddressOf TextBox_Changed
    PanelOthers.Controls.Add(textbox)

    'So here after LinkLabel clicked 5 times then my LinkLabel will be disabled and I'm trying to do like this but it said LinkLabel1 is not declared
    LinkLabel1.Enabled = False
End Sub

如何使LinkLabel属性能够被设置?我能够编写它的 Click 事件,因为我在我的事件中为它添加了一个处理程序CheckBox

4

1 回答 1

0

这条线

'So here after LinkLabel clicked 5 times then my LinkLabel will be disabled and I'm trying to do like this but it said LinkLabel1 is not declared
LinkLabel1.Enabled = False

说这LinkLabel1永远不存在,因为您动态声明了您的 linkLabel1

'Adding LinkLabel dynamically
linklabel1.Name = "lnkAddSubj"
linklabel1.Text = "Add Subject"
linklabel1.Location = New Point(300, 3)
AddHandler linklabel1.Click, AddressOf linklabel1_Click
PanelOthers.Controls.Add(linklabel1)

在 中linklabel1_Click,您应该改用您的sender。投射到LinkLabel

Private Sub linklabel1_Click(sender As Object, e As EventArgs)
    Dim linkLbl As LinkLabel = sender 'do this
    Dim count As Integer = PanelOthers.Controls.OfType(Of Label)().ToList().Count
    Dim textbox As New TextBox()

    count = PanelOthers.Controls.OfType(Of TextBox)().ToList().Count
    textbox.Location = New System.Drawing.Point(15, 40 * count)
    textbox.Size = New System.Drawing.Size(172, 20)
    textbox.Name = "textbox_" & (count + 1)
    AddHandler textbox.TextChanged, AddressOf TextBox_Changed
    PanelOthers.Controls.Add(textbox)

    'So here after LinkLabel clicked 5 times then my LinkLabel will be disabled and I'm trying to do like this but it said LinkLabel1 is not declared
    'put if condition here to check if the textBox number already >= 5
    linkLbl.Enabled = False 'change this using the actual sender
End Sub    

CheckedChanged另外,作为一个附带问题:您是否需要在每次事件发生时多次动态添加链接标签?这对我来说似乎不是一个很好的做法。

于 2016-01-15T02:09:20.897 回答