2016-02-17 53 views
0

我一直在爲此苦苦掙扎了一段時間,經過廣泛的搜索後,我還沒有找到答案。通過特定的一組行和輸出數據搜索?

在我的Visual Basic類中,我有一個程序,我必須從文本文件(songs.txt)中獲取文本,在列表框中顯示流派,並在流派之後在組合框中顯示相應的歌曲顯示。

目前,這是我的代碼。

' Variables 
    Dim strFilePath As String = "E:\Advanced VB\DJPlayList\DJPlayList\songs.txt" 
    Dim strFileError As String = "File not found. Please try again." 
    Dim strFileErrorTitle As String = "File Error" 
    Dim objReader As IO.StreamReader 
    Dim intCount As Integer = 0 
    Dim strSongGenre(intCount) As String 
    Dim i As Integer = 0 

    ' Finding the file 
    If IO.File.Exists(strFilePath) Then 
     ' Opening the text file 
     objReader = IO.File.OpenText(strFilePath) 
     Do Until objReader.Peek = -1 
      ReDim Preserve strSongGenre(intCount) 
      strSongGenre(intCount) = objReader.ReadLine 
      cboMusicGenre.Items.Add(strSongGenre(intCount)) 
      intCount += 1 
     Loop 
    Else 
     MsgBox(strFileError, , strFileErrorTitle) 
     Close() 
    End If 

這增加了從文本文件中的所有信息到陣列中,並加載到列表框,但我被困在如何輸出流派的專門和與它對應的歌曲。

的文本文件,如下所示:

All You Need is Love-Beatles 'Song Name 
Rock 'Song Genre 
4.25 'Song Time 
What Hurts the Most-Rascal Flatts 
Country 
5.25 
Touch it-Busta Rhymes 
Rap 
5.46 
My Girl-Temptations 
R&B 
4.35 
What you know?-T.I. 
Rap 
4.30 

如何具體得到流派和歌曲名稱?感謝您提前幫助

+2

這不是VBA,它是VB.NET。我已經爲您更改了標籤......如果您需要幫助,則需要確保您解決了正確的受衆羣體問題。沒有人使用VBA可以幫助你做到這一點。 –

+0

@Heinzi但在這種情況下,閱讀線條單獨適合需要什麼 –

+0

@DavidWilson:採取的要點。 – Heinzi

回答

1

所以實際情況是您的代碼正在讀取每一行並將它們全部存儲在ComboBox中。

也許在這個級別最容易做​​的事情是創建2個額外的臨時字符串變量,而不是讀1線爲循環的每個迭代,閱讀都是這樣

彼此相關的三條線
tempName= objReader.ReadLine 
    strSongGenre(intCount) = objReader.ReadLine 
    tempDuration = objReader.ReadLine 

如果你不希望使用歌曲的名稱和時間,然後什麼也不做與他們,他們會在循環的下一次迭代

因此,最終的代碼看起來應該是這樣

被覆蓋
Do Until objReader.Peek = -1 
    Dim tempName,tempDuration as string 
    ReDim Preserve strSongGenre(intCount) 
    tempName= objReader.ReadLine 
    strSongGenre(intCount) = objReader.ReadLine 
    tempDuration = objReader.ReadLine 
    cboMusicGenre.Items.Add(strSongGenre(intCount)) 
    intCount += 1 
Loop 
+0

@SDC這應該讓你繼續前進 - 儘管關於點擊組合框列出所有歌曲的其他內容 - 上述代碼將被重寫得更好。你知道結構嗎?和列表(..)?使用這些數據操作數據會容易得多,因爲數組與列表和集合等相比非常有限。請在我的個人檔案的電子郵件地址中告訴我。 –