【问题标题】:I want to create an array of labels and sort them according to the values in the labels using insertion sort我想创建一个标签数组并使用插入排序根据标签中的值对它们进行排序
【发布时间】:2017-01-11 21:40:52
【问题描述】:

我从 5 个文本框中获取输入,并通过将插入文本框中的值放入标签中并四处移动标签,直到它们中的值被排序。

到目前为止,我已将它们放入标签中,但我不知道如何在单击按钮时移动标签并让标签移动以进行排序。

这是模拟插入排序算法的一种方式。

到目前为止我的按钮点击代码:

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");
    }

【问题讨论】:

    标签: c# algorithm sorting simulator


    【解决方案1】:

    您需要对文本框的值进行排序,然后将值放入标签中。像这样的东西:

     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);
            }
    

    【讨论】:

    • 你使用列表框听到我们不能使用计时器吗?我还想显示标签移动以对其中的数字进行排序
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-03
    相关资源
    最近更新 更多