Button.Text.Contains 双击输入

Button.Text.Contains Double Enter

我需要检查我的按钮,看它是否有多行文本,如果有,没有,那么我需要将该信息输入到文本框中。

到目前为止我有:

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

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

EDIT换行不在一行,中间有文字

如果你想专门检查两个连续的换行符,你可以把你的代码改成这样:

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

数一数:

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

if (numberOfLines > 1) then ....

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

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