Repository navigation
Expand file tree
/
Copy pathSymbolPickerForm.cs
More file actions
119 lines (102 loc) · 3.86 KB
/
Copy pathSymbolPickerForm.cs
File metadata and controls
119 lines (102 loc) · 3.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace PointDescriptionUpdater;
public class SymbolPickerForm : Form
{
private DataGridView dgv;
private TextBox txtNewSymbol;
private Button btnAdd;
private Button btnRemove;
public event Action<string>? SymbolSelected;
public event Action<string>? SymbolAdded;
public event Action<string>? SymbolRemoved;
public SymbolPickerForm(Form owner)
{
this.Owner = owner;
this.Text = "Master Symbols";
this.FormBorderStyle = FormBorderStyle.SizableToolWindow;
this.ShowInTaskbar = false;
// Height matches main form
int h = owner.Height;
this.Size = new Size(250, h);
this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(owner.Right + 10, owner.Top);
// Grid for symbols
dgv = new DataGridView
{
Dock = DockStyle.Fill,
AllowUserToAddRows = false,
AllowUserToDeleteRows = false,
ReadOnly = true,
RowHeadersVisible = false,
SelectionMode = DataGridViewSelectionMode.FullRowSelect,
AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill,
MultiSelect = false
};
dgv.Columns.Add("Symbol", "Required Symbols");
var toolTip = new ToolTip();
toolTip.SetToolTip(dgv, "Double click to change descriptions");
dgv.CellDoubleClick += (s, e) => {
if (e.RowIndex >= 0)
{
var val = dgv.Rows[e.RowIndex].Cells[0].Value?.ToString();
if (!string.IsNullOrEmpty(val))
SymbolSelected?.Invoke(val);
}
};
// Bottom panel for editing
var panelBottom = new Panel { Dock = DockStyle.Bottom, Height = 45 };
// Center mathematically across 250 width
// txt(150) + pad(5) + btn(25) + pad(5) + btn(25) = 210
// (250 - 210) / 2 = 20 left margin
txtNewSymbol = new TextBox { Location = new Point(20, 11), Width = 150 };
int th = txtNewSymbol.Height;
btnAdd = new Button { Text = "+", Location = new Point(175, 11), Width = 25, Height = th, TextAlign = ContentAlignment.MiddleCenter };
btnRemove = new Button { Text = "-", Location = new Point(205, 11), Width = 25, Height = th, TextAlign = ContentAlignment.MiddleCenter };
btnAdd.Click += (s, e) => {
string text = txtNewSymbol.Text.Trim();
if (!string.IsNullOrEmpty(text))
{
bool exists = false;
foreach (DataGridViewRow row in dgv.Rows)
{
if (row.Cells[0].Value?.ToString() == text)
exists = true;
}
if (!exists)
{
dgv.Rows.Add(text);
SymbolAdded?.Invoke(text);
}
txtNewSymbol.Clear();
}
};
btnRemove.Click += (s, e) => {
if (dgv.SelectedRows.Count > 0)
{
string? text = dgv.SelectedRows[0].Cells[0].Value?.ToString();
if (!string.IsNullOrEmpty(text))
{
dgv.Rows.RemoveAt(dgv.SelectedRows[0].Index);
SymbolRemoved?.Invoke(text);
}
}
};
panelBottom.Controls.Add(txtNewSymbol);
panelBottom.Controls.Add(btnAdd);
panelBottom.Controls.Add(btnRemove);
this.Controls.Add(dgv);
this.Controls.Add(panelBottom);
}
public void LoadSymbols(IEnumerable<string> symbols)
{
dgv.Rows.Clear();
foreach (var sym in symbols.OrderBy(x => x))
{
dgv.Rows.Add(sym);
}
}
}