So I am using a listbox which has a list of game names on it, when you select a game from the list and click the button it starts the game from the exe. This works well and good, but I want to make it easier for me to add new games without having to edit the code all of the time. I have 2 text boxes; txtDataAdd and txtFilePath. txtDataAdd adds a name to the listbox, and txtFilePath is for the user to input the file path of the game and then the box adds it to the code, however I have no idea how to do it, or if it's even possible, here's a snippet of code:
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"
sFinalAdd = "If lbListBox.SelectedItem = "" & sDataAdd & "" then
sData1 = "" & sCodeAdd & "" "
This seems to work for me - although you might need to adapt it slightly to change the path of where you want to save the text file and in the sub that executes the game's exe file. I've added comments so you should be able to see what's going on.
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