我有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
事件,在点击时添加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
事件中为它添加了处理程序。
答案 0 :(得分: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
事件发生时,您是否需要多次动态添加链接标签?这似乎对我来说不是一个很好的做法。