提问:设置richtextbox控件中的SelStart属性值,但得不到正确结果的问题(源代码)

chenpeng1979 2008-12-07 11:01:20
本人使用串口发送字符数据,加载的文件使用richtextbox控件打开,每次从控件中取10个字符发送,一开始想到用Mid$()函数取字符,但是测试中发现一旦加载的文件很大,如2M容量,则程序运行会非常缓慢。后来改用richtextbox控件中的SelStart和SelLength属性设置取字符的位置和长度,运行速度得到了解决,但是在发送中却会丢失一些字符。经过单步运行发现当SelStart的位置刚好在字符char(13)时,SelStart会给自己减1,这样便丢失字符了,请问如何解决以上问题?
以下为该部分的源代码:
slen = Len(RichTextBox1.Text) ‘每次发送10个字符,计算发送的次数
times = Fix(slen / 10) + 1
For i = 1 To times

sendsize = 10 ’设置每次发送的个数

If times = 1 Then ‘如果总共只需发送1次,则每次发送个数为字符总个数
sendsize = slen
End If
If i = times And i > 1 Then ’如果当前为发送为最后1次,则当次的发送个数为剩余字符
sendsize = slen - 10 * (i - 1)
End If

ProgressBar1.Value = 100 * i / times
Label1.Caption = "已完成" & Str(Fix(100 * i / times)) & "%"

RichTextBox1.SelStart = (i - 1) * 10 ‘设置发送位置
RichTextBox1.SelLength = sendsize ’设置发送长度
buf = RichTextBox1.SelText

Comm1.InputMode = comInputModeText ‘通过串口发送
Comm1.Output = buf

Next i
...全文
182 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
SYSSZ 2008-12-07
  • 打赏
  • 举报
回复
如果是char(13)的问题用s=replace(RichTextBox1.Text),char(13),"")将char(13)删除再发送
SYSSZ 2008-12-07
  • 打赏
  • 举报
回复
RichTextBox1.SelStart = (i - 1) * 10 '设置发送位置
i=1,RichTextBox1.SelStart = 0,前9
i=2,RichTextBox1.SelStart = 10,10-19
i=3,RichTextBox1.SelStart = 20,20-29
…………
似乎应是1-10,11-20,21-30……
zzyong00 2008-12-07
  • 打赏
  • 举报
回复
同意老张
把chr(13)替换成"",接收到数据后,如果有chr(10),就替换成chr(13)+chr(10)
chenpeng1979 2008-12-07
  • 打赏
  • 举报
回复
4楼的老张,我在测试时是用多行文本实验的,如第1行写9个字符"MsgBox Ri"然后回车换行,程序中设置取10个字符,即RichTextBox1.SelStart = 0
RichTextBox1.SelLength = 10 ,但程序运行时则显示当前运算的SelLength=9,它会把回车字符忽略掉.
SYSSZ 2008-12-07
  • 打赏
  • 举报
回复
实测RichTextBox1.SelStart = 0是对的,正是从上一位开始,从下一位开始取,所以一楼的回贴作废.
另外,RichTextBox1.SelLength并不会长度减1
测试代码如下:
Private Sub Command1_Click()
For i = 1 To Len(RichTextBox1.Text)
s = Mid(RichTextBox1.Text, i, 1)
MsgBox Asc(s) & "/" & i
Next
MsgBox Len(RichTextBox1.Text)
End Sub

Private Sub Command2_Click()
RichTextBox1.SelStart = 10
RichTextBox1.SelLength = 10
MsgBox RichTextBox1.SelText
MsgBox Len(RichTextBox1.SelText)
End Sub
chenpeng1979 2008-12-07
  • 打赏
  • 举报
回复
说明:发送的文本是多行的,每行都有回车换行字符char(13)和char(10),这些字符也需要进行发送,不能漏

1,453

社区成员

发帖
与我相关
我的任务
社区描述
VB 控件
社区管理员
  • 控件
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧