1

假设我有一个 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;
    }

我希望该EraseScreenLabel(Label label);方法从屏幕上删除该标签,例如GoodbyeLabel由该writeTextToScreen();方法创建的标签,因为我不希望它不再可见。我怎样才能让那个方法做我想做的事?我已经尝试使用Dispose();, 和Finalize();, 以及label = null;. 任何人都可以提供任何帮助吗?

4

1 回答 1

1
new System.Windows.Forms.FlowLayoutPanel();

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

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

特别是,您应该摆脱您的methods类并在表单类上创建这些实例方法。

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

于 2013-06-02T13:22:57.917 回答