private void btnDeleteGame_Click(object sender, EventArgs e) { Game games = (Game)lstGamesList.SelectedItem; GamesDB.deleteGame(games); PopulateGameList(); }
private void btnAddOrUpdateGame_Click(object sender, EventArgs e) { Game game; if (gameToBeUpdated == null) { game = new Game(); } else { game = gameToBeUpdated; } if (!string.IsNullOrWhiteSpace(txtNameOfGame.Text) && !string.IsNullOrWhiteSpace(txtNumberOfCopies.Text) && !string.IsNullOrWhiteSpace(txtPrice.Text) && IsAnInt(txtNumberOfCopies.Text) == true && IsADecimal(txtPrice.Text) == true && !string.IsNullOrWhiteSpace(txtAvailability.Text)) { game.GameName = txtNameOfGame.Text; game.NumberOfCopies = Convert.ToInt32(txtNumberOfCopies.Text); game.Price = Convert.ToDouble(txtPrice.Text); game.Availability = txtAvailability.Text; GamesDB.AddOrUpdateGame(game); } else { MessageBox.Show("You must enter something in all four fields."); } }
private void txtSearchGames_TextChanged(object sender, EventArgs e) { if (txtSearchGames.Text.Length > 0) { /**************START GET GAME BY NAME************/ // clear the list lstGamesList.Items.Clear(); // get the text from the search box string search = txtSearchGames.Text; // get a list of all games by name List <Game> games = GamesDB.getAllGamesByName(); // get a second list of games to compare to List <Game> matches = new List <Game>(); // get all games by name that contain the search name matches = games.Where(g => g.GameName.Contains(search)).ToList(); // add them to the listbox when you find a game name that // contains the search word foreach (var match in matches) { lstGamesList.Items.Add(match); } /***************END GET GAME BY NAME*************/ /**************START GET GAME BY PRICE***********/ // get the text from the search box string search2 = txtSearchGames.Text; // get a list of all games by price List <Game> gamesByPrice = GamesDB.getAllGamesByPrice(); // get a second list to compare to List <Game> match2 = new List <Game>(); // get all games whose price starts with the price in the text box match2 = gamesByPrice.Where(g => g.Price.ToString().StartsWith(search)).ToList(); // display all games whose price matches what is typed in the // search box foreach (var i in match2) { lstGamesList.Items.Add(match2); } /***************END GET GAME BY PRICE*************/ } else { // populate the list with all the games PopulateGameList(); } }
/// <summary> /// Populates the gamesListBox with all the /// games that have been added into the database. /// </summary> private void PopulateGameList() { lstGamesList.Items.Clear(); try { List <Game> games = GamesDB.getAllGamesByName(); foreach (Game game in games) { lstGamesList.Items.Add(game); } } catch { MessageBox.Show("Error, please try again later"); Application.Exit(); } }