我的表单打开,然后在启动时关闭

时间:2019-10-23 01:22:28

标签: c# winforms

当我尝试启动表单时,它会像从未打开过一样闪烁。

我已经检查过我的代码,没有错

这是我的代码:

public partial class Initialization : Form
{
    public Initialization()
    {
        InitializeComponent();
    }

    private async Task Wait1(int Milliseconds)
    {
        await Task.Delay(Milliseconds);
    }

    private async void Wait(int Millisecondsdew)
    {
        Wait1(Millisecondsdew);
    }
    private void Initialization_Load(object sender, EventArgs e)
    {
        MessageBox.Show("Initialization");
        ///Setup Stuff///
        ShowIcon = false;
        ControlBox = false;
        FormBorderStyle = FormBorderStyle.FixedDialog;
        Text = "";
        /// Start Initializing///
        richTextBox1.Text = "Initializing....";
        Wait(1000);
        if (File.Exists(@"C:\Program Files (x86)\Lazy Tools\AdditionalFiles.exe"))
        {
            richTextBox1.Text = "Initializing.... \n Software Installers 1 Exists";
            Wait(1000);
            if (File.Exists(@"C:\Program Files (x86)\Lazy Tools\SoftwareInstallers2.exe"))
            {
                richTextBox1.Text = richTextBox1.Text + "\n Software Installers 2 Exists";
                Wait(1000);
            }
            else
            {
                MessageBox.Show("Please reinstall software store, \n Software Installers 2 is missing");
            }
        }
        else
        {
            MessageBox.Show("Please reinstall software store, \n Software Installers 1 is missing");
        }
    }
}

异步在我的第一种形式上工作正常,但这是第二种形式。当我调出第二个表格时,它闪烁并像从未打开过一样。

1 个答案:

答案 0 :(得分:0)

显然,将ControlBox属性设置为false ,然后Text属性设置为空字符串,在表单加载后导致表单关闭(似乎是一个错误)。

重现该问题的代码:

// Using other events like `Form_Shown` or even a `Button_Click` still has the same behavior
private void Form1_Load(object sender, EventArgs e)
{
    this.ControlBox = false;
    this.Text = "";
}

作为一种解决方法,您可以将FormBorderStyle属性设置为FormBorderStyle.None

this.FormBorderStyle = FormBorderStyle.None;

..这将与您尝试实现的效果类似。这实际上是隐藏标题栏的标准方法。

如果您不想隐藏边框,或者您实际上想使用ControlBoxText属性,则可以执行以下任一操作:

  • 在设计时设置这两个属性。或者...

  • 将这两行移动到以下形式的构造函数中:

    public Initialization()
    {
        InitializeComponent();
        this.ControlBox = false;
        this.Text = "";
    }
    
  • 或确保在Text属性之前将ControlBox属性设置为

    private void Initialization_Load(object sender, EventArgs e)
    {
        this.Text = "";
        this.ControlBox = false;
    }