Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A sparse array input control

0.00/5 (No votes)
9 Feb 2005 1  
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

BackColor The BackColor property controls the color of the background of the control.
Columns The 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.
CursorColor The CursorColor property controls the color of the cursor.
Font The Font property controls the font used for the column labels.
GridColor The GridColor property controls the color of the grid.
GutterSize The 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.
Rows The 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.
SelectedColor The 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:

<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