2016-09-04 1 views
1

Ich nehme die Eingabe von 5 Textfelder und Sortieren der Werte in die Textfelder, indem Sie sie in Etiketten und verschieben Sie die Etiketten herum bis die Werte in ihnen sortiert sind.Ich möchte ein Array von Etiketten erstellen und sortieren sie nach den Werten in den Etiketten mit Einfügen sortieren

Ich habe sie bis jetzt in Etiketten, aber ich weiß nicht, wie man die Etiketten auf Knopfdruck bewegt und die Etiketten verschieben, um sortiert zu bekommen.

Dies ist eine Möglichkeit, den Algorithmus der Einfügesortierung zu simulieren.

mein Code so weit für einen Knopf klicken:

private void button1_Click(object sender, EventArgs e) 
{ 
    if (comboBox1.SelectedItem.ToString() == "insertion sort") 
    { 
     for (i = 0; i < 5; i++) 
     { 
      if (c != 0) 
      { 
       myLabel[i].Dispose(); 
      } 

      myLabel[i] = new Label(); 
      myLabel[i].Location = new Point(a, b); 
      myLabel[i].Width = 70; 
      myLabel[i].Height = 70; 
      myLabel[i].BackColor=Color.White; 
      myLabel[i].BorderStyle = BorderStyle.FixedSingle; 
      panel1.Controls.Add(myLabel[i]); 
      a = a + 100; 
      myLabel[i].Visible = true; 
     } 

     timer1.Start(); 
     c++; 
    } 

    myLabel[0].Text = textBox1.Text; 
    myLabel[1].Text = textBox5.Text; 
    myLabel[2].Text = textBox4.Text; 
    myLabel[3].Text = textBox3.Text; 
    myLabel[4].Text = textBox2.Text; 
} 


public partial class Form1 : Form 
{ 
    Label[] myLabel=new Label[5]; 
    int a = 30;   //x coordinates of first label in label1 array 
    int b = 125;  //y coordinates of first label in label1 array 
    int c = 0; 
    int k = 0; 
    int n = 0; 
    int j = 1; 
    int i; 

    public Form1() 
    { 
     InitializeComponent(); 
     comboBox1.Items.Add("Selection Sort"); 
     comboBox1.Items.Add("Insertion Sort"); 
    } 

Antwort

1

Sie brauchen Wert von Textbox zu sortieren und dann in den Etiketten Wert. Etwas wie das:

private void button1_Click(object sender, EventArgs e) 
    { 
     if (comboBox1.SelectedItem.ToString() == "insertion sort") 
     { 
      for (i = 0; i < 5; i++) 
      { 
       if (c != 0) 
       { 
        myLabel[i].Dispose(); 
       } 

       myLabel[i] = new Label(); 
       myLabel[i].Location = new Point(a, b); 
       myLabel[i].Width = 70; 
       myLabel[i].Height = 70; 
       myLabel[i].BackColor=Color.White; 
       myLabel[i].BorderStyle = BorderStyle.FixedSingle; 
       panel1.Controls.Add(myLabel[i]); 
       a = a + 100; 
       myLabel[i].Visible = true; 
      } 

      timer1.Start(); 
      c++; 
     } 

       var list = new List<KeyValuePair<string, string>>(); 
       list.Add(new KeyValuePair<string, string>(textBox1.Text, textBox1.Text.Value)); 
       list.Add(new KeyValuePair<string, string>(textBox2.Text, textBox2.Text.Value)); 
       list.Add(new KeyValuePair<string, string>(textBox3.Text, textBox3.Text.Value)); 
       list.Add(new KeyValuePair<string, string>(textBox4.Text, textBox4.Text.Value)); 
       list.Add(new KeyValuePair<string, string>(textBox5.Text, textBox5.Text.Value)); 
       list.Sort(Compare2); 
       int increment = 0; 
       foreach(var item in list) 
       {     
        myLabel[increment].Text=item.Value; 
        increment++; 
       }   
    } 



static int Compare2(KeyValuePair<string, string> a, KeyValuePair<string, string> b) 
     { 
      return a.Value.CompareTo(b.Value); 
     } 
+0

sind Sie mit einer Listbox hören können wir es nicht mit Timers tun? Ich möchte auch die Etiketten zeigen, die sich bewegen, um die Zahlen in ihnen zu sortieren – Char

Verwandte Themen