-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomInputDialog.cs
87 lines (78 loc) · 2.94 KB
/
CustomInputDialog.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
using System;
using System.Windows.Forms;
public partial class CustomInputDialog : Form {
public CustomInputDialog() {
Application.EnableVisualStyles();
InitializeComponent();
}
public string MainInstruction {
get => lblMainInstruction.Text;
set => lblMainInstruction.Text = value;
}
public string Content {
get => lblContent.Text;
set => lblContent.Text = value;
}
public string Input {
get => inputTextBox.Text;
set => inputTextBox.Text = value;
}
public int MaxLength {
get => inputTextBox.MaxLength;
set => inputTextBox.MaxLength = value;
}
public bool UsePasswordMasking {
get => inputTextBox.UseSystemPasswordChar;
set => inputTextBox.UseSystemPasswordChar = value;
}
private void SizeDialog() {
const int mainInstructionNormalHeight = 25;
const int contentNormalHeight = 21;
const int windowNormalHeight = 209;
int modifier = 0;
if (string.IsNullOrWhiteSpace(MainInstruction)) {
modifier -= 36;
lblContent.Location = new System.Drawing.Point(lblContent.Location.X, lblContent.Location.Y - 35);
} else if (lblMainInstruction.Height > mainInstructionNormalHeight) {
modifier = lblMainInstruction.Height - mainInstructionNormalHeight;
lblContent.Location = new System.Drawing.Point(lblContent.Location.X, lblContent.Location.Y + modifier);
}
if (string.IsNullOrWhiteSpace(Content))
modifier -= 30;
else if (lblContent.Height > contentNormalHeight)
modifier += lblContent.Height - contentNormalHeight;
if (modifier != 0) // limit size reduction to 51
this.Height = windowNormalHeight + Math.Max(modifier, -51);
}
private void CustomInputDialog_Load(object _, EventArgs __) {
SizeDialog();
if (Owner != null)
CenterToParent();
else
CenterToScreen();
}
private void CustomInputDialog_Shown(object _, EventArgs __) {
if (this.Visible)
inputTextBox.Focus();
}
private void btnOK_Click(object _, EventArgs __) {
DialogResult = DialogResult.OK;
}
}
public partial class WalkmanLib {
public static DialogResult InputDialog(ref string input, string mainInstruction = null, string title = null, string content = null, bool usePasswordMasking = false, int maxLength = short.MaxValue, Form owner = null) {
var inputForm = new CustomInputDialog() {
MainInstruction = mainInstruction,
Content = content,
Text = title,
Input = input,
UsePasswordMasking = usePasswordMasking,
MaxLength = maxLength,
Owner = owner,
};
var result = inputForm.ShowDialog();
if (result == DialogResult.OK)
input = inputForm.Input;
return result;
}
}