2016-01-15 153 views
0

我有一個LinkLabel,我在表單中動態添加了這個屬性。只有當CheckBox被選中時,我的LinkLabel纔會顯示。我用這個LinkLabel在我的表格中添加一個TextBox和用戶只能添加5最大TextBox。達到它的最大值後,LinkLabel將爲已禁用(但尚未添加到我的編碼中)。將LinkLabel1.Enabled屬性設置爲false(vb.net)

這是我目前使用的編碼。

'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事件中爲它添加了一個處理程序

回答

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是根本不存在,因爲你動態

'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在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事件發生?這對我來說似乎不是一個很好的做法。

+0

你好,謝謝你的回覆。我只需要1個'LinkLabel'用於我的表單。如果你有一個好的和簡單的編碼,我應該善意地與我分享。我正在嘗試改進我的編碼。 – Emerald

+0

如果你只需要一個,最簡單的方法就是在你的'designer'中創建一個,而不是動態編碼,我想。通過使用Visual Studio的'designer'頁面來創建它,你甚至不需要編寫代碼來創建。你只需拖放。您只需要爲其「Click」事件創建事件處理程序代碼 - 我向您展示的代碼。 – Ian

+0

哦,是的,你真的是對的。因爲我正在考慮動態添加全部內容,所以我甚至不考慮這個小巧的智能解決方案。順便謝謝你:) – Emerald

相關問題