簡體   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