Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancel form load

Tags:

c#

forms

winforms

I have the following code :

This call the second form

private void updateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Update fm = new Update();
            fm.ShowDialog();
        }

This is the constructor

    public Update()
    {
        InitializeComponent();
    }

This is the load

    private void Update_Load(object sender, EventArgs e)
    {
        String ver = checkver();
        if (ver == "update")
        {
            if (RemoteFileExists(dlUrl) == true)
            {
                WebClient webClient = new WebClient();
                webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
                webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
                webClient.DownloadFileAsync(new Uri(dlUrl), "");
            }
            else
                MessageBox.Show("An error occurred. Please try later.");
        }
        else if (ver == "newest")
        {
            MessageBox.Show("You are currently using the newest version.");
            this.Close();
        }
        else
        {
            this.Close();
        }
    }

My problem is, that when the function result is 2 or 3 the form show up for millisecond and then close (flashing). I want the form to not flash. Is it possible?

I tried to use this.Hide(), this.Visible = False but nothing helped.

EDIT: I put the original code EDIT2: Put more code

like image 541
a1204773 Avatar asked Jun 22 '12 00:06

a1204773


People also ask

How do I cancel WinForm?

To close a form, you just need to set the form's DialogResult property (to any value by DialogResult. None ) in some event handler. When your code exits from the event handler the WinForm engine will hide the form and the code that follows the initial ShowDialog method call will continue execution.

What is Load event in a form?

The Form Load Event in VB . NET. An important event you'll want to write code for is the Form Load event. You might want to, for example, set the Enabled property of a control to False when a form loads. Or maybe blank out an item on your menu.


2 Answers

You can hide the form before loading and then set it back to visible in your if else conditions. e.g:

            MyForm myForm = new MyForm();
            myForm.Opacity = 0;
            myForm.Show();

And then:

if (ver == "update")
        {
            if (RemoteFileExists(dlUrl) == true)
            {
               myForm.Opacity = 100;
               ...

            }
            else
                MessageBox.Show("An error occurred. Please try later.");
        }
        else if (ver == "newest")
        {
            MessageBox.Show("You are currently using the newest version.");
            this.Close();
        }
        else
        {
            this.Close();
        }
like image 95
astro boy Avatar answered Nov 02 '22 23:11

astro boy


You should probably do whatever check you're performing before you choose to open the form in the first place.

So something like:

if(funct() == "1")
{
    var form = new Form();
    form.ShowDialog();
}
like image 29
Adam Lear Avatar answered Nov 03 '22 01:11

Adam Lear