所以我正在使用一个列表框,其中包含游戏名称列表,当您从列表中选择游戏并单击按钮时,它会从exe开始游戏。这很好用,但我想让我更容易添加新游戏,而无需一直编辑代码。我有2个文本框; 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 :(得分: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