Saturday, May 4, 2013

Progress bar with process C#

How to attach a process bar with a process in C#...


Let we want to add 1 to 100 numbers in a list box and runs a progress bar to show the process percentage.

1. Create a window form application. Design a form with a Listbox to insert the values and with a progress bar to show the process. Also a button to start that process.

2. Now add a backgroundworker control from toolbox. That will add at the bottom of form.

3. You have to add a method for that task you want to perfrom (i.e. in this example we want to add 1 to 100 number in a list. so i create a task method) with the parameters BackgroundWorker bw and DoWorkEventArgs  like 

4. Set Property of Backgroundworker controls.
WorkerReportProcess=true
WorkerSupportCance=true
from property window


   public void task(BackgroundWorker bw, DoWorkEventArgs e)
        {
            if (bw.CancellationPending)  // if cancel button pressed  
            {
                e.Cancel = true;  
            }
            else
            {
                for (int i = 0; i<= 100; i++)
                {
                    listBox1.Items.Add(i);  
                    bw.ReportProgress(i);  //Gives the report of process (value of i as process %age used).
                    System.Threading.Thread.Sleep(1000); // for delay in execution
                }
            }
        } 


     5. Generate the BacgroundWorker.Dowork event by clicking the event from events of backgroundworker.

         
       private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker bw = (BackgroundWorker)sender;
            task(bw, e);
        }


  6. To Change the process percentage of ProgressBar1  use ProgressChanged event of BackgroundWorker       control.


private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
        }

7. To show the massage when the process is completed. work on RunWorkerCompleted event of backgroundWorker1 like


   private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            MessageBox.Show("Work Compleated!!!");
        }

8. We use "Thread.Sleep(...)" method of threading. so we also have to set CheckForIllegalCrossThreadCalls = false;  on page_load event


private void Form1_Load(object sender, EventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;
        }

9. To start procss on the button write in button1_click(...)



 private void button1_Click(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }

10. You may take a "Cancel" button on form to cancel the running process. For cancel the process you write the code.


   private void button2_Click(object sender, EventArgs e)
        {
            backgroundWorker1.CancelAsync();
        }


Now as you click the Start Process button the process will start and progress bar show how much process is completed!!!

Write your comment about this post and also welcome for the batterment suggestions...


Thursday, April 12, 2012

Bubble Sort



#include
#include
void main()
{
int ar[5];
int i,j,temp;
printf("Enter 5 element for array:\n");
for(i=0;i<5;i++)
{
scanf("%d",&ar[i]);
}
for(i=0;i<5;i++)
{
for(j=0;j<5-i-1;j++)
{
if(ar[i]>ar[j])
{
temp=ar[i];
ar[i]=ar[j];
ar[j]=temp;
}
}
}
printf("Sorted Array:\n");
for(i=0;i<5;i++)
{
printf("%d\n",ar[i]);
}
getch();
}

Saturday, April 23, 2011

Insertion sort in 'c'

Here, sorting takes place by inserting a particular element at the appropriate position, that’s why the name- insertion sorting. In the First iteration, second element A[1] is compared with the first element A[0]. In the second iteration third element is compared with first and second element. In general, in every iteration an element is compared with all the elements before it. While comparing if it is found that the element can be inserted at a suitable position, then space is created for it by shifting the other elements one position up and inserts the desired element at the suitable position. This procedure is repeated for all the elements in the list.

#include
void main()
{  
 int A[20], N, Temp, i, j;  
 clrscr();  
 printf("\n\n\t ENTER THE NUMBER OF TERMS...: ");
 scanf("%d", &N);  printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:");
 for(i=0; i<N; i++)  
 {      
  scanf("\n\t\t%d", &A[i]);  
 }  
 for(i=1; i<N; i++)
 {   
  Temp = A[i];   
  j = i-1;   
  while(Temp<A[j] && j>=0)   
  {    
   A[j+1] = A[j];    
   j = j-1;   
  }   A[j+1] = Temp;  
 }  
 printf("\n\tTHE ASCENDING ORDER LIST IS...:\n");  
 for(i=0; i<N; i++)
 {   
  printf("\n\t\t\t%d", A[i]);
 }  
 getch(); 
}