Hi there.
I just started coding in c sharp recently so i can't seem to figure this out.
When my program loads it it reads all the text files in a folder called games.
It opens each text file and get's the load path and some other variables then creates a new game object with those variables, then inserts it into a vector of game objects.
Afterwards it loops through the game vector and adds the name of the game to the listbox.
I have a couple of buttons, New and delete.
When I have a game selected in the list and hit the Delete button I get the error: Items collection cannot be modified when the DataSource property is set.
Also when I launch a game it should add the name of the game to listBox2 but doesn't.
I'm clueless. I've done some searching on google but haven't come up with much.
Code
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
namespace GameLauncher
{
public partial class Form1 : Form
{
//Variable declaration
public bool minimizeToTray = true;
//Object declaration
List<Games> GameList = new List<Games>();
List<String> RunningGames = new List<String>();
public Form1()
{
InitializeComponent();
ReadGamesFromDir getGames = new ReadGamesFromDir(ref GameList);
LoadGameList();
}
~Form1()
{
SaveGames saveGames = new SaveGames(GameList);
}
private void LoadGameList()
{
for (int i = 0; i < GameList.Count(); i++)
{
listBox1.Items.Add(GameList.ElementAt(i).getName());
}
}
private void NewGameButton_Click(object sender, EventArgs e)
{
Form2 NewGameForm = new Form2();
NewGameForm.Show();
}
private void LaunchGame_Click(object sender, EventArgs e)
{
if(File.Exists(GameList.ElementAt(listBox1.SelectedIndex).getLocation()))
{
RunningGames.Add(GameList.ElementAt(listBox1.SelectedIndex).getName());
Process.Start(GameList.ElementAt(listBox1.SelectedIndex).getLocation());
listBox1.Items.Add(GameList.ElementAt(listBox1.SelectedIndex).getName());
}
else
{
MessageBox.Show("Invalid executable file path!");
}
}
private void Edit_Click(object sender, EventArgs e)
{
Games game = GameList.ElementAt(listBox1.SelectedIndex);
Edit editForm = new Edit(ref game);
editForm.Show();
LoadGameList();
}
private void Delete_Click(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
listBox1.Items.Remove(listBox1.SelectedItem);
GameList.RemoveAt(listBox1.SelectedIndex);
}
}
private void editToolStripMenuItem1_Click(object sender, EventArgs e)
{
Form3 EditSettingsForm = new Form3();
EditSettingsForm.Show();
}
private void desktopGameToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 DesktopGameForm = new Form2();
DesktopGameForm.WebGameCheckBox.Checked = false;
DesktopGameForm.Show();
}
private void browserGameToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 WebGameForm = new Form2();
WebGameForm.WebGameCheckBox.Checked = true;
WebGameForm.Show();
}
private void Kill_Click(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
if (GameList.ElementAt(listBox1.SelectedIndex).getIsWebGame() == false)
{
Process[] prs = Process.GetProcesses();
foreach (Process pr in prs)
{
if (pr.ProcessName == GameList.ElementAt(listBox1.SelectedIndex).getName() + ".exe")
{
pr.Kill();
}
}
}
else
{
MessageBox.Show("ERROR: Can't kill a web browser game!");
}
}
}
}
}