0

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.

Emerald
  • 864
  • 1
  • 14
  • 37

1 Answers1

0

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.

Ian
  • 30,182
  • 19
  • 69
  • 107
  • Hello, thanks for your reply. I need 1 `LinkLabel` only for my form. If you got a good and simple coding that I should use kindly to share with me. I am trying to improve my coding as well. – Emerald Jan 15 '16 at 02:19
  • If you only need one, the easiest is to create one in your `designer`, not by dynamic coding, I think. By making use of `designer` page of Visual studio to create it, you don't even need to code to create. You just drag and drop. You only need to create event handler code for its `Click` event - the one I have shown you. – Ian Jan 15 '16 at 02:23
  • Oh yes yes you are really correct. Because I am thinking of adding all by dynamically so that's why I don't even think about this small smart solution. By the way thank you so much :) – Emerald Jan 15 '16 at 02:29