Click here to Skip to main content
16,020,417 members
Articles / Programming Languages / C#
Article

A sparse array input control

Rate me:
Please Sign up or sign in to vote.
1.40/5 (2 votes)
9 Feb 20053 min read 31.8K   383   14   2
An article on a simple control to enter sparse values or ranges of values in an array.

Sample Image

Introduction

You may have run into the need of entering sparse values for a two dimensional array. The most common case is that of entering time ranges for each day of the week when a person is available for work.

Searching through CP, I found some interesting work by ClearlyDotNet (see "A Time-Of-Day Picker control"), which provides such functionality, except that by returning TimeSpans, would not allow for a generic control that allows for the entry of non-time values.

What I needed was a control that generated generic output that could be saved to a file, and restored as needed, as well as the ability to dynamically change the row and column definitions while keeping any selected cell that applies in the new row/column set.

Having sometime on my hands, I decided to write my own. This article is the result of my labors, and it is placed here at CP as part of my repayment for some good ideas that I have gotten from other submissions. You can use the source as you wish, just remember that it is public domain, so keep it that way!

Using the code (programmer's view)

You can adapt the control to meet your requirements by changing the following properties:

Property

Definition

BackColorThe BackColor property controls the color of the background of the control.
ColumnsThe Columns property controls the data point names, and are displayed across the page. The property accepts a string array. The values are used in the Text property.
CursorColorThe CursorColor property controls the color of the cursor.
FontThe Font property controls the font used for the column labels.
GridColorThe GridColor property controls the color of the grid.
GutterSizeThe GutterSize property controls the gutter size inside each cell of the grid. The value is treated as a percentage of the cell height/width. Note that the cursor uses double the gutter size to ease the telling of where the cursor is at.
RowsThe Rows property controls the data point names, and are displayed down the page. The property accepts a string array. The values are used in the Text property.
SelectedColorThe SelectedColor property controls the color of the selected cells in the grid.
Text

The Text property is an XML string that has one entry per selected cell. The syntax is as follows:

XML
<SparseArray>
<Entry>
  <Row>textfromRowsproperty</Row>
  <Col>textfromColsproperty</Col>
</Entry>
<Entry>
  <Row>textfromRowsproperty</Row>
  <Col>textfromColsproperty</Col>
</Entry>
<Entry>
  <Row>textfromRowsproperty</Row>
  <Col>textfromColsproperty</Col>
</Entry>
<Entry>
  <Row>textfromRowsproperty</Row>
  <Col>textfromColsproperty</Col>
</Entry>
</SparseArray>

If you set this property to an invalid value, no cells will be selected.

All of the properties are available at design time, and changes are shown immediately.

The control fires one event TextChanged whenever the user changes the selected cells. This event does not fire if the Text property is set programmatically.

Using the code (end user's view)

The end user selects cells by clicking on them. He/she can also select ranges by clicking on the starting cell and then dragging the cursor to the end cell. Note that selecting regions causes the reversal of cell state for those cells already selected.

Points of Interest

The code was developed under Visual Studio 2003, and Framework 1.1. My intention is to share the code so all can use and maybe learn a bit from it. Do not assume that the code is free from bugs, as it was done in a few hours, but feel free to make any changes and/or corrections that you feel appropriate.

History

Released to the world December 20, 2004.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer eCandidus
United States United States
A random human being, loose on a planet full of other organic beings. Got sidetracked into computer programming as the arpanet, which later became the internet was being tested. If you remember the IBM 1620 Model 1 (whole 4K of core baby!), then you can place me in time, as it was the computer I first played with.

Begun with assembly, moved on to Fortran (no number), and in alpha order: Algol, APLer (for life), Basic, C, C++(when it was a preprocess!), C#, COBOL, Java, Lisp, Pascal, PL/1, POP (Ada-like, not e-mail), Python and RPG. (Sorry, HTML is not a programming language, neither is SQL .. And I have done them both).

Begun with single user (1620), on to centralized (360), shared (VAX), standalone (PC), networked (LAN/Internet), client/server and on to distributed (SQL/IIS/C# Web Services/Ajax/Javascript/Thick Client)

My goal in life is to drive the Oscar Mayer Wienermobile.

Comments and Discussions

 
GeneralI think you uploaded the wrong thing... Pin
ACorbs7-Feb-05 10:42
ACorbs7-Feb-05 10:42 
GeneralRe: I think you uploaded the wrong thing... Pin
je_gonzalez10-Feb-05 18:49
je_gonzalez10-Feb-05 18:49 
My apologies... The proper zip file is now available.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.