Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#在收集列表框中查找前3名_C#_Listbox_Find_Highest - Fatal编程技术网

C#在收集列表框中查找前3名

C#在收集列表框中查找前3名,c#,listbox,find,highest,C#,Listbox,Find,Highest,查找前3个按钮–处理数据以查找3个最高销售额 并显示这些销售人员的姓名及其所在地(第一、第二、第三), 和销售额 我有两个列表框,我只想找到前三名销售金额最高的销售人员,并将他们作为消息框显示在3行中 申请图片: 列表框名称:lstNames、lstotalsales 我的查找前3个按钮代码是: private void btnFindTop3_Click(object sender, EventArgs e) { decimal dec1HighestAmount = 0;

查找前3个按钮–处理数据以查找3个最高销售额 并显示这些销售人员的姓名及其所在地(第一、第二、第三), 和销售额

我有两个列表框,我只想找到前三名销售金额最高的销售人员,并将他们作为消息框显示在3行中

申请图片:


列表框名称:lstNames、lstotalsales

我的查找前3个按钮代码是:

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);
}
private void btnFindTop3\u单击(对象发送者,事件参数e)
{
十进制DEC1HIGHESTAUNT=0;
十进制dec2HighestAmount=0;
十进制dec3HighestAmount=0;
对于(int Index=0;Index
这是我用来填充列表框的代码:

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

        // ... ??
    }

    // ...
}
公共部分类表单1:表单
{
List lstotal=新列表();
公共表格1()
{
初始化组件();
}
私有void btnReadInSalesData_单击(对象发送者,事件参数e)
{
openFileDialog1.FileName=“SalesNumbers.txt”;
if(openFileDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)//if并打开对话框OK
{
StreamReader srFile=File.OpenText(openFileDialog1.FileName);
小数点总数=0;
而(!srFile.EndOfStream)
{
字符串strline=srFile.ReadLine();
字符串[]strSplit=strline.Split(“$”);
foreach(字符串strsplitedoutput在strSplit中)
{
if(十进制三倍体(strsplitedoutput,out decTotal))
{
lstotal.Add(decTotal);
lstotalsales.Items.Add(strsplitedoutput);
}
else//else非小数添加字符串
{
lstNames.Items.Add(strsplitedoutput);//将销售人员姓名添加到lstNames列表框
}
}
}//时间结束
srFile.Close();//关闭StreamReader
}
其他的
Show(“用户取消读取文件操作”);//如果用户取消读取文件操作,则显示此MessageBox
// ... ??
}
// ...
}
谢谢你

var top_three = (from sales in lstTotalSales.Items
                 orderby sales descending
                 select sales).Take(3);
将使用linq保持你的前三名


啊。。。好啊坏消息。。。你还有很多工作要做。。。好消息是,这会让你走上正确的道路:

public partial class Form1 : Form
{
    List<decimal> lstTotal = new List<decimal>();
    List<SalesPerson> lstSalesPerson = new List<SalesPerson>;

    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;
            decimal tempSales =0;
            string tempName = "";

            while (!srFile.EndOfStream)
            {
                string strline = srFile.ReadLine();
                string[] strSplit = strline.Split('$');

                // I'll seriously assume this happens only twice ...
                foreach (string strSplittedOutput in strSplit)
                {

                    if (decimal.TryParse(strSplittedOutput, out decTotal))
                    {
                        lstTotal.Add(decTotal);
                        lstTotalSales.Items.Add(strSplittedOutput);
                        tempSales = strSplittedOutput;
                    }
                    else //else than decimals add strings
                    {
                        lstNames.Items.Add(strSplittedOutput); //add the Sales men names to lstNames listbox
                        tempName = strSplittedOutput;
                    }
                }

                    // Adding this to our people list ...
                    lstSalesPerson.Add(new SalesPerson {Name=tempName,TotalSales=tempSales});

            } //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

        // ... ??
    }

    // ...
}


public class SalesPerson {
    public string Name {get; set;}
    public decimal TotalSales {get; set;}
}

问题是….?与其找到前三名,为什么不对列表进行排序呢?以防这个程序的用户想要找到前5名、前10名等。这是messagebox的输出:,我只是想得到lstNames中人名的索引号,它等于messagebox中要显示的前3名。谢谢你,谢谢你,我也在努力弄清楚他们的名字和销售额!所以如果您有一组人,每个对象都包含姓名、销售额和小狗的颜色,则可以让每个列表使用显示值仅显示一个属性,但在主列表上运行linq将获得对象。然后只参考你需要的属性,如姓名和销售额…似乎你没有理解我的意思,或者我不理解你的意思,我有两个不同的列表框,linkq将在不知道姓名的情况下获得总销售额。请多解释。感谢您的时间:)列表框名称:lstNames,lstotalsales我不知道如何通过c#循环获得列表框中人名的索引号,该索引号等于销售额索引号。我希望这对你来说足够清楚。谢谢:)你如何填写这些清单?你必须有一个有名字和销售财产的人的集合,对吗?
var top_three = (from person in lstSalesPerson
             orderby person.TotalSales descending
             select person).Take(3);

foreach (var person in top_three) {
     // do something with person.Name
     // do something with person.TotalSales
}