-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
84 lines (75 loc) · 2.09 KB
/
Form1.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace CascadingComboSpike
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
_numberClass.SelectedIndex = 0;
new ComboCascader(_numberClass, _number, FillNumberFromNumberClass);
new ComboCascader(_number, _letter, FillLetterFromNumber);
new ComboCascader(_letter, _sector, FillSector);
}
#region Methods that load data into comboboxes
private void FillNumberFromNumberClass()
{
_number.Items.Clear();
switch (_numberClass.SelectedIndex)
{
case 0: // <select one>
break;
case 1: // Even numbers
_number.Items.AddRange(Enumerable.Range(1, 9).Where(n => n % 2 == 0).Cast<object>().ToArray());
break;
case 2: //Odd numbers
_number.Items.AddRange(Enumerable.Range(1, 9).Where(n => n % 2 == 1).Cast<object>().ToArray());
break;
case 3: //Multiples of 3
_number.Items.AddRange(Enumerable.Range(1, 9).Where(n => n % 3 == 0).Cast<object>().ToArray());
break;
case 4: //Prime numbers
_number.Items.AddRange(new[] { 2, 3, 5, 7 }.Cast<object>().ToArray());
break;
default:
throw new Exception("Out of range, I guess.");
}
}
private void FillLetterFromNumber()
{
_letter.Items.Clear();
if (_number.SelectedItem == null)
{
return;
}
int selectedNumber = (int)_number.SelectedItem;
int start = selectedNumber - 1 + (int)'A';
int end = start + 4;
for (int i = start; i <= end; i++)
{
_letter.Items.Add(((char)i).ToString());
}
}
private void FillSector()
{
_sector.Items.Clear();
if (_number.SelectedItem == null || _letter.SelectedItem == null)
{
return;
}
int number = (int)_number.SelectedItem;
string letter = (string)_letter.SelectedItem;
foreach (char part in "abcd")
{
_sector.Items.Add(string.Format("{0}{1}-{2}", number, letter, part));
}
}
#endregion
}
}