如何将数据从并行数组传输到文本框中?

时间:2019-04-19 19:10:33

标签: vb.net

我正在尝试编写一个程序,该程序将文本文件中的数据拆分为并行数组,然后将每个数组的内容写入文本框中。文本文件是这样的:

标题1,类型1,导演1,
标题2,类型2,导演2,
Title3,Genre3,Director3,

Imports System.IO
Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Dim sr As New StreamReader("MovieData.txt")
Dim movieTitles(9), genre(9), directors(9), itemsRead() As String
Dim i As Integer = 0

Do Until sr.Peek = -1

    itemsRead = sr.ReadLine.Split(",")

    movieTitles(i) = itemsRead(0)
    genre(i) = itemsRead(1)
    directors(i) = itemsRead(2)

    TextBox1.Text = movieTitles(i).PadRight(20) & genre(i).PadRight(20) & directors(i)

    i += 1

Loop

sr.Dispose()

  End Sub  
  End Class

当我单击按钮时,文本框仅显示:

Title3 Genre3 Director3

我该如何解决?

2 个答案:

答案 0 :(得分:0)

完成后,只需像这样替换TextBox1.Text行的代码即可:

TextBox1.Text += movieTitles(i).PadRight(20) & genre(i).PadRight(20) & directors(i) & vbNewLine

我希望这能回答您的问题, ^ _ ^

enter image description here

答案 1 :(得分:0)

如果您希望数据排列整齐,请使用DataGridView。当您只需要从文本文件中提取数据时,就不需要StreamReader的额外功能。

现在,类及其属性是如此简单,以至于您实际上不需要并行数组。 我在类中添加了一个自定义构造函数,因此您可以创建一个新实例并将所有属性设置为一行。

电影列表可以用作网格的数据源。无需设置列。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim Movies As New List(Of Movie)
    Dim lines = File.ReadAllLines("MovieData.txt")
    For Each line In lines
        Dim MovieProperties = line.Split(","c)
        Dim M As New Movie(MovieProperties(0).Trim, MovieProperties(1).Trim, MovieProperties(2).Trim)
        Movies.Add(M)
    Next
    DataGridView1.DataSource = Movies
End Sub

Public Class Movie
    Public Property Title As String
    Public Property Genre As String
    Public Property Director As String
    Public Sub New(T As String, G As String, D As String)
        Title = T
        Genre = G
        Director = D
    End Sub
End Class

编辑,根据OP注释

'**EDIT** Move the declaration of Movies to Form level
Private Movies As New List(Of Movie)

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    '**EDIT** Remove local declaration of Movies
    Dim lines = File.ReadAllLines("MovieData.txt")
    For Each line In lines
        Dim MovieProperties = line.Split(","c)
        Dim M As New Movie(MovieProperties(0).Trim, MovieProperties(1).Trim, MovieProperties(2).Trim)
        Movies.Add(M)
    Next
    DataGridView1.DataSource = Movies
    'To return the movies by Director, I strongly suggest you fill a list box with the
    'director names so spelling errors won't frustate users
    ListBox1.DataSource = GetUniqueDirectors()
End Sub

Private Function GetUniqueDirectors() As List(Of String)
    'A bit of Linq using Distinct so we don't get duplicate listing of director names.
    Dim Directors = ((From m In Movies
                      Order By m.Director
                      Select m.Director).Distinct()).ToList
    Return Directors
End Function

Private Function GetMoviesByDirector(sDirector As String) As List(Of String)
    'A little Linq magic to get the movies for selected director
    Dim DirectorsMovies = (From s In Movies
                           Where s.Director = sDirector
                           Order By s.Title
                           Select s.Title).ToList
    Return DirectorsMovies
End Function

Public Class Movie
    Public Property Title As String
    Public Property Genre As String
    Public Property Director As String
    Public Sub New(T As String, G As String, D As String)
        Title = T
        Genre = G
        Director = D
    End Sub
End Class

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    If ListBox1.SelectedIndex = -1 Then
        MessageBox.Show("Please select a Director from the list.")
        Return
    End If
    ListBox2.DataSource = Nothing
    ListBox2.Items.Clear()
    ListBox2.DataSource = GetMoviesByDirector(ListBox1.SelectedItem.ToString)
End Sub