VB.NET问题:怎么让For...Next...循环加速?
我设计了一个程序,在RichTextBox
的TextChanged
事件,通过For...Next...
枚举每个字符,将其中的尖括号(<>
)、双引号(""
)中的文字换成不同的颜色。
这是我的代码:
Public Class FrmEdit
Private Sub CodeTextBox_TextChanged(sender As Object, e As EventArgs) Handles CodeTextBox.TextChanged
If CodeTextBox.Text = "" Then Exit Sub
CodeTextBox.ForeColor = Color.Black
CodeTextBox.SelectAll() : CodeTextBox.SelectionFont = Me.Font
TipProgressBar.Visible = True
TipProgressBar.Maximum = CodeTextBox.TextLength - 1
TipProgressBar.Minimum = 0
TipProgressBar.Value = 0
Dim AngleBracketsOn As Boolean = False
Dim DoubleQuotesOn As Boolean = False
For i As Integer = 0 To CodeTextBox.TextLength - 1
CodeTextBox.SelectionStart = i
CodeTextBox.SelectionLength = 1
If CodeTextBox.SelectedText = "<" Then
AngleBracketsOn = True
CodeTextBox.SelectionLength = CodeTextBox.TextLength - i
CodeTextBox.SelectionColor = Color.Purple
End If
If CodeTextBox.SelectedText = ">" Then
If AngleBracketsOn = True Then
AngleBracketsOn = False
CodeTextBox.SelectionStart = i + 1
CodeTextBox.SelectionLength = CodeTextBox.TextLength - i - 1
CodeTextBox.SelectionColor = Color.Black
End If
End If
If CodeTextBox.SelectedText = """" Then
If DoubleQuotesOn = False Then
DoubleQuotesOn = True
CodeTextBox.SelectionLength = CodeTextBox.TextLength - i
CodeTextBox.SelectionColor = Color.Blue
Else
DoubleQuotesOn = False
CodeTextBox.SelectionStart = i + 1
CodeTextBox.SelectionLength = CodeTextBox.TextLength - i - 1
CodeTextBox.SelectionColor = Color.Black
End If
End If
TipProgressBar.Value = i
Next
CodeTextBox.SelectionLength = 0
CodeTextBox.SelectionStart = CodeTextBox.TextLength
TipProgressBar.Visible = False
End Sub
End Class
我复制了一段百度翻译网页(http://fanyi.baidu.com/#en/zh/)的源代码,卡了9分钟才完成。不知道有没有更好的办法,可以加快程序的速度?求大神帮忙!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)