WinForms: Is there a concept of associating a label with a textbox?

前端 未结 7 864
梦谈多话
梦谈多话 2020-12-18 18:21

I\'m using Visual Studio 2010 with C#. Is there a concept in Windows Forms development of somehow linking a label with is text box? Something so that they move together as

7条回答
  •  萌比男神i
    2020-12-18 19:14

    You can use extension methods to do it, follow example:

    Private associatedLabels As New Dictionary(Of Control, Label)    
    
    Public Sub AssociateLabel(ByVal control As Control, ByVal label As Label)
        If (Not associatedLabels.ContainsKey(control)) Then
            associatedLabels.Add(control, label)
        End If
    End Sub
    
    
    Public Function GetAssociatedLabel(ByVal control As Control) As Label
        If (associatedLabels.ContainsKey(control)) Then
            Return associatedLabels(control)
        Else
            Throw New Exception("There is no associated label")
        End If
    End Function
    

提交回复
热议问题