-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSevenSegmentArray.cs
More file actions
186 lines (165 loc) · 6.86 KB
/
Copy pathSevenSegmentArray.cs
File metadata and controls
186 lines (165 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Windows.Forms;
using System.Drawing;
/*
* Seven-segment LED array control for .NET
* (uses the original seven-segment LED control)
*
* Copyright 2009 Dmitry Brant. All Rights Reserved.
* me@dmitrybrant.com
* http://dmitrybrant.com
*
* This component is free for personal use.
* If you would like to use it in a commercial application, please
* e-mail me at the address above.
* This software comes as-is, with no warranty.
*
* Features:
* - Arbitrary number of array elements
* - All the customizable settings of the original 7-seg control
* - Accepts a string as a value, and distributes the characters
* among the array elements, automatically handling decimal points.
*
*/
namespace DmitryBrant.CustomControls
{
public class SevenSegmentArray : UserControl
{
public SevenSegmentArray()
{
this.SuspendLayout();
this.Name = "SevenSegmentArray";
this.Size = new System.Drawing.Size(100, 25);
this.Resize += new System.EventHandler(this.SevenSegmentArray_Resize);
this.ResumeLayout(false);
this.TabStop = false;
elementPadding = new Padding(4, 4, 4, 4);
RecreateSegments(4);
}
/// <summary>
/// Array of segment controls that are currently children of this control.
/// </summary>
private SevenSegment[] segments = null;
/// <summary>
/// Change the number of elements in our LED array. This destroys
/// the previous elements, and creates new ones in their place, applying
/// all the current options to the new ones.
/// </summary>
/// <param name="count">Number of elements to create.</param>
private void RecreateSegments(int count)
{
if (segments != null)
for (int i = 0; i < segments.Length; i++) { segments[i].Parent = null; segments[i].Dispose(); }
if (count <= 0) return;
segments = new SevenSegment[count];
for (int i = 0; i < count; i++)
{
segments[i] = new SevenSegment();
segments[i].Parent = this;
segments[i].Top = 0;
segments[i].Height = this.Height;
segments[i].Anchor = AnchorStyles.Top | AnchorStyles.Bottom;
segments[i].Visible = true;
}
ResizeSegments();
UpdateSegments();
this.Value = theValue;
}
/// <summary>
/// Align the elements of the array to fit neatly within the
/// width of the parent control.
/// </summary>
private void ResizeSegments()
{
int segWidth = this.Width / segments.Length;
for (int i = 0; i < segments.Length; i++)
{
segments[i].Left = this.Width * (segments.Length - 1 - i) / segments.Length;
segments[i].Width = segWidth;
}
}
/// <summary>
/// Update the properties of each element with the properties
/// we have stored.
/// </summary>
private void UpdateSegments()
{
for (int i = 0; i < segments.Length; i++)
{
segments[i].ColorBackground = colorBackground;
segments[i].ColorDark = colorDark;
segments[i].ColorLight = colorLight;
segments[i].ElementWidth = elementWidth;
segments[i].ItalicFactor = italicFactor;
segments[i].DecimalShow = showDot;
segments[i].Padding = elementPadding;
}
}
private void SevenSegmentArray_Resize(object sender, EventArgs e) { ResizeSegments(); }
protected override void OnPaintBackground(PaintEventArgs e) { e.Graphics.Clear(colorBackground); }
private int elementWidth = 10;
private float italicFactor = 0.0F;
private Color colorBackground = Color.DarkGray;
private Color colorDark = Color.DimGray;
private Color colorLight = Color.Red;
private bool showDot = true;
private Padding elementPadding;
/// <summary>
/// Background color of the LED array.
/// </summary>
public Color ColorBackground { get { return colorBackground; } set { colorBackground = value; UpdateSegments(); } }
/// <summary>
/// Color of inactive LED segments.
/// </summary>
public Color ColorDark { get { return colorDark; } set { colorDark = value; UpdateSegments(); } }
/// <summary>
/// Color of active LED segments.
/// </summary>
public Color ColorLight { get { return colorLight; } set { colorLight = value; UpdateSegments(); } }
/// <summary>
/// Width of LED segments.
/// </summary>
public int ElementWidth { get { return elementWidth; } set { elementWidth = value; UpdateSegments(); } }
/// <summary>
/// Shear coefficient for italicizing the displays. Try a value like -0.1.
/// </summary>
public float ItalicFactor { get { return italicFactor; } set { italicFactor = value; UpdateSegments(); } }
/// <summary>
/// Specifies if the decimal point LED is displayed.
/// </summary>
public bool DecimalShow { get { return showDot; } set { showDot = value; UpdateSegments(); } }
/// <summary>
/// Number of seven-segment elements in this array.
/// </summary>
public int ArrayCount { get { return segments.Length; } set { if ((value > 0) && (value <= 100)) RecreateSegments(value); } }
/// <summary>
/// Padding that applies to each seven-segment element in the array.
/// Tweak these numbers to get the perfect appearance for the array of your size.
/// </summary>
public Padding ElementPadding { get { return elementPadding; } set { elementPadding = value; UpdateSegments(); } }
private string theValue = null;
/// <summary>
/// The value to be displayed on the LED array. This can contain numbers,
/// certain letters, and decimal points.
/// </summary>
public string Value
{
get { return theValue; }
set
{
theValue = value;
for (int i = 0; i < segments.Length; i++) { segments[i].CustomPattern = 0; segments[i].DecimalOn = false; }
if (theValue != null)
{
int segmentIndex = 0;
for (int i = theValue.Length - 1; i >= 0; i--)
{
if (segmentIndex >= segments.Length) break;
if (theValue[i] == '.') segments[segmentIndex].DecimalOn = true;
else segments[segmentIndex++].Value = theValue[i].ToString();
}
}
}
}
}
}