Progress bar doesn't function well - c#

I have an app that reads the smart cards and take a little bit long time
so I need to show a progress bar. However, I followed some tutorials it didn't work for me. The problem is,as seen in the picture, the progress bar remains at the same point even after filling the fields with the data.
These are my codes, as shown within them, I want the progress bar to be completed when data displayed equals to true, and then hide it along with the "Pleas Wait !!!" label.
public void DataDisplayed()
{
//bool displayedData = false;
int t;
progressBar1.Minimum = 0;
progressBar1.Maximum = 200;
if (IDNumbertxt.Text != "" || FullNametxt.Text != "")
{
displayedData = true;
}
else
{
displayedData = false;
}
if (displayedData == false)
{
for (t = 0; t <= 100; t++)
{
progressBar1.Value = t;
}
}
else if (displayedData == true)
{
progressBar1.Value = 200;
waitlbl.Hide();
progressBar1.Hide();
}
Pleas, anyone could help ?
Thanks in advance

Try This.
for (t = 0; t <= 200; t++)
{
progressBar1.Value = t;
}
because your for loop stop at 100 which is the middle value of your progress bar
UPDATE
if (displayedData == false)
{
for (t = 0; t <= 100; t++)
{
progressBar1.Value = t;
}
}
else
{
progressBar1.Value = 200;
waitlbl.Hide();
progressBar1.Hide();
}
if you are using boolean you don't need to use an else if statement because boolean have only two answer it's either True or False
Also because your progressBar1.Hide(); was inside your else condition the progress bar will hide almost instantly after the progress bar value change to 200

Related

Progress Bar Do Work Backgroundworker C# WinForms

hope anybody can help me. My problem is the progress bar in C# WinForms. I have the following Code:
(There is a stupid calculate from an uint until a given number from a textbox and i want to show the progress while the calculate method is running)
// The stupid method which calculate
public void ueberlaufUint()
{
try
{
uint ueberlaufZahl = Convert.ToUInt32(textBox1.Text);
do
{
ueberlaufZahl++;
//Console.WriteLine(ueberlaufZahl);
} while (ueberlaufZahl <= 100);
label1.Text = "Endzahl: " + ueberlaufZahl;
}
catch (Exception)
{
MessageBox.Show("Only not negative natural numbers accepted");
}
}
// Buttonclickevent
private async void button1_Click(object sender, EventArgs e)
{
ueberlaufUint();
progressBar1.Maximum = 100;
progressBar1.Step = 1;
var progress = new Progress<int>(v =>
{
// This lambda is executed in context of UI thread,
// so it can safely update form controls
progressBar1.Value = v;
});
// Run operation in another thread
await Task.Run(() => DoWork(progress));
}
// DoWork
public void DoWork(IProgress<int> progress)
{
// This method is executed in the context of
// another thread (different than the main UI thread),
// so use only thread-safe code
for (int j = 0; j < 10000; j++)
{
ueberlaufUint();
// Use progress to notify UI thread that progress has
// changed
if (progress != null)
progress.Report((j + 1) * 100 / 100000);
}
}
The progressbar only counts few steps with no dependency (in my meaning) with the calculate method.
Very great thanks in forward, sorry for my bad english.
Just a typo. You have an extra 0 in the code:
progress.Report((j + 1) * 100 / 100000);
should be
progress.Report((j + 1) * 100 / 10000);

Pause and resume a chart and text box from updating with button click C# forms

So want to be able to pause and resume my chart and text box from updating when I click a button. Currently, now it just updates in real-time time. I have attached my code below, I've tried ResumeUpdates() and PauseUpdates() for chart but it does not work for the textboxes I have no idea. The data im getting is stored in an ArrayList with originally comes from a serial port
Edit: if I have initraph() within mmy button function and call it from there it calls it once and gets updated once.
first button click =chart and textboxes updates in realtime
second button click = chart and textboxes pause
third button click = chart and textboxes resume updating
private void InitChart()
{
if (!b)
{
speedRecordChart.ChartAreas[0].AxisX.Maximum = 200;
speedRecordChart.ChartAreas[0].AxisX.Minimum = 0;
if (position.lat1 > 90)
{
textBox_analLat.Text = position.latComp1 + position.lat1 / 100;
}
else
{
textBox_analLat.Text = position.latComp1 + position.lat1;
}
if (position.lng1 > 180)
{
textBox_analLong.Text = position.lngComp1 + position.lng1 / 100;
}
else
{
textBox_analLong.Text = position.lngComp1 + position.lng1;
}
speedRecordChart.Series["Speed"].Points.Clear();
for (int i = 0; i < totalSpeedList.Count() - 1; ++i)
{
speedRecordChart.Series["Speed"].Points.AddY(totalSpeedList[i]);
}
Console.WriteLine("speedChart : " + totalSpeedList);
}
return;
}
/*****************************************for button**********/
Boolean b = true;
private void button_Resume_Click(object sender, EventArgs e)
{
if (b == true)
{
button_Resume.Text = "resume";
//speedRecordChart.Series.ResumeUpdates();
// serialPort1.Close();
InitChart();
}
else
{
button_Resume.Text = "pause";
// speedRecordChart.Series.SuspendUpdates();
// serialPort1.Open();
}
b = !b;
}

How to load a progress bar in c# win form?

I am trying to open a winForm in C# VS2012. and using this code structure for loading form. I am trying to see the loading status of form How can I do it.fm.LoadingStatus(); is for notation purpose which function i will call to show the correct loading status.
.
.
formOpen = Application.OpenForms["student"];
if (formOpen == null)
{
fm = new student();
fm.TopLevel = false;
fm.Parent = this.panel1;
fn.Show(); //I want to show this progress in loaging bar
int i=0;
progressBar1.Visible = true;
progressBar1.Value = 0;
progressBar1.Minimum = 0;
progressBar1.Maximum = ***fm.LoadingStatus()***;
progressBar1.Step = 100;
for (i = 0; i <= 100; i++)
{
progressBar1.PerformStep();
}
}
.
.

How to make two pictures not visible after a certain time

I'm making a memotest and I need to press two different images and they need to keep visible for 3-5 seconds. I've tried Thread.Sleep(5000) but it doesn't show me the second one. What should I do?
The only way I've founded to see for some seconds the images was by putting a MessageBox but that isn't the idea and I don't know other way to do it.
if (pic != null && pic.Name == fondos[i].Name)
{
if (CantClick == 0)
{
ParejaActual = listRandom[i].pareja;
CantClick = 1;
primerI = i;
picAnterior = pic;
imgAnterior = img;
pic.Visible = false;
}
else if (CantClick == 1)
{
pic.Visible = false;
if (ParejaActual == listRandom[i].pareja)
{
SoundPlayer simpleSound = (new SoundPlayer(Configuracion.RootFolder + "aplau.wav"));
simpleSound.Play();
Ganando++;
label3.Text = Ganando.ToString();
//MessageBox.Show("Si");
//NO SE DESTAPA LA SEGUNDA.
//Thread.Sleep(5000);
CantClick = 0;
img.Visible = false;
imgAnterior.Visible = false;
Application.DoEvents();
}
else
{
(new SoundPlayer(Configuracion.RootFolder + "sad.wav")).Play();
MessageBox.Show("No");
Mal++;
CantClick = 0;
label4.Text = Mal.ToString();
pic.Visible = true;
picAnterior.Visible = true;
}
}
}
Thank you!
Instead of using Thread.Sleep , use System.Timers class. After an interval, just hide one image and show the other. Tell me if you want any other help.

How to set value of progress bar

I have written a user control using C# Winforms. In the user control, I have three textboxes:
txtStartNumber - input is of type: int.
txtEndNumber - input is of type: int.
txtQuantity - iput is of type: int. (value = txtEndNumber - txtStartNumber)
The progress bar denotes the no. of records added to the database and its total range is set to be equal to txtQuantity.
When one or more records are duplicate, the progress bar is stopped.
My questions are:
How to set the initial value of the progress bar?
How to manage the progress shown by progress bar?
How I save it to the database:
for (long i = from; i < to; i++)
{
for (int j = 0; j < (to - from); j++)
{
arrCardNum[j] = from + j;
string r = arrCardNum[j].ToString();
try
{
sp.SaveCards(r, 2, card_Type_ID, SaveDate, 2);
progressBar1.Value = j;
}
}
}
Try this:
private void StartBackgroundWork() {
if (Application.RenderWithVisualStyles)
progressBar.Style = ProgressBarStyle.Marquee;
else {
progressBar.Style = ProgressBarStyle.Continuous;
progressBar.Maximum = 100;
progressBar.Value = 0;
timer.Enabled = true;
}
backgroundWorker.RunWorkerAsync();
}
private void timer_Tick(object sender, EventArgs e) {
progressBar.Value += 5;
if (progressBar.Value > 120)
progressBar.Value = 0;
}
The Marquee style requires VisualStyles to be enabled, but it continuously scrolls on its own without needing to be updated. I use that for database operations that don't report their progress.
Here is another Progress Bar Tutorial
You can't use loop to do this with progressbar. There is a difference between running code in for, while, do...while loops or in timers. In loops code is immediately done and you can't see this, in timers you can. Even if you try to put in loops if counters, it will not works:
for(int i=a;i<b;++i)
{
if (cnt < 1000000)
{
IncrProgressBar();
cnt++;
}
else
{
cnt = 0;
}
}
If you want to use progressbar to do this then you must put in timer OnTick event code that adds data to database, and in this event increment progressbar value. It's similarly with changing form component's other properties (Text, Size, ...). If you want to see change on component you must use timers.
To change the value use:
private void timer1_Tick(object sender, EventArgs e)
{
progressBar2.Value = progressBar2.Value - 15;
}
In C#

Categories