在VB里面,用“打开”按钮实现打开文本文件,并将文本文件的内容显示到窗体上的textbox中,我的代码写到下面就不知道该怎么办了,试过很多种方法,还是没能实现将内容显示到文本框中。麻烦各位高手帮忙看下,不胜感激!
Private Sub openBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles openBtn.Click
OpenFileDialog1.Filter = "文本文件 (*.txt)|*.txt|(*.step)|*.step|所有文件|*.*" '打开文件
OpenFileDialog1.ShowDialog()
'希望高手能帮忙把文本内容写入textbox中的代码告知下
End Sub
Private Sub openBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles openBtn.Click
OpenFileDialog1.Filter = "文本文件 (*.txt)|*.txt|(*.step)|*.step|所有文件|*.*" '打开文件
OpenFileDialog1.ShowDialog()
'希望高手能帮忙把文本内容写入textbox中的代码告知下
End Sub
Dim InputData
Do While Not EOF(1) ' 检查文件尾。
Line Input #1, InputData ' 读入一行数据。
OutputText.text = OutputText.text & InputData & Chr(13) & Chr(10) ' 在立即窗口中显示。
Loop
Close #1 ' 关闭文件。
Dim r As System.IO.StreamReader
r = New System.IO.StreamReader("c:\a.txt", System.Text.Encoding.GetEncoding("GB2312"))
Dim str As String = ""
Do While Not r.EndOfStream
str = str & r.ReadLine
Loop
Me.RichTextBox1.Text = str
r = New System.IO.StreamReader("c:\a.txt", System.Text.Encoding.GetEncoding("GB2312"))
Dim str As String = ""
Do While Not r.EndOfStream
'str = str & r.ReadLine Me.RichTextBox1.AppendText(r.ReadLine)
Me.RichTextBox1.AppendText(vbNewLine)
Loop
用个打开对话框,用变量把“C:\A.TXT”换掉就是了。不太会换,汗.....能给我换下吗?谢谢了!