Post: MessageBox.Show is instant (ProgressBar)
12-17-2011, 04:08 PM #1
Chrom3D
Big Sister
(adsbygoogle = window.adsbygoogle || []).push({}); Hi Smile

Everytime I debug it the message box goes instant, anyway to avoid this?

Originally posted by another user
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}

private void timer1_Tick(object sender, EventArgs e)
{
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
progressBar1.Step = 10;
progressBar1.PerformStep();
if (progressBar1.Value == progressBar1.Maximum)
{MessageBox.Show("100%");
}
if (progressBar1.Value == progressBar1.Maximum)
{
progressBar1.Value = 99;
}
}
}
}
12-17-2011, 04:43 PM #2
slim355
You talkin to me?
What is your timer interval? What do you mean by "goes instant"?
12-17-2011, 04:51 PM #3
Chrom3D
Big Sister
Originally posted by PR1VATEJ0KER View Post
What is your timer interval? What do you mean by "goes instant"?


1000, and once the progressbar reaches 100% the messagebox doesen't stop to pop up. I want it to pop up once, not unlimited.

I've tried "timer1.Enabled = false;" which makes it stop once I press "Ok" on one of the messages.

---------- Post added at 11:51 AM ---------- Previous post was at 11:47 AM ----------

Originally posted by PR1VATEJ0KER View Post
What is your timer interval? What do you mean by "goes instant"?


P.S it's nothing important, but would be nice to find the solution.
12-17-2011, 04:59 PM #4
slim355
You talkin to me?
Originally posted by Chrom3D View Post
1000, and once the progressbar reaches 100% the messagebox doesen't stop to pop up. I want it to pop up once, not unlimited.

I've tried "timer1.Enabled = false;" which makes it stop once I press "Ok" on one of the messages.

---------- Post added at 11:51 AM ---------- Previous post was at 11:47 AM ----------



P.S it's nothing important, but would be nice to find the solution.


Ah ok, try it before the MessageBox

if (progressBar1.Value == progressBar1.Maximum)
{
Timer1.Stop(); // or Timer1.Enabled = False //Should work also
MessageBox.Show("100%");
}

The following user thanked slim355 for this useful post:

Chrom3D
12-17-2011, 05:03 PM #5
Chrom3D
Big Sister
Originally posted by PR1VATEJ0KER View Post
Ah ok, try it before the MessageBox

if (progressBar1.Value == progressBar1.Maximum)
{
Timer1.Stop(); // or Timer1.Enabled = False //Should work also
MessageBox.Show("100%");
}


Thanks man. It worked =D Didn't think it was that easy.
12-17-2011, 05:06 PM #6
slim355
You talkin to me?
No Problem man, Ive bumped into these problems myself over time of doing various projects for college.

The thing here is that the MessageBox is 'modal', that means you must interact with it before the next line of code is executed.

But, the Timer1_Tick event kept firing every 1000 milliseconds and re-Entering your if statement showing another MessageBox each tick. Winky Winky
Last edited by slim355 ; 12-17-2011 at 05:11 PM.
12-19-2011, 09:44 PM #7
Pichu
RIP PICHU.
Originally posted by Chrom3D View Post
Hi Smile

Everytime I debug it the message box goes instant, anyway to avoid this?


Inside the timer tick do this

progressBar1.Interval(1);
if (progressBar1.Value == progressBar1.Maximum)
{
timer1.Stop();
progressBar1.Value = 0;
MessageBox.show("100%");
}

If you want to randomize the interval you can do this:

Random rnd = new Random();

int a = rnd.Next(1,10);
progressBar1.increment(a);

You don't need to set a new minimum or maximum unless you are changing it form the original which I doubt you are.
12-19-2011, 10:47 PM #8
Chrom3D
Big Sister
Originally posted by Sublimity View Post
Inside the timer tick do this

progressBar1.Interval(1);
if (progressBar1.Value == progressBar1.Maximum)
{
timer1.Stop();
progressBar1.Value = 0;
MessageBox.show("100%");
}

If you want to randomize the interval you can do this:

Random rnd = new Random();

int a = rnd.Next(1,10);
progressBar1.increment(a);

You don't need to set a new minimum or maximum unless you are changing it form the original which I doubt you are.


if (progressBar1.Value == progressBar1.Maximum)
{
timer1.Stop();
progressBar1.Value = 0;
MessageBox.show("100%");
}

So basically when the value equals the maximum the timer will stop, and return to 0? and the messagebox will execute?

I don't understand why you had the message box there ?

Anyways, thanks for the great info, I'll try remember it Winky Winky
12-19-2011, 10:55 PM #9
Pichu
RIP PICHU.
Originally posted by Chrom3D View Post
if (progressBar1.Value == progressBar1.Maximum)
{
timer1.Stop();
progressBar1.Value = 0;
MessageBox.show("100%");
}

So basically when the value equals the maximum the timer will stop, and return to 0? and the messagebox will execute?

I don't understand why you had the message box there ?

Anyways, thanks for the great info, I'll try remember it Winky Winky


Yea, you need to stop the timer or else the messagebox will repeat infinitely and crash the application.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo