How to use Progress Bar with my Functions [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Its my code, Can anyone please suggest me how i can put my progress bar so it can perform progress with function simultaneously..
i used progress bar in my application.
private void button1_Click(object sender, EventArgs e)
{
canaryTimer.Start();
canaryTimer.Interval = 1000;
canaryTimer.Tick += new EventHandler(canaryTimer_Tick);
PickDatafromTextFile();
CallRichtextbox();
GenerateExcel();
DeleteExcelRows();
SplitDateandTime();
SortDateandTime();
CombineDataoftwoExcel();
UpdateExcelFile();
}
private void canaryTimer_Tick(object sender, EventArgs e)
{
for (int i = 0; i <= 100; i++)
{
canaryProgressBar.Value=i;
}
}

A very simple suggestion:
Update your Progressbar from within the Button1_Click after each function completes.
Assuming your myProgressBar.Value is initialized with 0;
Example:
PickDatafromTextFile();
myProgressBar.Value += 14;
CallRichtextbox();
myProgressBar.Value += 14;
GenerateExcel();
myProgressBar.Value += 14;
DeleteExcelRows();
myProgressBar.Value += 14;
SplitDateandTime();
myProgressBar.Value += 14;
SortDateandTime();
myProgressBar.Value += 14;
CombineDataoftwoExcel();
myProgressBar.Value += 14;
UpdateExcelFile();
myProgressBar.Value = 100;
I know this isn't the correct way to do so. But for a simple task like this any other method would seem overkill to me.

I don't get the point of your question... If you want to load a textfile and than work with the information, you have to check out how much percent of the work is done. after that, you can increase the progressbar with this command:
progressBar1->Increment(1);
also you can set it to a value
progressBar1->Value = 5;

Related

How do I update several controls on my Form1 using BackgroundWorker?

I found several examples that shows how to update UI control using BackgroundWorker.
For example the following code:
BackgroundWorker backgroundWorker1 = new BackgroundWorker();
backgroundWorker1.WorkerReportsProgress = true;
//start the operation on another thread
private void btnStart_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
//DoWork event handler is executed on a separate thread
private void backgroundWorker1_DoWork(object sender, DoWorkeventArgs e)
{
//a long running operation
for (int i = 1; i < 11; i++)
{
Thread.Sleep(2000);
backgroundWorker1.ReportProgress(i * 10);
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.value = e.ProgressPercentage;
}
That I found here:
https://www.c-sharpcorner.com/UploadFile/Ashush/using-the-backgroundworker-component
The problem is that ALL the examples that I found, shows how to update Only One control on the form. For example the code that I gave, demonstrate how to update a progressBar value.
But this is not a realistic situation, in the real world, I would usually want to update SEVERAL controls on my form.
For example:
progressBar1.Value = 54;
listBox1.SelectedIndex = 2;
button1.BackgroundColor = Color.Yellow;
image1.left = image1.left + 20;
How do I change the code that I showed, in order to update the controls that I gave in the previous lines? There must be an easy way to do that. By the way, my application uses several threads, and each one of them have to update several controls on the UI while they are still running...
I Hope that my question is clear,
Thanks.
Edit:
Neil suggested me to find the solution in this thread:
How do I update the GUI from another thread?
But I saw that people there recommended using BackgroundWorker... which is exactly what I'm asking about.
They didn't explain how to use this method in order to update several UI controls in parallel.
Try something like this:
button1.Invoke(
new Action(
() =>
button1.BackgroundColor = Color.Yellow;
)
);
image1.Invoke(
new Action(
() =>
image1.left = image1.left + 20;
)
);
//etc.

C# Resize picturebox with timer [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am not very good at this, but I have an idea, and I want to ask here if it is possible. So I want to make, so PictureBox grows bigger, grows to some size and then goes smaller again, and continue doing this. I have only figured out how to make, so it goes bigger or smaller, but I can not figure out how to make so it detects specific size, and then do the opposite. The code I use.
Size size = pictureBox.Size;
size.Height--;
size.Width--;
pictureBox.Size = size;
Just an example, since you didn't specify your criterion for enlarging and reducing. Initialize your timer:
// set the interval you prefer
System.Timers.Timer timer = new System.Timers.Timer(500);
timer.Elapsed += OnElapsed;
timer.AutoReset = true;
timer.Enabled = true;
Create a member variable that controls whether the PictureBox must be enlarged or reduced:
private Boolean m_Reducing = false;
And then put everything together in your subscribed timer handler:
private static void OnElapsed(Object sender, ElapsedEventArgs e)
{
Size size = pictureBox.Size;
if (m_Reducing)
{
--size.Height;
--size.Width;
if ((size.Width == minimumWidth) && (size.Height == minimumHeight))
m_Reducing = false;
}
else
{
++size.Height;
++size.Width;
if ((size.Width == maximumWidth) && (size.Height == maximumHeight))
m_Reducing = true;
}
pictureBox.Size = size;
}

How to execute the multiple timers in different time interval using window scheduler....? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How to execute the multiple timers in different time interval using window scheduler....?
Is it possible using this functionality in .net...?
Thanks
You can also use single timer for different time intervals
for example:
private void Form_Load()
{
timer1.Interval = 1000; // 1 second
timer1.Start(); // Start timer, This will raise Tick event after 1 second
OnTick(); // So, call Tick event explicitly when we start timer
}
Int32 counter = 0;
private void timer1_Tick(object sender, EventArgs e)
{
OnTick();
}
private void OnTick()
{
if (counter % 1 == 0)
{
OnOneSecond();//Write your code in this method for one second
}
if (counter % 2 == 0)
{
OnTwoSecond();
}
counter++;
}

Command instead of 'Goto' in C# VSTO [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
what is another command for 'Goto' ? I am using VSTO to make a ribbon for excel and is doesnt seem to support Goto, and I am trying to create a loop.
Edit: This is the loop i am trying to create :
TimeSpan startTimeSpan = new TimeSpan(0, 0, 0, 20);
TimeSpan timeDecrease = TimeSpan.FromSeconds(1);
private Timer timer = new Timer();
public void Ribbon_Load(Office.IRibbonUI ribbonUI)
{
timer.Interval = 1000;
this.ribbon = ribbonUI;
timer.Tick += timer_Tick;
timer.Start();
}
private void timer_Tick(object sender, EventArgs e)
{
if (startTimeSpan.ToString() != "00:00:00")
{
startTimeSpan = startTimeSpan - timeDecrease;
ribbon.InvalidateControl("timerLabel");
}
else
{
//when timer drop to "00:00:00" then loop to "TimeSpan startTimeSpan = new TimeSpan(0, 0, 0, 20);"
}
}
You probably want to be using a while loop, with the exit condition in the while statement, and use break to quit the loop before the exit condition is reached, or continue to skip the current iteration of the loop and go on to the next.
C# does support goto command, but it isn't for looping. Jump statements are very often not the best solution.
goto - http://msdn.microsoft.com/en-us/library/13940fs2.aspx
If you are trying to loop just use a looping statement.
looping - http://msdn.microsoft.com/en-us/library/ms228598(v=vs.90).aspx
You can find a lot of q&a on excel VSTO.
Whats wrong with something like:
for (int i = selectedRange.Rows.Count; i > 0; i--)
{
---YOUR CODE HERE---
}
OR
foreach (Excel.Range row in rng.Areas[1].Rows)
{
---YOUR CODE HERE---
}

Using ProgressBar for specific time [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I want my ProgressBar starting in some point in my code and run total of seconds until my file finish, and of course I know how long the run of my file will take.
I try to read on MSDN but I did not understood how to use it.
My application run files (wireshark file, send the packet using bittwist) and each file will run few seconds and I want the option to see the progress ongoing.
For example I want to set my ProgressBar running for 30 seconds.
How can I do it?
Maybe you want something like this:
public void AnimateProgBar (int milliSeconds)
{
if (!timer1.Enabled) {
progressBar1.Value = 0;
timer1.Interval = milliSeconds / 100;
timer1.Enabled = true;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (progressBar1.Value < 100) {
progressBar1.Value += 1;
progressBar1.Refresh();
} else {
timer1.Enabled = false;
}
}
Then you just have to call AnimateProgBar(2000) to have your ProgressBar animated during 2 seconds.
EDIT: Sorry, I posted code in VB.NET. Modified to C#.
EDIT: You can add the handler and call the function in this way (for example):
private void Form1_Load(object sender, EventArgs e)
{
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
AnimateProgBar(2000);
}

Categories