I have a LinkLabel that I added dynamically in my form. My LinkLabel will only be displayed when a CheckBox is checked. I used this LinkLabel to add a TextBox in my form and user can only add 5 maximum TextBox. After it reach to it's maximum then the LinkLabel will be disabled (but not added to my coding yet).
Here is my coding that I currently use.
'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
Here is my LinkLabel event to add TextBox when clicked, 5 times max, but I haven't add the coding to set the limit yet
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
How to make the LinkLabel properties be able to be set? I am able to write it's Click event because I add a handler for it inside my CheckBox event.
This line
'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
Says that LinkLabel1 is never existed because you declare your linkLabel1 dynamically
'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)
In the linklabel1_Click, you should use your sender instead. Cast it to 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
Also, as a side question: do you need to add your link label dynamically multiple times everytime CheckedChanged event occurs? This seems to be not a very good practice to me.