首页 > 解决方案 > 从 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

123

标签: vb.net

解决方案


要求 #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

推荐阅读