Button.Text.Contains Double Enter

时间:2015-01-15 14:59:07

标签: vb.net button contains

如果,我需要检查我的按钮以查看它是否有多行文字,然后我需要将该信息输入文本框。

到目前为止,我有:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click, Button5.Click, Button4.Click, Button3.Click, Button2.Click, Button1.Click

Dim btn = DirectCast(sender, Button)
Dim btntext = btn.Text
If btntext.Contains(vbcrlf)=false then
   Rtextbox.Text += btntext
End If
End Sub

然而,这仅检查是否有一个换行符,我需要检查两个

编辑换行符不在一行,中间有文字

3 个答案:

答案 0 :(得分:1)

统计他们:

Dim btntext = btn.Text
Dim numberOfLines = (btntext.length - btntext.Replace(vbCrLf, "").Length) \ vbCrLf.Length

if (numberOfLines > 1) then ....

答案 1 :(得分:1)

如果文本包含多个换行符,则第一个和最后一个换行符的位置将不同。

Sub Button1Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Dim btn = DirectCast(sender, Button)
    If btn.Text.IndexOf(vbCrLf) = btn.Text.LastIndexOf(vbCrLf) Then Rtextbox.Text += btn.Text
End Sub

答案 2 :(得分:0)

如果您想要专门检查两个连续的换行符,您只需将代码更改为:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click, Button5.Click, Button4.Click, Button3.Click, Button2.Click, Button1.Click   
    Dim btn = DirectCast(sender, Button)
    Dim btntext = btn.Text
    If btntext.Contains(vbcrlf & vbcrlf)=false then
       Rtextbox.Text += btntext
    End If
End Sub

请注意从vbcrlfvbcrlf & vbcrlf的更改。

如果要检查2个或更多换行符,可以使用正则表达式计算新换行符的数量:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click, Button5.Click, Button4.Click, Button3.Click, Button2.Click, Button1.Click   
    Dim btn = DirectCast(sender, Button)
    Dim btntext = btn.Text
    If New System.Text.RegularExpressions.Regex("\n").Matches(btntext).Count >= 2 then
       Rtextbox.Text += btntext
    End If
End Sub