Cancel form upload

I have the following code:

Call of 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 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 result of the function is 2 or 3, the form is displayed in milliseconds and then it closes (blinks). I want the form not to blink. Is it possible?

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

EDIT: I put the source code EDIT2: add more code

+4
source share
6 answers

You can hide the form before loading, and then return it to the visible conditions in if else . eg:

  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(); } 
+8
source

The best way to do this is:

 private void Form_Load(object sender, EventArgs e) { switch(funct()) { case 2: this.BeginInvoke(new MethodInvoker(this.Close)); break; case 3: this.BeginInvoke(new MethodInvoker(this.Close)); break; default: MessageBox.Show("Something"); } } 
+2
source

You probably should do whatever you do before you decide to open the form first.

So something like:

 if(funct() == "1") { var form = new Form(); form.ShowDialog(); } 
+1
source

I assume Update_Load is your FormLoad handler? This is called after . Your form has been displayed. If you do not want to display it, it is too late. Change updateToolStripMenuItem_Click to this:

 String ver = checkver(); if (ver == "update") { if (RemoteFileExists(dlUrl)) { Update fm = new Update(); fm.ShowDialog(); } else MessageBox.Show("An error occurred. Please try later."); } else if (ver == "newest") { MessageBox.Show("You are currently using the newest version."); } 

And change your Update_Load to:

 WebClient webClient = new WebClient(); webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed); webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged); webClient.DownloadFileAsync(new Uri(dlUrl), ""); 
+1
source

Perhaps first hide it, and then show only if the function () == "1":

 private void Form_Load(object sender, EventArgs e) { this.Close(); if (funct() == "1") MessageBox.Show("Something"); } 
0
source

try it

 private void Form_Load(object sender, EventArgs e) { switch(funct()) { case 2: this.Close(); break; case 3: this.Close(); break; default: MessageBox.Show("Something"); } } 
0
source

All Articles