高手请进:VB.NET程序中的bug
程序要求:richtextbox1的文字录入比较。现已实现两个文本框字符的对比,但在测试的时候,用不同的输入法如用万能五笔就没问题,但用如微软拼音之类的输入法就会有BUG出现。如连续输入一个词组,五笔就没问题,能够正确判断,但在拼音输入法里面,只把一个词组的最末一个字判断标识出来了(用红色表示)
代码如下:
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
Dim tagetstring As String = Me.intext.Text
Dim cursorpos As Integer = Me.RichTextBox1.SelectionStart
If String.IsNullOrEmpty(Me.RichTextBox1.Text) Then
Exit Sub
End If
For i As Integer = IIf(Me.RichTextBox1.SelectionStart - 1 >= 0, Me.RichTextBox1.SelectionStart - 1, 0) To Me.RichTextBox1.Text.Length - 1
Me.RichTextBox1.SelectionStart = i
RichTextBox1.SelectionLength = 1
If i >= tagetstring.Length Then
RichTextBox1.SelectionColor = Color.Red
Else
If Not Me.RichTextBox1.Text(i).Equals(tagetstring(i)) Then
RichTextBox1.SelectionColor = Color.Red
Else
RichTextBox1.SelectionColor = Color.Black
End If
End If
Label4.Text = RichTextBox1.TextLength
Next
time1.Text = RichTextBox1.SelectionStart
Me.RichTextBox1.SelectionStart = cursorpos
RichTextBox1.SelectionLength = 0
End Sub
可能是与回车键之类的有关吧?请各位指教指教 --------------------编程问答-------------------- 我随后又用五笔测试了一下,发现用五笔也有同样的问题出来,如果有很快地连续输入三个数字6,那么第一个6就没有用红色标识出来,但随后的两个6会是红色显示。那么就个问题就是与输入法无关,与键盘响应速度有关? --------------------编程问答-------------------- TextChanged,如果你是大量文本粘贴,估计也会有问题;
你试验一下键盘keyup事件里面进行响应——不过类似的操作最好不要用大的循环,都不会太快。
==================================================================
博客空间:http://blog.csdn.net/lovingkiss
资源下载:http://download.csdn.net/user/lovingkiss
优惠接单开发,组件控件定制开发,成品源代码批发
联系方式:Q64180940 全天在线
================================================================== --------------------编程问答-------------------- 在位输入的字符改变色调的时候,也会触发Changed..似乎.. --------------------编程问答-------------------- 帮顶!! --------------------编程问答-------------------- --------------------编程问答-------------------- 我用keypress事件试了一下,响应太慢了
应试不是程序响应事件的问题,本人估计是在比较字符的时候所选用的RichTextBox1.SelectionLength = 1
限制了字符比较的时候只能一个字符一个字符地比较 --------------------编程问答-------------------- 其实只要比较当前输入位置的字符就可以了的.
mid(RichTextBox1.text,RichTextBox1.selstart,1) = mid (label4.text ,RichTextBox1.selstart,1)
进行相应的修改.因为每次输入,都进行了比较
不过这种情况忽略了多选修改的情况 --------------------编程问答-------------------- 楼上的这位大哥大概也犯了同样的错误,如果一次输入多个字符,那么就只会比较其中的一个字符,而不是多个字符串 --------------------编程问答-------------------- 这个差不多了,你试试
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
Dim i As Integer = RichTextBox1.SelectionStart
Dim tagetstring As String = intext.Text
Dim str As String = RichTextBox1.Text
Dim toal As Integer
str = RichTextBox1.Text
RichTextBox1.SelectionStart = 0
RichTextBox1.SelectionLength = str.Length
RichTextBox1.SelectionColor = Color.Black
For toal = 1 To str.Length
If Mid(str, toal, 1) <> Mid(tagetstring, toal, 1) Then
RichTextBox1.SelectionStart = toal - 1
RichTextBox1.SelectionLength = 1
RichTextBox1.SelectionColor = Color.Red
End If
Next
RichTextBox1.SelectionStart = i
RichTextBox1.SelectionLength = 0
End Sub --------------------编程问答-------------------- 发错了 晕 --------------------编程问答-------------------- Dim tagetstring As String = intext.Text
Dim str As String = RichTextBox1.Text
Dim strsub As Integer = str.Length - strbuff.Length
rtbsellen = RichTextBox1.SelectionLength
rtbselstart = RichTextBox1.SelectionStart
RichTextBox1.SelectionStart = 0
RichTextBox1.SelectionLength = str.Length
RichTextBox1.SelectionColor = Color.Black
Dim i As Integer = 0
For i = 1 To str.Length
If Mid(str, i, 1) <> Mid(tagetstring, i, 1) Then
RichTextBox1.SelectionStart = i - 1
RichTextBox1.SelectionLength = 1
RichTextBox1.SelectionColor = Color.Red
End If
Next
RichTextBox1.SelectionStart = rtbselstart
RichTextBox1.SelectionLength = rtbsellen
strbuff = RichTextBox1.Text
补充:.NET技术 , VB.NET