d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > [c#] Adding And Removing From Listbox > Need Some Help
Add Reply New Topic New Poll
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Jan 27 2013 01:42pm
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!");
               }
           }
       }
   }
}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jan 27 2013 05:57pm
instead of adding/removing from the listbox's collection, keep a separate list and set the datasource. to modify it, modify the list instead of the listbox's collection
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Jan 27 2013 06:54pm
Quote (carteblanche @ Jan 27 2013 03:57pm)
instead of adding/removing from the listbox's collection, keep a separate list and set the datasource. to modify it, modify the list instead of the listbox's collection


Will it automatically update when a game is added / removed or will I have to create an update function to call afterwards?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jan 27 2013 08:03pm
Quote (SelfTaught @ Jan 27 2013 07:54pm)
Will it automatically update when a game is added / removed or will I have to create an update function to call afterwards?


not sure what you're asking. it will not automatically read your text file to detect changes, no. you'll have to write something for that.

if you change your list, then yes those are automatically reflected on the listbox. that's what data binding is all about.
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Jan 27 2013 08:53pm
Quote (carteblanche @ Jan 27 2013 06:03pm)
not sure what you're asking. it will not automatically read your text file to detect changes, no. you'll have to write something for that.

if you change your list, then yes those are automatically reflected on the listbox. that's what data binding is all about.


Alright, I got it figured out. Thanks.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll