发 帖  
原厂入驻New

Read、ReadText、Write 和 WriteText 方法范例 (VB)

2009-1-8 10:24:13  3043
分享
<p><font face="Verdana">Read、ReadText、Write 和 WriteText 方法范例 (VB)</font></p><font face="Verdana">
<p><br/>本范例演示将文本框的内容读入文本 Stream 和二进制 Stream 的方式。显示的其他属性和方法包括 Position、Size、Charset 和 SetEOS。</p>
<p>Public Sub ReadX()<br/>&nbsp;&nbsp;&nbsp; 'Declare variables<br/>&nbsp;&nbsp;&nbsp; Dim objStream As Stream<br/>&nbsp;&nbsp;&nbsp; Dim varA As Variant<br/>&nbsp;&nbsp;&nbsp; Dim bytA() As Byte<br/>&nbsp;&nbsp;&nbsp; Dim i As Integer<br/>&nbsp;&nbsp;&nbsp; Dim strBytes As String<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; 'Instantiate and Open Stream<br/>&nbsp;&nbsp;&nbsp; Set objStream = New Stream<br/>&nbsp;&nbsp;&nbsp; objStream.Open<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; 'Write the text content of a textbox to the stream<br/>&nbsp;&nbsp;&nbsp; objStream.WriteText Text1.Text<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; 'display the text contents and size of the stream<br/>&nbsp;&nbsp;&nbsp; objStream.Position = 0<br/>&nbsp;&nbsp;&nbsp; debug.Print "Default text:"<br/>&nbsp;&nbsp;&nbsp; Debug.Print objStream.ReadText<br/>&nbsp;&nbsp;&nbsp; Debug.Print objStream.Size<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; 'Switch character set and display<br/>&nbsp;&nbsp;&nbsp; objStream.Position = 0<br/>&nbsp;&nbsp;&nbsp; objStream.Charset = "Windows-1252"<br/>&nbsp;&nbsp;&nbsp; Debug.Print "New Charset text:"<br/>&nbsp;&nbsp;&nbsp; Debug.Print objStream.ReadText<br/>&nbsp;&nbsp;&nbsp; Debug.Print objStream.Size<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; 'Switch to a binary stream and display<br/>&nbsp;&nbsp;&nbsp; objStream.Position = 0<br/>&nbsp;&nbsp;&nbsp; objStream.Type = adTypeBinary<br/>&nbsp;&nbsp;&nbsp; Debug.Print "Binary:"<br/>&nbsp;&nbsp;&nbsp; Debug.Print objStream.Read<br/>&nbsp;&nbsp;&nbsp; Debug.Print objStream.Size<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; 'Load an array of bytes with the text box text<br/>&nbsp;&nbsp;&nbsp; ReDim bytA(Len(Text1.Text))<br/>&nbsp;&nbsp;&nbsp; For i = 1 To Len(Text1.Text)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bytA(i - 1) = CByte(Asc(Mid(Text1.Text, i, 1)))<br/>&nbsp;&nbsp;&nbsp; Next<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; 'Write the buffer to the binary stream and display<br/>&nbsp;&nbsp;&nbsp; objStream.Position = 0<br/>&nbsp;&nbsp;&nbsp; objStream.Write bytA()<br/>&nbsp;&nbsp;&nbsp; objStream.SetEOS<br/>&nbsp;&nbsp;&nbsp; objStream.Position = 0<br/>&nbsp;&nbsp;&nbsp; Debug.Print "Binary after Write:"<br/>&nbsp;&nbsp;&nbsp; Debug.Print objStream.Read<br/>&nbsp;&nbsp;&nbsp; Debug.Print objStream.Size<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; 'Switch back to a text stream and display<br/>&nbsp;&nbsp;&nbsp; Debug.Print "Translated back:"<br/>&nbsp;&nbsp;&nbsp; objStream.Position = 0<br/>&nbsp;&nbsp;&nbsp; objStream.Type = adTypeText<br/>&nbsp;&nbsp;&nbsp; Debug.Print objStream.ReadText<br/>&nbsp;&nbsp;&nbsp; Debug.Print objStream.Size<br/>End Sub</p>
<p></font>&nbsp;</p>
0
2009-1-8 10:24:13   评论 分享淘帖
1 个讨论
dddddddddddddd!
2009-2-1 22:35:29 评论

举报

只有小组成员才能发言,加入小组>>

135个成员聚集在这个小组

加入小组

创建小组步骤

关闭

站长推荐 上一条 /7 下一条

快速回复 返回顶部 返回列表