vb.net - 从 txt 文件读取并在文本框中写入
问题描述
有人可以帮我解决这个问题,我不知道下一步该怎么做
在计算机的任何位置给出一个文本文件,它包含 15 个可以重复的不同整数 制作程序,使一个数字的多次重复不会单独打印,但每个数字只打印一次,旁边写在哪里他看上去
Imports System.IO
Public Class form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim povratnaVrijednost As DialogResult
Dim nazivDatoteke As String
Try
OpenFileDialog1.AddExtension = True
OpenFileDialog1.Multiselect = False
OpenFileDialog1.Filter = "Tekst datoteke (*.txt)|*.txt;"
povratnaVrijednost = OpenFileDialog1.ShowDialog()
If povratnaVrijednost = Windows.Forms.DialogResult.OK Then
If OpenFileDialog1.CheckFileExists = True And
OpenFileDialog1.CheckPathExists = True Then
nazivDatoteke = OpenFileDialog1.FileName
TextBox1.Text = nazivDatoteke
Dim citac As New StreamReader(nazivDatoteke)
Dim redTeksta As String = ""
Do
redTeksta = citac.ReadLine()
If Not redTeksta Is Nothing Then
RichTextBox1.Text = RichTextBox1.Text + redTeksta
End If
Loop Until redTeksta Is Nothing
citac.Close()
End If
End If
Catch ex As Exception
MsgBox("Greska prilikom otvaranja" + ex.StackTrace.ToString)
End Try
End Sub
End Class
解决方案
要求 #1:
在计算机的任何位置给出一个文本文件,它包含 15 个可以重复的不同整数
这个要求意味着几个其他的要求。首先,您需要阅读文本文件。其次,您需要将值解析为数字(可能是整数)。
您可以使用 OpenFileDialog (文档) 并指定它只能接受文本文件:
Using browseFileDialog = New OpenFileDialog()
With browseFileDialog
.Filter = "*.txt|*.txt"
If (.ShowDialog() = DialogResult.Ok) Then
'.FileName will be the text file that the user picked
End If
End With
End Using
要读取文本文件,假设您需要每一行,请使用 File.ReadAllLines 方法(文档):
Using browseFileDialog = New OpenFileDialog()
With browseFileDialog
.Filter = "*.txt|*.txt"
If (.ShowDialog() = DialogResult.Ok) Then
Dim lines = IO.File.ReadAllLines(.FileName)
End If
End With
End Using
要解析值,请使用 Array.ConvertAll 方法(文档)并在谓词内部使用 Integer.Parse 方法(文档):
Using browseFileDialog = New OpenFileDialog()
With browseFileDialog
.Filter = "*.txt|*.txt"
If (.ShowDialog() = DialogResult.Ok) Then
Dim lines = IO.File.ReadAllLines(.FileName)
Dim values = Array.ConvertAll(lines, Function(line) Integer.Parse(line))
End If
End With
End Using
请记住,如果您想验证这些行是否都是有效数字,而不是假设它们是有效数字,则此步骤会有所不同。但是,您没有在原始帖子中指定该要求,所以我不包括如何做到这一点。
要求 #2:
使程序不单独打印一个数字的多次重复,而是每个数字只打印一次
您可以使用 Random.Next 方法(文档)生成随机值。请务必声明一次随机对象的新实例,以便只设置一次种子。要对值进行随机排序,请使用 OrderBy 方法(文档)在谓词中传递随机值:
Private ReadOnly _random As New Random()
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using browseFileDialog = New OpenFileDialog()
With browseFileDialog
.Filter = "*.txt|*.txt"
If (.ShowDialog() = DialogResult.Ok) Then
Dim lines = IO.File.ReadAllLines(.FileName)
Dim values = Array.ConvertAll(lines, Function(line) Integer.Parse(line))
Dim randomlyOrderedValues = values.OrderBy(Function(value) _random.Next())
RichTextBox1.Text = String.Join(", ", randomlyOrderedValues.ToArray())
End If
End With
End Using
End Sub
推荐阅读
- sql - 从 SQL 查询中测量 ResultSet 的时间性能
- amazon-web-services - S3 批处理操作 - 可以避免 lambda 调用之间的长时间延迟吗?
- ruby-on-rails - 在另一个别名中使用 Ransack 别名
- reactjs - 如何在不使用 React 保持滚动位置的情况下转到另一个页面
- reactjs - 反应变量未更新
- android-ndk - 如何为 android 编译铬版本的 bsdiff
- javascript - 如何在函数中返回从 promise 解析的值?
- webrtc - 如何使用 janus webrtc 将屏幕与视频共享为本地流
- c++ - 从计算着色器更新顶点
- javascript - Node.JS SyntaxError:JSON 中第 20 位的意外标记 i