C# 从Windows窗体中删除标签(当它';它不再需要了

C# 从Windows窗体中删除标签(当它';它不再需要了,c#,forms,labels,C#,Forms,Labels,假设我有windows窗体的以下代码: public Form1() { this.Shown += new EventHandler(Form1_Shown); InitializeComponent(); } // - This Form1_Shown class is what's done AFTER the form is shown! Put stuff here! private void Form1_Shown(object sender, System.Eve

假设我有windows窗体的以下代码:

public Form1()
{
    this.Shown += new EventHandler(Form1_Shown);
    InitializeComponent();
}

// - This Form1_Shown class is what's done AFTER the form is shown! Put stuff here!
private void Form1_Shown(object sender, System.EventArgs e)
{
    methods.WriteTextToScreen("HelloLabel", "Hello!", 15, 15);
    methods.sleepFor(1);
    methods.EraseScreenLabel(Form1.HelloLabel);
    methods.WriteTextToScreen("GoodbyeLabel", "Goodbye!", 80, 80);
    methods.sleepFor(3);
    methods.EraseScreenLabel(Form.GoodbyeLabel);
}

public class methods
{

    public static int timeSlept;

    public static Label[] UsedTextBoxes = new Label[10000000000000000000];

    public static void WriteTextToScreen(string name, string text, int locX, int locY)
    {
        int numberOfPrintedItems = methods.UsedTextBoxes.GetLength(1);
        Label tempLabel = new Label();
        UsedTextBoxes[numberOfPrintedItems + 1] = tempLabel;

        tempLabel.Text = text;
        tempLabel.Name = name;
        tempLabel.Location = new Point(locX, locY);
        tempLabel.Visible = true;
        tempLabel.Enabled = true;
    }

    public static void EraseScreenLabel(Label label)
    {
        System.Windows.Forms.FlowLayoutPanel obj = new System.Windows.Forms.FlowLayoutPanel();
        obj.Controls.Remove(label);
        label = null;
    }

    public static void sleepFor(int seconds)
    {
        timeSlept = 0;

        System.Timers.Timer newTimer = new System.Timers.Timer();
        newTimer.Interval = 1000;
        newTimer.AutoReset = true;

        newTimer.Elapsed += new System.Timers.ElapsedEventHandler(newTimer_Elapsed);

        newTimer.Start();

        while (timeSlept < seconds)
        {
            Application.DoEvents();
        }

        newTimer.Dispose();

    }

    public static void newTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        timeSlept = IncreaseTimerValues(ref timeSlept);
        Application.DoEvents();
    }

    public static int IncreaseTimerValues(ref int x)
    {
        int returnThis = x + 1;
        return returnThis;
    }
public Form1()
{
this.show+=新的EventHandler(如图所示为Form1_);
初始化组件();
}
//-此Form1_Showed类是在显示表单后执行的操作!把东西放在这里!
显示私有void Form1_(对象发送方,System.EventArgs e)
{
方法:WriteTextToScreen(“HelloLabel”,“Hello!”,15,15);
方法:睡眠(1);
方法:擦除屏幕标签(Form1.HelloLabel);
方法:WriteTextToScreen(“再见”,“再见!”,80,80);
方法:睡眠(3);
方法:删除屏幕标签(Form.GoodbyeLabel);
}
公共类方法
{
公共静态int-timeSlept;
公共静态标签[]UsedTextBox=新标签[100000000000000000];
公共静态无效WriteTextToScreen(字符串名称、字符串文本、int-locX、int-locY)
{
int numberOfPrintedItems=methods.usedTextBox.GetLength(1);
Label tempLabel=新标签();
UsedTextBox[numberOfPrintedItems+1]=模板标签;
tempLabel.Text=文本;
tempLabel.Name=名称;
tempLabel.Location=新点(locX,locY);
tempLabel.Visible=true;
tempLabel.Enabled=true;
}
公共静态空白标签(标签标签)
{
System.Windows.Forms.FlowLayoutPanel obj=新建System.Windows.Forms.FlowLayoutPanel();
对象控件。移除(标签);
label=null;
}
公共静态无效休眠时间(整数秒)
{
timeSlept=0;
System.Timers.Timer newTimer=新的System.Timers.Timer();
新时间间隔=1000;
newTimer.AutoReset=true;
newTimer.appeated+=新系统计时器ElapsedEventHandler(newTimer\u appeated);
newTimer.Start();
while(时差<秒)
{
Application.DoEvents();
}
newTimer.Dispose();
}
public static void newTimer_经过(对象发送方,System.Timers.ElapsedEventArgs e)
{
timeSlept=增加timerValue(ref timeSlept);
Application.DoEvents();
}
公共静态int递增timervalues(ref int x)
{
int returnThis=x+1;
归还这个;
}
我希望使用
EraseScreenLabel(Label Label);
方法从屏幕上删除该标签,例如由
writeTextToScreen();
方法创建的
GoodbyeLabel
,因为我不希望它再可见。如何使该方法执行我希望的操作?我已经尝试使用
Dispose();
Finalize();
,以及
label=null;
。有人能提供帮助吗

new System.Windows.Forms.FlowLayoutPanel();
您刚刚创建了一个新的空面板。
修改该选项不会对表单中的现有面板产生任何影响

您需要修改表单上的现有面板

特别是,您应该去掉
方法
类,并在表单类上创建这些实例方法

您还应该将
sleepFor()
替换为
wait Task.Delay()

您刚刚创建了一个新的空面板。
修改该选项不会对表单中的现有面板产生任何影响

您需要修改表单上的现有面板

特别是,您应该去掉
方法
类,并在表单类上创建这些实例方法


您还应该将
sleepFor()
替换为
await Task.Delay()

我从methods类中删除了所有方法,并创建了一个静态FlowLayoutPanel();它直接位于public int timeSlept下;我的所有标签现在都指向一个中心FlowLayoutPanel对象,但对象控制.Remove(“GoodbyeLabel”);仍然没有删除该标签。我是否错过了你的部分建议或什么?@ApachePilotMPE:这不会编译。我从methods类中删除了所有方法,并创建了一个静态FlowLayoutPanel();直接位于public int timeSlept下;我的所有标签现在都转到一个中心FlowLayoutPanel obj,但是obj.Controls.remove(“再见”一词);仍然没有删除该标签。我错过了你的部分建议还是什么?@ApachePilotMPE:那不会编译。你在这段代码中犯了几个错误。编写UI代码的核心是永远不要睡觉。这根本不起作用,UI在你睡觉时被冻结,无法重新绘制自己。创建一个包含100000000000000000个元素的数组也是一件很重要的事情这永远不会奏效。最好从一本关于.NET编程的书中的示例或练习中学习编写C#代码的正确方法。@HansPassant:比这糟糕得多。他正在使用
DoEvents()
和一个不必要的后台线程来模拟
等待任务。延迟
。你在这段代码中犯了几个错误。编写UI代码的核心是永远不睡觉。这根本不起作用,UI在你睡觉时被冻结,无法重新绘制自己。创建一个包含100000000000000000个元素的数组也是永远不可能起作用的。贝丝“我不想从一本关于.NET编程的书中的示例或练习中学习编写C代码的正确方法。@HansPassant:比这更糟糕。他正在使用
DoEvents()
和一个不必要的后台线程来模拟
等待任务。延迟