取消选中复选框时如何删除消息框中的记录?

时间:2014-01-15 16:41:22

标签: .net vb.net checkbox messagebox

我是编程的新手。我对此来源有疑问。取消选中该复选框时必须删除它。但它仍在那里。有解决方案吗抱歉英文不好。 :d

Public Class Form1
Dim message As String
Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter

End Sub

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
    If CheckBox1.CheckState = 1 Then
        message = message & CheckBox1.Text & vbNewLine
    End If
End Sub

Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
    If CheckBox2.CheckState = 1 Then
        message = message & CheckBox2.Text & vbNewLine
    End If
End Sub

Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
    If CheckBox3.CheckState = 1 Then
        message = message & CheckBox3.Text & vbNewLine
    End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    MsgBox(message)
End Sub

1 个答案:

答案 0 :(得分:0)

由于您尝试创建一系列已检查项目,因此您不需要这些CheckedChanged事件。只需使用StringBuilder在您需要时为您构建字符串:

Private Sub Button1_Click(sender As Object, e As EventArgs) _
                         Handles Button1.Click
  Dim message As New StringBuilder
  If CheckBox1.Checked Then
    message.AppendLine(CheckBox1.Text)
  End If
  If CheckBox2.Checked Then
    message.AppendLine(CheckBox2.Text)
  End If
  If CheckBox3.Checked Then
    message.AppendLine(CheckBox3.Text)
  End If
  MessageBox.Show(message.ToString)
End Sub