1

此代码是表单上每个文本框的动态标签创建器。确保表单上所需的每个文本框都使用双击/单击链接到事件处理程序。我发现此代码有效,但如果有人有此代码的更简洁版本,请随时发布修订。

公开课形式1

Private Sub MyEventRoutine( _
   ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles TextBox2.DoubleClick, TextBox1.DoubleClick

    'get textbox
    Dim myTextBox As TextBox = sender

    'decl label associated with textbox
    Dim thelabeltochange As Control

    'decl label-name finder code
    Dim lblname As String
    lblname = myTextBox.Name & "label"

    If Me.Controls.Find(lblname, True).Length = 0 Then

        Dim lab As New Label

        lab.Name = myTextBox.Name & "label"
        lab.Size = New Size(40, 20)
        lab.Text = "Correct"
        lab.Location = New Point(myTextBox.Left + 89, myTextBox.Top)

        Me.Controls.Add(lab) 'this should newly created label to your form

        lab.BringToFront()

        Debug.Write("LABEL name >" & lblname & "< generated on first double click" & vbCrLf)

    Else

        For Each tmp As Control In Me.Controls

            If tmp.Name = myTextBox.Name & "label" Then

                Debug.Write("label exists and has name" & vbCrLf)

                thelabeltochange = tmp

                If thelabeltochange.Text = "Correct" Then

                    Debug.Write("label set as correct and is now wrong" & vbCrLf)
                    thelabeltochange.Text = "Wrong"
                    thelabeltochange.BringToFront()

                ElseIf thelabeltochange.Text = "Wrong" Then

                    Debug.Write("label set as wrong and is now To do" & vbCrLf)
                    thelabeltochange.Text = "To do"
                    thelabeltochange.BringToFront()

                ElseIf thelabeltochange.Text = "To do" Then

                    Debug.Write("label set as To do and is now deleted" & vbCrLf)
                    Me.Controls.Remove(thelabeltochange)

                End If

            End If

        Next

    End If



End Sub

结束类

4

0 回答 0