/*! * \brief Remove the selected ticket (if any) from the lottery. * * If no ticket is selected, the function does nothing besides showing an error message. * If removing the selected ticket leaves the list empty, the add-comment button * and delete-ticket button are disabled. */ private void BtnRemoveTicket_Click(object sender, RoutedEventArgs e) { if (!HasTicketSelected()) { return; } var ticket = (Ticket)LwTickets.SelectedItem; if (WndDialogMessage.Show(this, "Do you want to delete this ticket?\n\nLottery number: " + ticket.LotteryNumber, "Confirm Delete", MessageBoxButton.YesNo, MessageBoxImage.Warning) != MessageBoxResult.Yes) { return; } CurrentLottery.RemoveTicket(ticket); LwTickets.Items.Remove(ticket); var oldPrice = int.Parse(TxtPrize.Text); TxtPrize.Text = "" + (oldPrice - ticket.Price); if (LwTickets.Items.Count < 1) { BtnAddComment.IsEnabled = false; BtnRemoveTicket.IsEnabled = false; } }
/*! * \brief Test if end-user has selected a lottery number in the list of tickets. * * If no selection is active an appropriate error message is shown. * * \return true if there is an item selected, and false if not. */ private bool HasTicketSelected() { if (LwTickets.SelectedItem != null) { return(true); } WndDialogMessage.Show(this, "No lottery number selected.", "Missing selection", MessageBoxButton.OK, MessageBoxImage.Error); return(false); }
/*! * \brief Show a dialog message with no icon, a window caption and an OK button. * * \param owner The owner of the window on which the dialog will be centered. * \param message The message to be shown in the dialog window. * \param caption The title that is displayed in the window's frame. * * \return The corresponding button that was pressed, or MessageBoxResult.None * if the window was closed without pushing a button. */ public static MessageBoxResult Show(Window owner, string message, string caption) { var dialog = new WndDialogMessage() { Owner = owner, Title = caption ?? "" }; dialog.SetMessage(message); dialog.ShowDialog(); return(dialog.Result); }
/*! * \brief Show a dialog message with a given icon, a window caption and a given set of buttons. * * \param owner The owner of the window on which the dialog will be centered. * \param message The message to be shown in the dialog window. * \param caption The title that is displayed in the window's frame. * \param buttons The button configuration of the dialog window. * \param icon The image icon that should be displayed in the dialog window. * * \return The corresponding button that was pressed, or MessageBoxResult.None * if the window was closed without pushing a button. */ public static MessageBoxResult Show(Window owner, string message, string caption, MessageBoxButton buttons, MessageBoxImage icon) { var dialog = new WndDialogMessage() { Owner = owner, Title = caption ?? "" }; dialog.SetMessage(message); dialog.SetButtons(buttons); dialog.SetIconImage(icon); dialog.ShowDialog(); return(dialog.Result); }
/*! * \brief Asks the end-user if the lottery "should be saved before closing". * \return true if the end-user canceled the dialog; false otherwise, regadless if the * end-user chose to save or not. */ private bool PromptSaveOnClose() { var result = WndDialogMessage.Show(this, "Save before closing?", "Closing Lottery", MessageBoxButton.YesNoCancel, MessageBoxImage.Question); if (result == MessageBoxResult.Yes) { SaveToFile(); } else if (result == MessageBoxResult.Cancel) { return(true); } return(false); }
/*! * \brief Check if a load-file name is stored and parse it if so. * * This function, run upon the welcome window being loaded, checks the * application's dynamic memory for a filename on a lottery save file * the application was started with (e.g. by double-clicking a save file). * * If such a file is found, it is parsed and the application shows the main * lottery window, assuming no parse error occured. */ private void WelcomeWindow_Loaded(object sender, RoutedEventArgs e) { if (!SkipUpdateCheck) { CheckForUpdates(); if (UpdateProcess != null && UpdateProcess.UpdateAvailable && !UpdateWasCancelled) { // Close the application since the update installer has been opened. Close(); return; } } if (Application.Current.Properties["LoadfileName"] == null) { return; } var filename = Application.Current.Properties["LoadfileName"].ToString(); Lottery importedLottery; try { Stream filestream = new FileStream(filename, FileMode.Open); importedLottery = InitiateImport(filestream); } catch (Exception ex) { WndDialogMessage.Show(this, "An unexpected error occured during loading of the save file:\n" + ex.Message, "Unexpected Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } if (importedLottery == null) { return; } var mainWindow = new WndMain(true, filename) { Owner = this, LotteryModel = importedLottery }; mainWindow.Show(); mainWindow.Owner = null; Close(); }
//! Open a new window for adding new tickets to the lottery. private void BtnSellTickets_Click(object sender, RoutedEventArgs e) { if (LotteryModel.NumLotteryNumbersLeft < 1) { WndDialogMessage.Show(this, "There are no more tickets to sell.", "Sold Out", MessageBoxButton.OK, MessageBoxImage.Information); return; } var newTicketsWindow = new WndNewTickets(LotteryModel) { Owner = this }; IgnoreSaveOnClosing = false; this.IsEnabled = false; newTicketsWindow.ShowDialog(); ProcessAutoSave(); }
//! Prompt the end-user to remove the selected ticket (if any) from the list of sold tickets. private void MenuItemRemoveWinner_Click(object sender, RoutedEventArgs e) { var selectedTicket = (Ticket)LwTickets.SelectedItem; if (selectedTicket == null) { return; } const string msg = "Do you want to remove the sold ticket?\nDoing so will make the number puchasable again."; if (WndDialogMessage.Show(this, msg, "Confirm Removal", MessageBoxButton.YesNo, MessageBoxImage.Warning) != MessageBoxResult.Yes) { return; } LotteryModel.RemoveTicket(selectedTicket); LwTickets.Items.Remove(selectedTicket); ((WndMain)Owner).IgnoreSaveOnClosing = false; }
//! Prompt the end-user to remove the selected (if any) ticket from the list of winners. private void MenuItemRemoveWinner_Click(object sender, RoutedEventArgs e) { var selectedTicket = (Ticket)LwWinningTickets.SelectedItem; if (selectedTicket == null) { return; } const string msg = "Do you want to remove the selected ticket from the pool of winners?\n\nThis DOES NOT delete the ticket."; if (WndDialogMessage.Show(this, msg, "Confirm Removal", MessageBoxButton.YesNo, MessageBoxImage.Warning) != MessageBoxResult.Yes) { return; } LotteryModel.RemoveWinningTicket(selectedTicket.LotteryNumber); LwWinningTickets.Items.Remove(selectedTicket); IgnoreSaveOnClosing = false; ProcessAutoSave(); }
/*! * \brief Initate importing a lottery save file into the application. * * NOTE that this function DOES NOT close the file's stream. * * \param filestream Open read-stream to the save file. */ private Lottery InitiateImport(Stream filestream) { string errorMsg; Lottery importedLottery; var importResult = Lottery.ImportFromFile(filestream, out importedLottery, out errorMsg); filestream.Close(); switch (importResult) { case Lottery.ImportResult.InvalidFile: WndDialogMessage.Show(this, "The import file is not a valid vplf file.", "Import Failed", MessageBoxButton.OK, MessageBoxImage.Error); return(null); case Lottery.ImportResult.ParseError: WndDialogMessage.Show(this, "Import of lottery file failed with error:\n" + errorMsg, "Import Failed", MessageBoxButton.OK, MessageBoxImage.Error); return(null); } return(importedLottery); }
/*! * \brief Draw a winning number and add the ticket to the list of winning tickets. * * If no tickets have yet be sold a message box informs the end-user of this and returns * without further action. Equally it shows a message and returns without further action if * all sold tickets have already won. */ private void BtnDrawWinner_Click(object sender, RoutedEventArgs e) { if (LotteryModel.Tickets.Count < 1) { WndDialogMessage.Show(this, "No tickets have yet been sold.\nNo winner is drawn.", "No Tickets Sold", MessageBoxButton.OK, MessageBoxImage.Information); return; } var winner = LotteryModel.DrawWinningNumber(); if (winner == null) { WndDialogMessage.Show(this, "All solds tickets have already won.\nNo winner is drawn.", "All Tickets Won", MessageBoxButton.OK, MessageBoxImage.Information); return; } LwWinningTickets.Items.Add(winner); IgnoreSaveOnClosing = false; ProcessAutoSave(); }
//! Check for updates, prompting the end-user to download and install if found. private void CheckForUpdates() { UpdateProcess = ApplicationUpdate.CheckForUpdate(); if (UpdateProcess == null) { const string msg = "Velvet Pearl Lottery could not check for updates.\n\nThis may be caused by a lack of internet connection or Enjin being down for maintenance. " + "If the problem persists, contact denDAY at \nwww.enjin.com/profile/493549."; WndDialogMessage.Show(this, msg, "Update Check Failed", MessageBoxButton.OK, MessageBoxImage.Warning); return; } else if (!UpdateProcess.UpdateAvailable) { return; } var choice = WndDialogMessage.Show(this, "A new update is available.\n\nDownload and install?", "New Update", MessageBoxButton.YesNo, MessageBoxImage.Question); if (choice != MessageBoxResult.Yes) { return; } // Download and register the install function (event handler) and cancel token. if (!UpdateProcess.DownloadUpdateAsync(InstallUpdate)) { return; } var cancelToken = new CancellationTokenSource(); cancelToken.Token.Register(CancelUpdate); UpdateStatusWnd = new WndUpdateStatus(cancelToken) { StatusText = "Downloading update ...", Owner = this }; UpdateStatusWnd.ShowDialog(); }
//! Attempt to add a new ticket, either with a random or specific lottery number. private void BtnAddTicket_Click(object sender, RoutedEventArgs e) { int actualLotteryNumber; if (BuyRandomLotteryNumber) { actualLotteryNumber = CurrentLottery.BuyLotteryNumber(TxtOwner.Text); // Makes sure that there was a ticket left to buy. if (actualLotteryNumber == Lottery.InvalidLotteryNumber) { WndDialogMessage.Show(this, "No more tickets available.", "No tickets left", MessageBoxButton.OK, MessageBoxImage.Information); return; } } else { if (string.IsNullOrEmpty(TxtSpecificLotteryNumber.Text)) { WndDialogMessage.Show(this, "No lottery number given.\nIf you want a random one, check the random box.", "Missing Number", MessageBoxButton.OK, MessageBoxImage.Information); return; } var userLotteryNumber = int.Parse(TxtSpecificLotteryNumber.Text); var result = CurrentLottery.BuyLotteryNumber(userLotteryNumber, TxtOwner.Text); switch (result) { case LotteryNumberSaleResult.Success: actualLotteryNumber = userLotteryNumber; break; case LotteryNumberSaleResult.NumberAlreadySold: WndDialogMessage.Show(this, "The number has already been sold.", "Number Unavailable", MessageBoxButton.OK, MessageBoxImage.Information); return; case LotteryNumberSaleResult.NumberOutOfRange: var msg = "The number is not in the alllowed range: " + Lottery.LotteryNumberMin + " - " + CurrentLottery.LotteryNumberMax + "."; WndDialogMessage.Show(this, msg, "Invalid Number", MessageBoxButton.OK, MessageBoxImage.Information); return; default: WndDialogMessage.Show(this, "An unspecified error occured.", "Unspecified Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } } // Add to list and update the tottal price. var ticket = CurrentLottery.Tickets.Find(T => T.LotteryNumber == actualLotteryNumber); LwTickets.Items.Add(ticket); LwTickets.ScrollIntoView(ticket); var oldPrice = int.Parse(TxtPrize.Text); TxtPrize.Text = "" + (oldPrice + ticket.Price); // Enabled the buttons for the list of tickets. BtnAddComment.IsEnabled = true; BtnRemoveTicket.IsEnabled = true; }