vb.net动态链接标签

时间:2018-11-21 18:51:55

标签: vb.net linklabel

如何确定在Visual Basic中单击了哪个动态链接?我有一些根据数据集动态创建的LinkLabel,我想打开一个包含该数据集信息的新表单,但是我需要知道如何根据单击的链接加载表单。下面的代码... //此函数根据数据表中的行创建链接标签

Sub DynamicLabels()
    Dim i As Integer
    Dim x As Integer = 14
    Dim y As Integer = 50
    Dim tp As TabPage = tabControl1.TabPages(1)
    If db.HasConnection() Then
        If db.SQLDS IsNot Nothing Then
            db.SQLDS.Clear()
        End If
        db.RunQuery("SELECT c.courseSubj AS Subject, c.courseNum AS CourseNum, r.className AS ClassName, t.tName AS Professor
                    FROM course c, classRoom r, teacher t, classroom_student u, student s
                    WHERE c.courseId=r.course_id AND t.teacherId=r.teacher_id AND s.studentId=u.student_id AND u.classroom_id=r.classId AND s.sUsername='" & Login.Usr.Text & "' ")
        For i = 0 To db.SQLDS.Tables(0).Rows.Count - 1
            ReDim MyLabel(db.SQLDS.Tables(0).Rows.Count)
            y += 50
            With MyLabel(i)
                MyLabel(i) = New LinkLabel()
                MyLabel(i).Name = "linklabel" & i.ToString
                MyLabel(i).Location = New Point(x, y)
                MyLabel(i).Size = New Size(700, 40)
                MyLabel(i).Font = New Font("Microsoft Sans Serif", 14)
                MyLabel(i).Text = String.Format(CType(db.SQLDS.Tables(0).Rows(i).Item("Subject"), String) & " " & CType(db.SQLDS.Tables(0).Rows(i).Item("CourseNum"), String) & " " & CType(db.SQLDS.Tables(0).Rows(i).Item("ClassName"), String) & ": " & CType(db.SQLDS.Tables(0).Rows(i).Item("Professor"), String))
                AddHandler MyLabel(i).LinkClicked, AddressOf label_LinkClicked
            End With
            tp.Controls.Add(MyLabel(i))
        Next
    End If
End Sub

我想加载一个新表单,其中包含来自数据集的一些信息。

1 个答案:

答案 0 :(得分:0)

我刚刚添加了一个标签进行演示。您添加的每个Label都具有相同的Event过程(AddressOf部分),但是Add Handler直接引用新的标签变量(mylabel.Click)

Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim mylabel As New Label With {
            .Text = "New Label",
            .Name = "newLabel",
            .Location = New Point(400, 100)
        }
        AddHandler mylabel.Click, AddressOf aLabel_Click
        Controls.Add(mylabel)
End Sub

这是您的事件过程

Private Sub aLabel_Click(sender As Object, e As EventArgs)
        Dim EventLabel As Label = DirectCast(sender, Label)
        Dim LabelText As String = EventLabel.Text
        'or any other property of the label you need to use
        MessageBox.Show(LabelText)
End Sub