我正在尝试从文本框中删除一行但不知道确切的行

时间:2013-08-26 20:43:36

标签: vb.net winforms visual-studio-2010

单击复选框时,我在文本框中添加一行。如果单击该复选框并想要删除该项目,我该怎么办:

Private Sub cbAddress_SelectedValueChanged(sender As Object, e As System.EventArgs) Handles cbAddress.SelectedValueChanged
if cbAddress.checked = true then
    dim thetext as string = txtTextbox.text
    dim theItem as string = "test"
    txtTextbox.text = thetext & Environment.NewLine & theItem
else
    ' i'm try to delete the line.
   ' I've tried to txtTextbox.text.Remove(blah, blah)
end if 

我应该在文本框中跟踪添加到哪一行,以便在取消选中时将其删除,还是有更好的方式?

2 个答案:

答案 0 :(得分:0)

替换Environment.NewLine

txtTextbox.Text = txtTextbox.Text.Replace(System.Environment.NewLine, "")

答案 1 :(得分:0)

您可以使用String.Replace方法:

Private Sub cbAddress_SelectedValueChanged(sender As Object, e As System.EventArgs) Handles cbAddress.CheckedChanged
    Dim theItem As String = "test"
    If cbAddress.Checked = True Then
        Dim thetext As String = txtTextbox.Text
        txtTextbox.Text = thetext & Environment.NewLine & theItem
    Else
        txtTextbox.Text = txtTextbox.Text.Replace(Environment.NewLine & theItem, "")
    End If
End Sub