-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListForm.cs
158 lines (143 loc) · 5.02 KB
/
ListForm.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
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace LVLTool
{
public partial class ListForm : Form
{
public ListForm()
{
InitializeComponent();
}
protected override void OnHandleCreated(EventArgs e)
{
//https://stackoverflow.com/questions/57124243/winforms-dark-title-bar-on-windows-10 (thank you:)
if (DwmSetWindowAttribute(this.Handle, 19, new[] { 1 }, 4) != 0)
DwmSetWindowAttribute(this.Handle, 20, new[] { 1 }, 4);
base.OnHandleCreated(e);
}
[DllImport("DwmApi")] //System.Runtime.InteropServices
private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, int[] attrValue, int attrSize);
public string PromptString
{
get { return labSelectPrompt.Text; }
set { labSelectPrompt.Text = value; }
}
public string CurrentOption
{
get { return listOptions.SelectedItem.ToString(); }
set
{
int index = listOptions.Items.IndexOf(value);
if (index > -1)
{
listOptions.SelectedIndex = index;
}
else
{
Console.WriteLine("Warning: ListForm.SetCurrentOption: value not found: "+ value);
}
}
}
public List<string> Options
{
get
{
List<string> retVal = new List<string>(listOptions.Items.Count);
foreach (object item in listOptions.Items)
{
retVal.Add(item.ToString());
}
return retVal;
}
set
{
listOptions.Items.Clear();
listOptions.Items.AddRange(value.ToArray());
}
}
internal void SetStyle(Form prevForm)
{
this.BackColor = prevForm.BackColor;
this.ForeColor = prevForm.ForeColor;
TextBox tb = null;
Button b = null;
foreach (Control tmp in prevForm.Controls)
{
if (tb == null)
tb = tmp as TextBox;
if (b == null)
b = tmp as Button;
if (tb != null && b != null)
break;
}
if (tb != null)
{
this.listOptions.BackColor = tb.BackColor;
this.listOptions.ForeColor = tb.ForeColor;
}
if (b != null)
{
btnCancel.BackColor = btnOk.BackColor = b.BackColor;
btnCancel.ForeColor = btnOk.ForeColor = b.ForeColor;
}
}
private void listOptions_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.DialogResult = DialogResult.OK;
}
}
private void listOptions_DoubleClick(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
/// <summary>
/// Prompts the user for a selection
/// </summary>
/// <param name="title">The title of the dialog</param>
/// <param name="caption">The dialog prompt</param>
/// <param name="options">the list of options to choose from</param>
/// <returns>Selected option or null if the user canceled</returns>
public static string PromptUserForSelection(string title, string caption, List<string> options, string defaultOption)
{
return PromptUserForSelection(title, caption, options,defaultOption, null);
}
/// <summary>
/// Prompts the user for a selection
/// </summary>
/// <param name="title">The title of the dialog</param>
/// <param name="caption">The dialog prompt</param>
/// <param name="options">the list of options to choose from</param>
/// <param name="f">the font to use for the form</param>
/// <returns>Selected option or null if the user canceled</returns>
public static string PromptUserForSelection(string title, string caption, List<string> options, string defaultOption, Form f)
{
string retVal = null;
ListForm form = new ListForm();
if(title != null)
form.Text = title;
if( caption != null)
form.PromptString = caption;
form.Options = options;
if( defaultOption != null)
form.CurrentOption = defaultOption;
if (f != null)
{
form.SetStyle(f);
}
if (form.ShowDialog() == DialogResult.OK)
{
retVal = form.CurrentOption;
}
return retVal;
}
}
}