Message Box Challenge
-
Hi All Check the code first 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 MessageBoxChallenge { public partial class Form1 : Form { private ProgressBar pb = new ProgressBar(); private Button b = new Button(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { pb.Value = 5; pb.Visible = true; pb.Left = 20; pb.Top = 30; pb.Style = ProgressBarStyle.Blocks; this.Width = 500; pb.Width = 400; this.Controls.Add(pb); b.Text = "click"; b.Top = 300; b.Left = 50; b.Visible = true; this.b.Click += new EventHandler(b_Click); this.Controls.Add(b); } void b_Click(object sender, EventArgs e) { pb.Value = 0; int inc = Convert.ToInt32(100 / 5); DialogResult result = MessageBox.Show("Do you want to continue", "Header", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { for (int i = 0; i <= 4; i++) { System.Threading.Thread.Sleep(300); pb.Value = pb.Value + inc; } } else { this.Dispose(); this.Close(); } } } } In actual problem I have a code which is taking around 300ms for each iteration in the for lood thats why i placed System.Threading.Thread.Sleep(600) in the for loop. Now the problem is When the MessageBox appears and I click on Yes Button, Out of 10 times ,3 times the MessageBox still remains visible even after clicking on the Yes button against the actual behavior of becoming invisible/hiding immediately after clicking the Yes button of MessageBox. Note that On my friend machine this problem does not exists. I do not know .Whats the actual problem is. Please help me with any study material or code. Thanks Regards THE SK
-
Hi All Check the code first 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 MessageBoxChallenge { public partial class Form1 : Form { private ProgressBar pb = new ProgressBar(); private Button b = new Button(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { pb.Value = 5; pb.Visible = true; pb.Left = 20; pb.Top = 30; pb.Style = ProgressBarStyle.Blocks; this.Width = 500; pb.Width = 400; this.Controls.Add(pb); b.Text = "click"; b.Top = 300; b.Left = 50; b.Visible = true; this.b.Click += new EventHandler(b_Click); this.Controls.Add(b); } void b_Click(object sender, EventArgs e) { pb.Value = 0; int inc = Convert.ToInt32(100 / 5); DialogResult result = MessageBox.Show("Do you want to continue", "Header", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { for (int i = 0; i <= 4; i++) { System.Threading.Thread.Sleep(300); pb.Value = pb.Value + inc; } } else { this.Dispose(); this.Close(); } } } } In actual problem I have a code which is taking around 300ms for each iteration in the for lood thats why i placed System.Threading.Thread.Sleep(600) in the for loop. Now the problem is When the MessageBox appears and I click on Yes Button, Out of 10 times ,3 times the MessageBox still remains visible even after clicking on the Yes button against the actual behavior of becoming invisible/hiding immediately after clicking the Yes button of MessageBox. Note that On my friend machine this problem does not exists. I do not know .Whats the actual problem is. Please help me with any study material or code. Thanks Regards THE SK
If you want your UI to always respond when your code is doing some calculations, you need to do those calculations in another thread. Application.DoEvents() between your message box and your other code is a hack that may work.
Christian Graus Driven to the arms of OSX by Vista.
-
Hi All Check the code first 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 MessageBoxChallenge { public partial class Form1 : Form { private ProgressBar pb = new ProgressBar(); private Button b = new Button(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { pb.Value = 5; pb.Visible = true; pb.Left = 20; pb.Top = 30; pb.Style = ProgressBarStyle.Blocks; this.Width = 500; pb.Width = 400; this.Controls.Add(pb); b.Text = "click"; b.Top = 300; b.Left = 50; b.Visible = true; this.b.Click += new EventHandler(b_Click); this.Controls.Add(b); } void b_Click(object sender, EventArgs e) { pb.Value = 0; int inc = Convert.ToInt32(100 / 5); DialogResult result = MessageBox.Show("Do you want to continue", "Header", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { for (int i = 0; i <= 4; i++) { System.Threading.Thread.Sleep(300); pb.Value = pb.Value + inc; } } else { this.Dispose(); this.Close(); } } } } In actual problem I have a code which is taking around 300ms for each iteration in the for lood thats why i placed System.Threading.Thread.Sleep(600) in the for loop. Now the problem is When the MessageBox appears and I click on Yes Button, Out of 10 times ,3 times the MessageBox still remains visible even after clicking on the Yes button against the actual behavior of becoming invisible/hiding immediately after clicking the Yes button of MessageBox. Note that On my friend machine this problem does not exists. I do not know .Whats the actual problem is. Please help me with any study material or code. Thanks Regards THE SK
Most probably, as you said your code works fine in your frnds computer. Might be your computer is getting hanged. And as you are keeping a sleep command immediately after the message box. What might be happening that when you fire a sleep command , the thread on which your whole application is working is paused. and hence the message box might not be able to get invisible , as the application is in sleep mode.. Or one more thing, Do one thing , keep any integer variable and show it in the message box displaying no of times message box is opened. This might give you an idea that after pressing YES , might be the new message box have appeared but because of very short time span , you might not be able to visualise the changes. .
-
Hi All Check the code first 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 MessageBoxChallenge { public partial class Form1 : Form { private ProgressBar pb = new ProgressBar(); private Button b = new Button(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { pb.Value = 5; pb.Visible = true; pb.Left = 20; pb.Top = 30; pb.Style = ProgressBarStyle.Blocks; this.Width = 500; pb.Width = 400; this.Controls.Add(pb); b.Text = "click"; b.Top = 300; b.Left = 50; b.Visible = true; this.b.Click += new EventHandler(b_Click); this.Controls.Add(b); } void b_Click(object sender, EventArgs e) { pb.Value = 0; int inc = Convert.ToInt32(100 / 5); DialogResult result = MessageBox.Show("Do you want to continue", "Header", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { for (int i = 0; i <= 4; i++) { System.Threading.Thread.Sleep(300); pb.Value = pb.Value + inc; } } else { this.Dispose(); this.Close(); } } } } In actual problem I have a code which is taking around 300ms for each iteration in the for lood thats why i placed System.Threading.Thread.Sleep(600) in the for loop. Now the problem is When the MessageBox appears and I click on Yes Button, Out of 10 times ,3 times the MessageBox still remains visible even after clicking on the Yes button against the actual behavior of becoming invisible/hiding immediately after clicking the Yes button of MessageBox. Note that On my friend machine this problem does not exists. I do not know .Whats the actual problem is. Please help me with any study material or code. Thanks Regards THE SK
try to refresh the main form that will ensure all the UI changes to be commited before moving on, check out either of the two methods, might work!!!
this.Invalidate(); //or this.Refresh();
-
Hi All Check the code first 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 MessageBoxChallenge { public partial class Form1 : Form { private ProgressBar pb = new ProgressBar(); private Button b = new Button(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { pb.Value = 5; pb.Visible = true; pb.Left = 20; pb.Top = 30; pb.Style = ProgressBarStyle.Blocks; this.Width = 500; pb.Width = 400; this.Controls.Add(pb); b.Text = "click"; b.Top = 300; b.Left = 50; b.Visible = true; this.b.Click += new EventHandler(b_Click); this.Controls.Add(b); } void b_Click(object sender, EventArgs e) { pb.Value = 0; int inc = Convert.ToInt32(100 / 5); DialogResult result = MessageBox.Show("Do you want to continue", "Header", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { for (int i = 0; i <= 4; i++) { System.Threading.Thread.Sleep(300); pb.Value = pb.Value + inc; } } else { this.Dispose(); this.Close(); } } } } In actual problem I have a code which is taking around 300ms for each iteration in the for lood thats why i placed System.Threading.Thread.Sleep(600) in the for loop. Now the problem is When the MessageBox appears and I click on Yes Button, Out of 10 times ,3 times the MessageBox still remains visible even after clicking on the Yes button against the actual behavior of becoming invisible/hiding immediately after clicking the Yes button of MessageBox. Note that On my friend machine this problem does not exists. I do not know .Whats the actual problem is. Please help me with any study material or code. Thanks Regards THE SK
Hi, use a BackgroundWorker and show a dialog with progress bar and cancel button. It is the decent way to offer information and control to a user while doing a long operation. Don't bombard the user with lots of MessageBoxes, they are just annoying people. There are some excellent articles about the subject here on CodeProject. :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets