-1

我对编程语言非常陌生,并且正在我大学的第一堂编程课上。对于一个项目,我们正在创建一个非常简单的项目,用户输入单词然后程序将它们组合在一起。这是我知道的第一天,但​​我正在努力争取额外的功劳和“添加一个复选框以允许在输入文本更改时更新标签”。

我有一个程序,当您在单独的文本框中输入两个单独的单词时,它会单独显示每个单词,然后将这两个单词组合在底部。我们的教授希望我在底部添加一个复选框选项,当点击 make 它时,当用户键入两个单词时,它们会自动显示而不点击显示按钮。

我知道这很容易,但任何帮助将不胜感激。

感谢您的帮助。

Public Class form1
    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblLeft.Click

    End Sub

    Private Sub lbl2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblRight.Click

    End Sub

    Private Sub txtBoxLeft_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBoxLeft.TextChanged

    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim strTxtBoxLeft As String
        strTxtBoxLeft = txtBoxLeft.Text
        Dim strTxtBoxRight As String
        strTxtBoxRight = txtBoxRight.Text

        lblLeft.Text = strTxtBoxLeft

        lblRight.Text = strTxtBoxRight

        lblCombo.Text = strTxtBoxLeft & " " & strTxtBoxRight
    End Sub

    Private Sub chkbox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkbox.CheckedChanged
        If chkbox.CheckState = False Then

        End If
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
End Class
4

1 回答 1

1

试试这个:

Public Class Form1
    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        combineText()
    End Sub

    Private Sub chkbox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkbox.CheckedChanged
        If chkbox.CheckState = False Then
            lblCombo.Text = ""
        End If
    End Sub

    Private Sub txtBox_TextChanged(sender As Object, e As EventArgs) Handles txtBoxRight.TextChanged, txtBoxLeft.TextChanged
        If chkbox.CheckState Then
            combineText()
        End If
    End Sub

    Private Sub combineText()
        lblCombo.Text = txtBoxLeft.Text & " " & txtBoxRight.Text
    End Sub
End Class

注意:我已将您的逻辑合并为仅从文本框中获取文本。还有一个处理程序可以处理任一文本框中的文本更改。最后,有一个方法可以处理将两个文本框的值组合在一起的方法,如果选中了复选框,或者如果用户单击更新按钮,则通过文本更改事件。此外,如果用户取消选中该复选框,则会清除结果。然后,当用户单击更新时,它将再次显示组合文本。

于 2013-09-17T18:31:24.540 回答