Track Bars


A track bar is a control made of a small bar that slides along another bar. To create a track bar in MSVC .NET, you use the TrackBar class.

Track Bar Example
  1. Start Microsoft Visual Studio .NET
  2. On the Start Page, click New Project (alternatively, on the main menu, you can click File -> New -> Project...)
  3. On the New Project dialog box, in the Project Types tree list, click Visual C++ Projects
  4. In the Templates list, click Managed C++ Empty Project
  5. In the Name edit box, replace the <Enter name> content with TrackBar Example
  6. In the Location combo box, accept the suggestion or type your own. If you don't have any, type C:\Programs\MSVC.NET
  7. Click OK
  8. On the main menu, click Project -> Add New Item...
  9. In the Add New Item dialog box, in the Templates list, click C++ File
  10. In the Name box, replace <Enter name> with Main and click OK
  11. Replace the contents of the empty file with the following (I have commented the whole file to indicate what each piece of code does):
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Drawing.dll>
    #using <System.Windows.Forms.dll>
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Windows::Forms;
    __gc public class SimpleForm : public Form
    	// This is the form that will host the application
    	// The control created with the TrackBar class
    	TrackBar *Tracker;
    	// This label will be used to display the position of the track bar
    	Label	 *lblPosition;
    	// This method will be used to initialize the TrackBar control
    	void CreateTrackBar();
    	// This event will fire when the user scrolls the track bar 
    	void TrackerChange(Object *Sender, System::EventArgs *pArgs);
    	// Set the caption of the form
    	this->Text = "TrackBar Example";
    	// Create a label that will show the position of the TrackBar control
    	lblPosition = new Label;
    	lblPosition->Location = Point(75, 80);
    	lblPosition->Text = "10";
    	lblPosition->AutoSize = true;
    void SimpleForm::CreateTrackBar()
    	// Create a TrackBar control
    	Tracker = new TrackBar;
    	Tracker->Location = Point(20, 20);
    	Tracker->Orientation = Orientation::Vertical;
    	Tracker->Size = Drawing::Size(20, 150);
    	Tracker->Minimum = 10;
    	Tracker->Maximum = 180;
    	Tracker->TickFrequency = 10;
    	Tracker->add_ValueChanged(new EventHandler(this, TrackerChange));
    void SimpleForm::TrackerChange(Object *Sender, System::EventArgs *pArgs)
    	// When the value of the TrackBar changes as the user scrolls
    	// display the value of its position in the label
    	lblPosition->Text = Tracker->Value.ToString();
    int __stdcall WinMain()
    	SimpleForm *FM = new SimpleForm();
    	return 0;
  12. Test the application.

Home Copyright © 2002 FunctionX, Inc.