Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.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#WPF排序编号_C#_Wpf_Sorting_Numbers - Fatal编程技术网

C#WPF排序编号

C#WPF排序编号,c#,wpf,sorting,numbers,C#,Wpf,Sorting,Numbers,我正在做一个项目,其中包括一个彩票号码生成器,然后保存到一个txt文件中,但我有一个问题:当每个号码位于不同的标签中时,如何按升序对号码进行排序 代码 static Random Gerar = new Random(); public static readonly string FilePath = "ChavesEuromilhões.txt"; public GerarChave() { InitializeComponent(); }

我正在做一个项目,其中包括一个彩票号码生成器,然后保存到一个
txt
文件中,但我有一个问题:当每个号码位于不同的标签中时,如何按升序对号码进行排序

代码

static Random Gerar = new Random();
    public static readonly string FilePath = "ChavesEuromilhões.txt";

    public GerarChave()
    {
        InitializeComponent();
    }

private void gerarNumeros() // method to generate numbers
    {

        int a = Gerar.Next(1, 50);
        int b = Gerar.Next(1, 50);
        int c = Gerar.Next(1, 50);
        int d = Gerar.Next(1, 50);
        int e = Gerar.Next(1, 50);
        int f = Gerar.Next(1, 12);
        int g = Gerar.Next(1, 12);

        num1.Content = a.ToString();
        num2.Content = b.ToString();
        num3.Content = c.ToString();
        num4.Content = d.ToString();
        num5.Content = e.ToString();
        num6.Content = f.ToString();
        num7.Content = g.ToString();


    }

    private void button_voltar_Click(object sender, RoutedEventArgs e)
    {
        MainWindow sw = new MainWindow();
        sw.Show();
        this.Close();

    }

    private void button_Guardar_Click(object sender, RoutedEventArgs e) //  save the number
    {
        using (StreamWriter writer = new StreamWriter(GerarChave.FilePath, true))
        {
            if (num1.Content == null || num2.Content == null || num3.Content == null || num4.Content == null || num5.Content == null || num6.Content == null || num7.Content == null || comboBox.SelectedValue == null || Date_Picker.SelectedDate == null)
            {
                System.Windows.MessageBox.Show("Todos Os campos São de Prenchimento Obrigatorio!");
            }
            else
            {
                writer.WriteLine(num1.Content + "," + num2.Content + "," + num3.Content + "," + num4.Content + "," + num5.Content + "," + num6.Content + "," + num7.Content + "," + comboBox.SelectedValue + "," + Date_Picker.SelectedDate.Value.Date.ToShortDateString());                   
                writer.Close();
                System.Windows.MessageBox.Show("Chave Guardada!");
            }

        }
    }

    private void button_ordenar_Click(object sender, RoutedEventArgs e) 
    {

       // button to order the numbers
     }
  }
}

}

例如,您可以将整数存储在一个
列表中,您只需调用
排序
,例如:

private readonly List<int> theNumbers = new List<int>();
private void gerarNumeros() // method to generate numbers
{
    int a = Gerar.Next(1, 50);
    int b = Gerar.Next(1, 50);
    int c = Gerar.Next(1, 50);
    int d = Gerar.Next(1, 50);
    int e = Gerar.Next(1, 50);
    int f = Gerar.Next(1, 12);
    int g = Gerar.Next(1, 12);

    theNumbers.Clear();
    theNumbers.Add(a);
    theNumbers.Add(b);
    theNumbers.Add(c);
    theNumbers.Add(d);
    theNumbers.Add(e);
    theNumbers.Add(f);
    theNumbers.Add(g);
    theNumbers.Sort();

    num1.Content = a.ToString();
    num2.Content = b.ToString();
    num3.Content = c.ToString();
    num4.Content = d.ToString();
    num5.Content = e.ToString();
    num6.Content = f.ToString();
    num7.Content = g.ToString();
}

private void button_ordenar_Click(object sender, RoutedEventArgs e)
{
    if (theNumbers.Count == 7)
    {
        num1.Content = theNumbers[0];
        num2.Content = theNumbers[1];
        num3.Content = theNumbers[2];
        num4.Content = theNumbers[3];
        num5.Content = theNumbers[4];
        num6.Content = theNumbers[5];
        num7.Content = theNumbers[6];
    }
}
private readonly List theNumbers=new List();
private void gerarNumeros()//生成数字的方法
{
int a=下一个(1,50);
intb=Gerar.Next(1,50);
int c=杰拉尔下一个(1,50);
intd=Gerar.Next(1,50);
int e=Gerar.Next(1,50);
intf=Gerar.Next(1,12);
int g=Gerar.Next(1,12);
数字清除();
编号。添加(a);
编号。添加(b);
编号。添加(c);
编号。添加(d);
编号。添加(e);
编号。添加(f);
编号。添加(g);
theNumbers.Sort();
num1.Content=a.ToString();
num2.Content=b.ToString();
num3.Content=c.ToString();
num4.Content=d.ToString();
num5.Content=e.ToString();
num6.Content=f.ToString();
num7.Content=g.ToString();
}
私有无效按钮\u或启用\u单击(对象发送者,路由目标)
{
如果(theNumbers.Count==7)
{
num1.Content=编号[0];
num2.Content=编号[1];
num3.Content=编号[2];
num4.Content=编号[3];
num5.Content=编号[4];
num6.Content=编号[5];
num7.Content=编号[6];
}
}

将num1.Conetent和num7.Content放入一个列表,并使用LINQ将数字添加到列表中,然后调用
Sort()
谢谢,效果很好!!:)