/// <summary> /// Compare which PrimeResult object has a number greater than the other. /// </summary> /// <param name="obj">The object we are going to compare to</param> /// <returns>1 if this object has a number greater than the one we are comparing to, -1 otherwise</returns> public int CompareTo(object obj) { PrimeResult result = (PrimeResult)obj; if (this.Number > result.Number) { return(1); } if (this.Number < result.Number) { return(-1); } return(0); }
/// <summary> /// Removes the composite elements from the list view. /// </summary> /// <param name="sender">The object that triggered the event handler</param> /// <param name="e">The event information</param> private void Button_RemoveComposites_Click(object sender, EventArgs e) { foreach (ListViewItem item in listView_PrimeCheckResults.Items) //Loop through each item in the list view. { PrimeResult result = (PrimeResult)item.Tag; if (!result.IsPrime) //Remove the element if it is not prime. { item.Remove(); } } DetermineExportStatus(); //Update display elements UpdateStatistics(); }
/// <summary> /// Removes all prime numbers from the list view. /// </summary> /// <param name="sender">The object that triggered the event handler</param> /// <param name="e">The event information</param> private void Button_RemovePrimes_Click(object sender, EventArgs e) { foreach (ListViewItem item in listView_PrimeCheckResults.Items) //Check all the items in the list view { PrimeResult result = (PrimeResult)item.Tag; //Tag allows us to associate an object with a list view item. if (result.IsPrime) //Remove the item if it is prime. { item.Remove(); } } DetermineExportStatus(); //We have to update our GUI elements each time we remove or add elements. UpdateStatistics(); }
/// <summary> /// Calls our IsPrime method in the BigIntPrimeChecker class and displays the result in the listView control /// </summary> /// <param name="num"></param> public void CheckNumAndDisplayResult(BigInteger num) { if (listView_PrimeCheckResults.Items.Count > 0) { foreach (ListViewItem item in listView_PrimeCheckResults.Items) { item.Focused = false; item.Selected = false; } } Boolean isPrime = BigIntPrimeChecker.isPrime(num); //Calling our prime checker method located in the prime checker I created in a previous assignment. PrimeResult result = new PrimeResult(num, isPrime); //The prime result object stores a number, and whether or not the number is prime. listView_PrimeCheckResults.Items.Add(result.ToString()); //Add the result to the list. listView_PrimeCheckResults.Items[listView_PrimeCheckResults.Items.Count - 1].Tag = result; //Causes the item added in the previous statement to associate with an object (.Tag). if (result.ToString().Length > 30) //If the number is too large, we shrink the text size. This could be improved in the future. { listView_PrimeCheckResults.Items[listView_PrimeCheckResults.Items.Count - 1].Font = new Font("Veranda", 8); //Give the list view entry a new font. } if (isPrime) { listView_PrimeCheckResults.Items[listView_PrimeCheckResults.Items.Count - 1].BackColor = Color.Green; } //Green backround color if the listView entry is prime. else { listView_PrimeCheckResults.Items[listView_PrimeCheckResults.Items.Count - 1].BackColor = Color.Red; } //Red backround color if the number is composite. textBox_NumberToCheck.Text = ""; //Clear the text box. textBox_NumberToCheck.Select(); //Automatically selects the text box to allow the user to enter a new number. listView_PrimeCheckResults.EnsureVisible(listView_PrimeCheckResults.Items.Count - 1); DetermineExportStatus(); UpdateStatistics(); //After a new result is entered, we need to update the labels showing the total entrys, primes, and composites in the listView control. }
/// <summary> /// Sorts the results in the list view from least to greatest. /// </summary> /// <param name="sender">The objecgt that triggered the event handler</param> /// <param name="e">The event information</param> private void Button_SortResults_Click(object sender, EventArgs e) { //Temporarily transfer the elements in the list view to an array. The array has a sort method I implemented with the I-Comparable interface. PrimeResult[] resultArray = new PrimeResult[listView_PrimeCheckResults.Items.Count]; for (int i = 0; i < resultArray.Length; i++) { resultArray[i] = (PrimeResult)listView_PrimeCheckResults.Items[i].Tag; //Add each element in the listView collection to the array } Array.Sort(resultArray); //Call my sort method. The sort method, CompareTo, is implemented in the PrimeResult class. ClearAllFromResultList(); //Clear the list. We will be adding the same elements back, just sorted. foreach (PrimeResult result in resultArray) { CheckNumAndDisplayResult(result.Number); //This method adds each element back to the listView. } if (listView_PrimeCheckResults.Items.Count > 0) { listView_PrimeCheckResults.EnsureVisible(0); } }
/// <summary> /// Copy constructor so we don't violate data hiding /// </summary> /// <param name="result">The PrimeResult object we want to duplicate</param> public PrimeResult(PrimeResult result) { this.numberChecked = result.Number; //Copy the values. this.isPrime = result.isPrime; }