繁体   English   中英

使用 C# windows 应用程序表单进行冒泡排序如何在每次单击按钮时清除 label.text

[英]Bubble Sort using C# windows application form how to clear label.text every button click

大家好,每次我按下按钮生成排序字符串时,输出都会连接到上一个标签输出中。 在显示新的输出之前,我不知道如何清除标签上的先前输出,这是我的代码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace You_Source
{
public partial class Form1 : Form
{
    string method;

    public Form1()
    {
        InitializeComponent();
    }

    private void radio_checked(object sender, EventArgs e)
    {
        RadioButton radio = (RadioButton)sender;
        method = radio.Text;
        //label_Output.Text = method;
    }

    private void button_Sort_Click(object sender, EventArgs e)
    {
        string input = textBox_Input.Text;
        label_Output.Clear();
        if ( method == "Bubble Sort")
        {
            char[] charInput = input.ToCharArray();
            char temp;

            for (int j = 0; j <= charInput.Length - 2; j++)
            {
                for (int i = 0; i <= charInput.Length - 2; i++)
                {
                    if (charInput[i] > charInput[i + 1])
                    {
                        temp = charInput[i + 1];
                        charInput[i + 1] = charInput[i];
                        charInput[i] = temp;
                    }
                }
            }
            foreach (char letter in charInput)
                label_Output.Text = label_Output.Text+letter;
        }
    }
}

任何人都可以给我一个关于该怎么做的提示。 示例(如果我输入“cba”,输出将是“abc”,然后当我在文本框“zyx”中输入另一个输入时,新标签输出将是“abcxyz”。我只是一个新的“xyz”显示。

您可以在代码的某个位置将字符串设置为"" ,然后用新字符串覆盖它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM