Search code examples
c#window-load

Blank window before all is loaded


How do I code a blank window, like this, before the whole form is loaded? I've tried dragging a white picturebox over the window at Window_Load and Window_Shown, but that didn't work out. Any ideas?


Solution

  • I have made a custom window so when it shows up it would appear as a message box with custom appearance and when the loading done I close this window like that

    CMessageBox cmb = new CMessageBox("Loading...");
    cmb.Show(this);
    
    //Do the heavy work here
    //after the heavy work finish call cmb.close()
    
    cmb.Close();
    

    of course cmb is the custom window that you have to make your self

    actually it is just a form that I have removed its borders and give it 'loading' text in the middle!

    OK I will add the code for the custom form

    Create a new form and add this code to it

    public CMessageBox(string message)
    {
        InitializeComponent();
        this.lblMessage.Text = message;
    }
    
    private void CMessageBox_Load(object sender, EventArgs e)
    {
        this.lblMessage.Top = (this.Height - this.lblMessage.Height) / 2;
        this.lblMessage.Left = (this.Width - this.lblMessage.Width) / 2;
    }
    
    private void lblMessage_TextChanged(object sender, EventArgs e)
    {
        this.lblMessage.Top = (this.Height - this.lblMessage.Height) / 2;
        this.lblMessage.Left = (this.Width - this.lblMessage.Width) / 2;
        this.Refresh();
    }
    
    public string _Caption
    {
        get { return this.lblMessage.Text; }
        set { this.lblMessage.Text = value; }
    }
    

    After that you call it as I showed you at the beginning

    I have set the window so it would have a small width and height just to contain the message, if you would like to change this you can ignore those 2 events

    private void CMessageBox_Load(object sender, EventArgs e)
    {
        this.lblMessage.Top = (this.Height - this.lblMessage.Height) / 2;
        this.lblMessage.Left = (this.Width - this.lblMessage.Width) / 2;
    }
    
    private void lblMessage_TextChanged(object sender, EventArgs e)
    {
        this.lblMessage.Top = (this.Height - this.lblMessage.Height) / 2;
        this.lblMessage.Left = (this.Width - this.lblMessage.Width) / 2;
        this.Refresh();
    }
    

    You need to add label in the design and give it a name lblMessage also you need to set the BorderStyle to none