2016-11-19 37 views
-1

因此,當您從列表中選擇一個遊戲並單擊它從exe啓動遊戲時,我使用的列表框中包含一個遊戲名稱列表。這個效果很好,但是我想讓我更容易添加新遊戲,而無需一直編輯代碼。我有兩個文本框; txtDataAdd和txtFilePath。 txtDataAdd爲列表框添加一個名稱,txtFilePath用於輸入遊戲的文件路徑,然後該框將其添加到代碼中,但我不知道如何去做,或者如果甚至可能,這裏是一個的代碼片段:使用文本框編寫代碼

Dim sDataAdd As String = txtDataAdd.Text 
    Dim sCodeAdd As String = txtFilePath.Text 
    Dim sFinalAdd As String 
    Dim test As String = "Documents\New Text Document.txt" 

    lbListBox.Items.Add(sDataAdd) 

    sFinalAdd = "If lbListBox.SelectedItem = "" & sDataAdd & "" then 
        sData1 = "" & sCodeAdd & "" " 


End Sub 

sFinalAdd是我想要的是單擊按鈕時要寫入了,是不是也可以保存新的代碼,以便它不會重置每次重新啓動應用程序時的代碼?謝謝。

+0

順便說一句,人們downvoting,你能解釋什麼,也許幫助我。 – maky55

+1

,因爲它用錯誤的語言標記? –

+0

嗯真的嗎?我正在使用Visual Studio 2015的visual basic ... – maky55

回答

1

這似乎適用於我 - 儘管您可能需要稍微調整它以更改要保存文本文件的路徑以及執行遊戲的exe文件的子路徑。我已經添加了評論,所以你應該能夠看到發生了什麼。

Public Class Form1 

    'This is where your list of games will be stored 
    Dim GamesList As New Dictionary(Of String, String) 
    'The filepath and name of your games text file 
    Dim GamesListPathName = "K:\GamesList.txt" 

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     AddGamesToDictionary() 
     PopulateListBox() 
    End Sub 

    'Checks to see if your text file containing the list of games exists. 
    'If it does, read it into the games list dictionary and 
    'populates your list box 
    Private Sub AddGamesToDictionary() 
     If My.Computer.FileSystem.FileExists(GamesListPathName) Then 
      LoadGamesList() 
     Else 
      GamesList.Add("Defender", "C:\Program Files\Defender\Defender.exe") 
      GamesList.Add("Asteroids", "C:\Program Files\Asteroids\Asteroids.exe") 
      GamesList.Add("Space Invaders", "C:\Program Files\Space Invaders\invaders.exe") 
     End If 
     SaveGameList() 
    End Sub 

    'Reads the text file that contains your list of games and paths 
    'onto the games list dictionary 
    Private Sub LoadGamesList() 
     Dim GamesListString As String = My.Computer.FileSystem.ReadAllText(GamesListPathName) 
     Dim tempGamesListArray() As String 
     tempGamesListArray = Split(GamesListString, vbCrLf) 

     For Each item As String In tempGamesListArray 
      If Not item = "End Of List" Then 
       Dim game As String 
       Dim gamePathName As String 
       game = Split(item, ",")(0) 
       gamePathName = Split(item, ",")(1) 
       GamesList.Add(game, gamePathName) 
      End If 
     Next 
    End Sub 

    'Saves the games in the games dictionary to the text file 
    Private Sub SaveGameList() 
     Dim filetext As String = "" 
     For Each game As String In GamesList.Keys 
      filetext = filetext & game & "," & GamesList(game) & vbCrLf 
     Next 
     filetext = filetext & "End Of List" 
     My.Computer.FileSystem.WriteAllText(GamesListPathName, filetext, False) 
    End Sub 

    'Populates the listbox with the games in the games dictionary 
    Private Sub PopulateListBox() 
     lbListBox.Items.Clear() 
     For Each game As String In GamesList.Keys 
      lbListBox.Items.Add(game) 
     Next 
    End Sub 

    'When the btnAddGame is clicked, if both textboxes aren't empty, 
    'add the text to the games dictionary and repopulate the listbox 
    Private Sub btnAddGame_Click(sender As Object, e As EventArgs) Handles btnAddGame.Click 
     If txtDataAdd.Text > "" And txtFilePath.Text > "" Then 
      Dim game As String = txtDataAdd.Text 
      Dim pathName As String = txtFilePath.Text 
      GamesList.Add(game, pathName) 
      SaveGameList() 
      PopulateListBox() 
     End If 
    End Sub 

    'When the listbox is clicked, make sure an item is selected 
    'and start the process using the path name associated with the item selected 
    Private Sub lbListBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lbListBox.SelectedIndexChanged 
     If lbListBox.SelectedIndex > -1 Then 
      Dim pathName As String = GamesList.Item(lbListBox.SelectedItem) 
      Process.Start(pathName) 
     End If 
    End Sub 
End Class 
+0

非常感謝!也歡呼明確的答案和評論,我可以看到我錯在哪裏以及如何改進。 – maky55