问题描述
我正在检查richtextBox中的一行是否为空。如果没有,它将进入下一行。 我正在使用的代码是:
Dim text as string="hello"
Dim lines = RichTextBox1.Lines
Dim upperBound = lines.GetUpperBound(0)
If lines(upperBound).Trim() = String.Empty Then
lines(upperBound) = text
RichTextBox1.Lines = lines
Else
RichTextBox1.AppendText(Environment.NewLine & text)
End If
哪个工作正常。但是刚才我发现,如果RichtextBox1完全为空,则此代码不会向其中添加文本。有什么办法可以解决这个小错误?谢谢
解决方法
尝试:
Dim text as string="hello"
Dim lines = RichTextBox1.Lines
Dim empty = {""}
If lines.Length = 0 Then lines = empty
Dim upperBound = lines.GetUpperBound(0)
If lines(upperBound).Trim() = String.Empty Then
lines(upperBound) = titolo
RichTextBox1.Lines = lines
Else
RichTextBox1.AppendText(Environment.NewLine & text)
End If
,
您可以尝试这个非常有效的系统。
这称为数组的LinQ,您可以查询数组中的空行, 如果计数大于0,则退出(或给出错误)
Dim text As String = "hello"
Dim lines = RichTextBox1.Lines
Dim qry = From n In lines
Where n = ""
Select n
If qry.Count > 0 Then
'you can give some error here
End If
,
看看这是否满足您的要求
Private Sub Button1_Click(sender As Object,e As EventArgs) Handles Button1.Click
Dim text As String = "hello"
If RichTextBox1.Text.Trim = "" Then
RichTextBox1.AppendText(titolo)
ElseIf RichTextBox1.Lines(RichTextBox1.Lines.GetUpperBound(0)).Length = 0 Then
RichTextBox1.AppendText(titolo)
ElseIf RichTextBox1.Lines(RichTextBox1.Lines.GetUpperBound(0)).Trim = "" Then
Dim lines = RichTextBox1.Lines
lines(lines.GetUpperBound(0)) = titolo
RichTextBox1.Lines = lines
Else
RichTextBox1.AppendText(Environment.NewLine & text)
End If
End Sub