Click here to Skip to main content
15,887,596 members
Articles / Programming Languages / C# 4.0

RangeBar Custom Control

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
3 Jul 2011CPOL2 min read 18.7K   320   6  
Custom control that can show ranges in a horizontal bar

Rangebar_CustomControl/RangeBar.jpg

Introduction

The code is an example of how to build a custom control. My aim is to make a control that could represent a range or a set of ranges in a graphical way. I chose not to implement something very difficult and to abstract as much information as possible from the final user. As of this version the control is fully operational, all properties are in designer window and a overridden collection editor is available for the ranges collection. Also any changes in properties including the ones in Ranges Collection Editor are immediately applied in design mode.

Snapshot of collection editor is shown below:

Custom Collection Editor

Background

Sometimes, I want to show a range of values or a time slice in another time slice in a graphical way as I have seen in other projects, but couldn't find an example of how could this be done, so I decided to make my own.

Using the Code

To use the code, just download the attached file. It already contains a test project so you can see the custom control in action.

When you run the project in Form1, you can add a range in range bar by typing a range in two textboxes. The first box is the starting x value from 0 to 100 and the second one is the ending x value, again from 0 to 100. Keep in mind that the first x value must be less than the end value. After entering the values, press the Add Range to add it and Remove Range to remove it. You could add ranges and remove them by simply providing the starting and ending values.

In design mode, you can change the gradient of the background in aspect of the colors involved as the angle they draw. The same applies for ranges too.

As of code perspective, only two functions are important and these are:

C#
RangeBar.AddRange(double RangeStart, double RangeEnd)

and:

C#
RangeBar.RemoveRange(double RangeStart, double RangeEnd)

in which you add and remove a range.

Also an event is in interest. The one that fires when you click on a range in the rangebar.

C#
RangeBar_Paint(object sender, PaintEventArgs e)

PaintEventsArgs contains the x value in the RangeBar range and the range start and end values.

Points of Interest

Some important points of interest that I learned are:

  • From 1st Version
    • How to work with the Graphics from PaintEventsArgs
    • Creating Events and passing custom EventArgs
    • Hiding unwanted inherited information like properties
  • From 2nd Version
    • Collections properties need special treatment with DesignerSerializationVisibility which should be set to Content
      C#
      DesignerSerializationVisibility(DesignerSerializationVisibility.Content)  
    • Implementing a custom Collection Editor
    • Learned a lot about design time enhancements

Also, some mathematics from the past came up concerning 2-dimensional graphics (although in this example, I only work in one dimension).

History

  • First revision
    • Just do the job
  • Second revision
    • Start and end properties of the control are now available
    • Internally Ranges are stored in a collection
    • A new custom Collection Editor
    • RangeBar summaries are complete

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Product Manager Apptel ltd
Greece Greece
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --