//When the button to save and exit is pressed, the window's close function is //called, which then fires the event watchLater_Close public void watchLater_Close(object sender, CancelEventArgs e) { MessageBoxResult result = MessageBox.Show(this, "Do you wish to save changes to the database" + " before closing?", "Close Window", MessageBoxButton.YesNoCancel); //When the closing event fires, the following messageBox shows to allow for confirmation, //cancellation, and to save or not save any changes to the database, namely the deletion //of any items switch (result) { case MessageBoxResult.Yes: DataRoutines.saveDatabase(readData); closeEvents(); break; case MessageBoxResult.No: closeEvents(); break; case MessageBoxResult.Cancel: e.Cancel = true; break; } //The result from the messageBox is used in this switch-case statement to call the appropriate //functions. If the yes button is pressed the DataRoutines function saveDatabase is called. //closeEvents is called after this or when no is pressed to return to the main tickerWindow, //and cancelling sets the necssary event argument to commence cancellation }
//These declarations are for important variables which must be //passed between functions in this module public Settings() { sources = DataRoutines.sourceRead(); InitializeComponent(); feedList.ItemsSource = sources; this.DataContext = sources; }
//When focus moves away from the ticker window or it //closes, it must first be undocked so that reserved screen space //is freed for use by other programs public void Refresh() //This function calls when the ticker needs to be refreshed { canv.Children.Clear(); //The canvas is cleared of any TickerItemElements List <string> sources = (DataRoutines.sourceRead()).ToList(); List <newsItem> news = RSS_Scraper.RSS_Scrape(sources); ticker.items.Clear(); foreach (newsItem item in news) { ticker.items.Add(new TickerItemElement(item)); } //The tickers list of items is cleared, and then filled //with a new set of news ticker.refreshInterval = Properties.Settings.Default.refreshTime; ticker.animTimer.Interval = new TimeSpan(0, 0, 1); string tickerSpeedStr = Properties.Settings.Default.tickerSpeed; int defaultSpeed = 60; switch (tickerSpeedStr) { case "50%": defaultSpeed = 2 * (defaultSpeed); ticker.animDuration = new TimeSpan(0, 0, defaultSpeed); break; case "75%": defaultSpeed = Convert.ToInt32((1.5 * (Convert.ToDouble(defaultSpeed)))); ticker.animDuration = new TimeSpan(0, 0, defaultSpeed); break; case "100%": ticker.animDuration = new TimeSpan(0, 0, defaultSpeed); break; case "150%": defaultSpeed = Convert.ToInt32((0.75 * (Convert.ToDouble(defaultSpeed)))); ticker.animDuration = new TimeSpan(0, 0, defaultSpeed); break; case "200%": defaultSpeed = Convert.ToInt32((0.5 * (Convert.ToDouble(defaultSpeed)))); ticker.animDuration = new TimeSpan(0, 0, defaultSpeed); break; } ticker.Start(); //The ticker is then reset and started. WHile inefficient, this does function //as intended }
//When the Settings window has closed, a new instance of the //main tickerWindow is called and shown, returning the program //to scrolling news on the screen public void saveSettings() { DataRoutines.saveSources(sources); Properties.Settings.Default.Save(); }
//The function initTicker is called when loading is complete, //to initialise the key aspects of the window public void initTicker() { List <string> sources = (DataRoutines.sourceRead()).ToList(); //The list of input newsSources is read using //DataRoutines.sourceRead List <newsItem> news = RSS_Scraper.RSS_Scrape(sources); //This list of input sources is then passed to //RSS_Scraper.RSS_Scrape, which gathers //all news from those sources ticker = new Ticker <TickerItemElement>(canv, this); //A new ticker is instantiated, with a parent of the //current tickerWindow instantiating it, and a //containing window of the canvas element canv foreach (newsItem item in news) { ticker.items.Add(new TickerItemElement(item)); } //Subsequently, the list of items the ticker has to //scroll is added to with the gathered news, creating //tickerItemElements as the process occurs ticker.refreshInterval = Properties.Settings.Default.refreshTime; //The refresh interval of the ticker is set to the user set //property refreshTime string tickerSpeedStr = Properties.Settings.Default.tickerSpeed; int defaultSpeed = 60; //The default speed of the ticker corresponds to a 60 second //TimeSpan. The string tickerSpeedStr is linked to the tickerSpeed //user setting, and is used to determine the modifier to this //value switch (tickerSpeedStr) { //The following switch-case block will choose the //appropriate modifier to the speed value case "50%": defaultSpeed = 2 * (defaultSpeed); ticker.animDuration = new TimeSpan(0, 0, defaultSpeed); break; //The speed being set to 50% slower means that the TimeSpan must //be doubled case "75%": defaultSpeed = Convert.ToInt32((1.5 * (Convert.ToDouble(defaultSpeed)))); ticker.animDuration = new TimeSpan(0, 0, defaultSpeed); break; //For 75%, this is halfway between 1.0 and 2.0 times the default value. As //this is multiplying by a double, the program must convert to the integer //deafult value to a double and back to an integer for use in the TimeSpan case "100%": ticker.animDuration = new TimeSpan(0, 0, defaultSpeed); break; //This is the default value, so there are no changes made case "150%": defaultSpeed = Convert.ToInt32((0.75 * (Convert.ToDouble(defaultSpeed)))); ticker.animDuration = new TimeSpan(0, 0, defaultSpeed); break; //A 150% speed corresponds to a 0.75 times multiplier, making the TimeSpan //smaller case "200%": defaultSpeed = Convert.ToInt32((0.5 * (Convert.ToDouble(defaultSpeed)))); ticker.animDuration = new TimeSpan(0, 0, defaultSpeed); break; //A 200% speed halves the TimeSpan to 30 seconds } ticker.Start(); dock(); //The ticker is started and the dock function called }
//When the constructor instantiates the window, //the component is initialised and the function //watchLaterLoaded is called private void watchLaterLoaded() { readData = DataRoutines.readAll(); itemList.ItemsSource = readData; this.DataContext = readData; }
//The constructor initialises the component, then //sets the data context to itself and sets the FeedItem //property accordingly. private void MenuItem_Click(object sender, RoutedEventArgs e) { bool ratingValid = false; //This variable sets up a loop in order to allow //for a while loop to be used while (ratingValid == false) { //The while loop allows for the rating input to be retried //by the user, or cancelled if they change their mind ratingStr = Interaction.InputBox("Please enter an importance rating from 0.0 to 10.0 to 1 decimal place" + " (Entering nothing will cancel this operation, as will pressing the cancel button) ", "Assign rating", ""); //A VisualBasic InputBox is created and displays the following message, with a suitable title and blank //default response. The input response is stored in a suitable variable, ratingStr, defined at the start of //the code for this object. if (ratingStr == "") { break; } //Pressing the cancel button on the InputBox will always return an empty string, so this if statement //is used to detect the cancellation event and break the while loop try { ratingDbl = Double.Parse(ratingStr); } //A try-catch block is used, and an attempt is made to parse the input string to a Double catch (Exception) { MessageBox.Show("Entered rating is not a valid number, please try again or cancel", "Invalid Format", MessageBoxButton.OK); continue; } //Any exception is caught here. This would be from an invalid parse, due to input of non-numeric characters. //As such, the following dialog box alerts the user of this, and continues the while loop to allow for //input to be retried. ratingDbl = Math.Round(ratingDbl, 1); //If the parsing is a success, the Double value is rounded to one decimal place if (ratingDbl >= 0.0 && ratingDbl <= 10.0) { newsItem currentItem = FeedItem; currentItem.Rating = ratingDbl; FeedItem = currentItem; ratingValid = true; } //If the rating input is between the boundaries of 0 and 10 inclusive, then the rating of the //newsItem object contained in the TickerItemEelement object is set to this input rating value //by first retrieving the stored newsItem, setting the rating value, and then storing the altered //newsItem //ratingValid is thereafter set to true, breaking the while loop. else { MessageBox.Show("Entered rating is not between 0.0 and 10.0, please try again", "Invalid Range", MessageBoxButton.OK); continue; } //If the rating value is outside of these boundaries, the following is displayed, and the //while loop continues to allow the user to retry inputting the rating } if (ratingStr == "") { } //If the input was cancelled then the while loop breaks, and so to stop //the addition of an unwanted item to the watchLater database, this if statement //detects that eventuality and ensures that no action is taken else { DataRoutines.addItem(this); } //If the if statement above is not triggered, then the TickerItemElement object //is added to the database using the DataRoutines function addItem. }