Let us look at a simple C# Windows Forms TrackBar example.
Example 1: TrackBar
This example will comprise the following files:
ProgressBar.cs
TrackBar.cs
Step 1: Create Project
- The first step is to create a C# Project.
- Go to
FILE-->New-->Project
to create a new project.
Step 2: Write Code
Write Code as follows:
*(a). ProgressBar.cs
Create a file named ProgressBar.cs
Here is the full code
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Gammasoft {
//
// Summary:
// Represents a Windows progress bar control.
[ClassInterface(ClassInterfaceType.AutoDispatch)]
[ComVisible(true)]
[DefaultBindingProperty("Value")]
[DefaultProperty("Value")]
public class ProgressBar : System.Windows.Forms.ProgressBar {
//
// Summary:
// Gets or sets a value indicating the horizontal or vertical orientation of the progress bar.
//
// Returns:
// One of the System.Windows.Forms.Orientation values.
//
// Exceptions:
// T:System.ComponentModel.InvalidEnumArgumentException:
// The assigned value is not one of the System.Windows.Forms.Orientation values.
[DefaultValue(Orientation.Horizontal)]
[Localizable(true)]
public Orientation Orientation {
get { return orientation; }
set {
if (value != Orientation.Horizontal && value != Orientation.Vertical) throw new InvalidEnumArgumentException(string.Format("The value of argument 'value' ({0}) is invalid for Enum type 'Orientation'.", value));
if (orientation != value) {
orientation = value;
if (IsHandleCreated) RecreateHandle();
}
}
}
//
// Summary:
// Overrides System.Windows.Forms.Control.CreateParams.
//
// Returns:
// Information needed when you create a control.
protected override CreateParams CreateParams {
get {
CreateParams createParams = base.CreateParams;
if (Orientation == Orientation.Vertical) createParams.Style |= PBS_VERTICAL;
return createParams;
}
}
private Orientation orientation = Orientation.Horizontal;
private const int PBS_VERTICAL = 4;
}
}
Run
Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj
project, build and run.
Reference
Download the code using the below links:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |
*(a). TrackBar.cs
Create a file named TrackBar.cs
Here is the full code
using System;
using System.Windows.Forms;
namespace Examples {
class Form1 : Form {
// The main entry point for the application.
public static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form1());
}
public Form1() {
ClientSize = new System.Drawing.Size(300, 300);
Text = "TrackBar Example";
trackBar.Parent = this;
trackBar.Location = new System.Drawing.Point(50, 50);
trackBar.Maximum = 200;
trackBar.Orientation = Orientation.Vertical;
trackBar.Size = new System.Drawing.Size(45, 200);
trackBar.TickStyle = TickStyle.None;
trackBar.ValueChanged += delegate(object sender, EventArgs e) {
progressBar.Value = trackBar.Value;
label.Text = string.Format("{0}", trackBar.Value);
};
trackBar.Value = 100;
progressBar.Parent = this;
progressBar.Location = new System.Drawing.Point(100, 50);
progressBar.Maximum = 200;
progressBar.Orientation = Orientation.Vertical;
progressBar.Size = new System.Drawing.Size(23, 200);
progressBar.Style = ProgressBarStyle.Continuous;
label.Parent = this;
label.Location = new System.Drawing.Point(150, 50);
}
private TrackBar trackBar = new TrackBar();
private Gammasoft.ProgressBar progressBar = new Gammasoft.ProgressBar();
private Label label = new Label();
}
}
Run
Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj
project, build and run.
Reference
Download the code using the below links:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |