C++ Tutorial: Status Strip Control in C++/CLI

   Today we are going to talk about such component in Visual Studio 2012 as Status Strip. With component is mostly situated at the bottom of the main form and it shows progress of some event, warnings, errors or other additional information. In today’s program we will use this component. Preparation    You need to create new Windows Forms Application project. Then drag Status Strip component on the form and will be added to the project. After that click on it and choose two components from the popup dialog – Progress Bar and Status Label. Later add Background Worker component to release multithreading. Also add two buttons to enable starting and canceling Background Worker. Then you need to configure Background Worker. Enable such properties as Worker Report Progress and Worker Support Cancellation. Also functions to catch such events: Do Work, Progress Changed, Run Worker Completed. Code After you have done preparation, let’s code. Paste we code below:
  1. private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
  2.                                  backgroundWorker1->RunWorkerAsync(1);
  3.                          }
  4.         private: System::Void backgroundWorker1_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) {
  5.                                 using namespace System::Threading;
  6.                                 while(true)
  7.                                 {
  8.                                  if(backgroundWorker1->CancellationPending) //if it was cancelled
  9.                                         {
  10.                                                 e->Cancel=true;
  11.                                                 break;
  12.                                         }
  13.                                  if(toolStripProgressBar1->Value==toolStripProgressBar1->Maximum)  //if the progress bar value reached maximum
  14.                                  {
  15.                                          break;
  16.                                  }
  17.        
  18.                                  backgroundWorker1->ReportProgress(10);  //reporting progress
  19.                                  Thread::Sleep(1000);   //wait for 1 second
  20.                                 }
  21.                          }
  22. private: System::Void backgroundWorker1_ProgressChanged(System::Object^  sender, System::ComponentModel::ProgressChangedEventArgs^  e) {
  23.  
  24.                          toolStripProgressBar1->Value+=e->ProgressPercentage;
  25.                          toolStripStatusLabel1->Text=(((double)toolStripProgressBar1->Value/(double)toolStripProgressBar1->Maximum)*100).ToString() + " %";toolStripStatusLabel1->Text=(((double)toolStripProgressBar1->Value/(double)toolStripProgressBar1->Maximum)*100).ToString() + " %";
  26.                  }
  27. private: System::Void backgroundWorker1_RunWorkerCompleted(System::Object^  sender, System::ComponentModel::RunWorkerCompletedEventArgs^  e) {
  28.  
  29.                          toolStripProgressBar1->Value=0;
  30.                          toolStripStatusLabel1->Text=""; //reseting value
  31.  
  32.                          if(e->Cancelled)    //Messages for the events
  33.                          {
  34.                                  MessageBox::Show("You have cancelled background worker!!!");
  35.                          }
  36.                          else
  37.                          {
  38.                                  MessageBox::Show("Work completed!!");
  39.                          }
  40.                  }
  41. private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
  42.  
  43.                          backgroundWorker1->CancelAsync();
  44.                  }
   In this code you can see : First button starts the thread. Then thread is started, background worker adds to the value of the progress bar from the Status Strip 10 points. After that we make output of how many work is done. Let’s see this line closer. First of all we convert values of progress bar to double and then we find ratio in percent. And to add it to label we convert it to String type and add “ %”. Then the work is completed we see the message. If any questions you are welcome. Thanks.

Add new comment