maky55 maky55 - 1 year ago 85 Question

Write code using a textbox

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 & "" "

End Sub

sFinalAdd is the code I want to be written when the button is clicked, and is it also possible to save the new code so that it doesn't reset every time the application is restarted? Thanks.

Answer Source

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
    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
            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
    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
    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
        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()
        For Each game As String In GamesList.Keys
    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)
        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)
        End If
    End Sub
End Class
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download