将 LinkLabel1.Enabled 属性设置为 false (vb.net)

Set LinkLabel1.Enabled properties to false (vb.net)

我在表单中动态添加了一个 LinkLabel。我的LinkLabel只会在勾选CheckBox的时候显示。我用这个 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 单击时添加文本框的事件,最多 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 事件中为它添加了一个 handler

这一行

'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 事件发生时,您是否需要多次动态添加 link 标签?这对我来说似乎不是一个很好的做法。