Find Top 3 Button – which processes the data to find the 3 highest sales amounts and displays these sales persons names along with their place (1st, 2nd, 3rd), and sales amount.
I have two list boxes I just want to find the top 3 highest sales persons with their sales amount and show them as message box to the user in 3 separate lines
Picture of the application: http://s17.postimg.org/6dvo3a4qn/Untitled.jpg
Listboxes names: lstNames, lstTotalSales
My find top 3 button code is:
private void btnFindTop3_Click(object sender, EventArgs e)
{
decimal dec1HighestAmount = 0;
decimal dec2HighestAmount = 0;
decimal dec3HighestAmount = 0;
for (int Index = 0; Index < lstTotalSales.Items.Count; Index++)
{
if (Convert.ToDecimal(lstTotalSales.Items[Index]) > dec1HighestAmount)
{
dec1HighestAmount = Convert.ToDecimal(lstTotalSales.Items[Index]);
}
if (Convert.ToDecimal(lstTotalSales.Items[Index]) < dec1HighestAmount)
{
dec2HighestAmount = Convert.ToDecimal(lstTotalSales.Items[Index]);
}
if (Convert.ToDecimal(lstTotalSales.Items[Index]) < dec2HighestAmount)
{
dec2HighestAmount = Convert.ToDecimal(lstTotalSales.Items[Index]);
}
}
MessageBox.Show("Highest Amount is " + dec1HighestAmount + " and " + dec2HighestAmount + " and " + dec3HighestAmount);
}
This is the code that I used to fill the listboxes:
public partial class Form1 : Form
{
List<decimal> lstTotal = new List<decimal>();
public Form1()
{
InitializeComponent();
}
private void btnReadInSalesData_Click(object sender, EventArgs e)
{
openFileDialog1.FileName = "SalesNumbers.txt";
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) //If and Open Dialog OK
{
StreamReader srFile = File.OpenText(openFileDialog1.FileName);
decimal decTotal = 0;
while (!srFile.EndOfStream)
{
string strline = srFile.ReadLine();
string[] strSplit = strline.Split('$');
foreach (string strSplittedOutput in strSplit)
{
if (decimal.TryParse(strSplittedOutput, out decTotal))
{
lstTotal.Add(decTotal);
lstTotalSales.Items.Add(strSplittedOutput);
}
else //else than decimals add strings
{
lstNames.Items.Add(strSplittedOutput); //add the Sales men names to lstNames listbox
}
}
} //End of while
srFile.Close(); //Close StreamReader
}
else
MessageBox.Show("User Cancel Read File Operation."); // if the user cancel the read file operation show this messagebox
// ... ??
}
// ...
}
Thank you