forked from zh3305/MapleShark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrompt.cs
81 lines (74 loc) · 3.31 KB
/
Prompt.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
/*
* PROMPT DIALOG
*
* A user input dialog inspired by Javascript window.Prompt().
* ÊäÈë¿ò
*
*/
using Newtonsoft.Json;
using System.Windows.Forms;
using System.Xml;
namespace MapleShark.Tools
{
/// <summary>
/// ÊäÈë¿ò
/// </summary>
public static class window
{
public static string ConvetXmlToJson(string XMLText)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(XMLText);
return JsonConvert.SerializeXmlNode(doc);
}
/// <summary>
/// Displays a prompt dialog and returns a value.
/// </summary>
/// <returns>Returns user input value. If user enters nothing empty string is returned.</returns>
public static string prompt()
{
return prompt("Prompt", "Input value: ", "");
}
/// <summary>
/// Displays a prompt dialog and returns a value.
/// </summary>
/// <param name="title">Text to be shown in the windowbar</param>
/// <param name="message">Message to get user to input a value</param>
/// <returns>Returns user input value. If user enters nothing empty string is returned.</returns>
public static string prompt(string title, string message)
{
return prompt(title, message, "");
}
/// <summary>
/// Displays a prompt dialog and returns a value.
/// </summary>
/// <param name="title">Text to be shown in the windowbar</param>
/// <param name="message">Message to get user to input a value</param>
/// <param name="defaultValue">The default value to assist user input</param>
/// <returns>Returns user input value. If user enters nothing empty string is returned.</returns>
public static string prompt(string title, string message, string defaultValue)
{
//Create controls and set default values
Form dialog = new Form() { Width = 300, Height = 129, FormBorderStyle = FormBorderStyle.FixedDialog, Text = title, StartPosition = FormStartPosition.CenterScreen };
Label label1 = new Label() { Left = 10, Top = 10 };
TextBox textBox1 = new TextBox() { Left = 10, Top = 30, Width = 260, Height = 20 };
Button button1 = new Button() { Text = "Ok", Left = 116, Top = 59, Width = 75, Height = 23 };
Button button2 = new Button() { Text = "Cancel", Left = 197, Top = 59, Width = 75, Height = 23 };
//Add all the creations to dialog
dialog.Controls.Add(textBox1);
dialog.Controls.Add(label1);
dialog.Controls.Add(button1);
dialog.Controls.Add(button2);
//Set behaviours of new controls
button1.Click += (sender, e) => { dialog.Close(); };
button1.DialogResult = DialogResult.OK;
button2.Click += (sender, e) => { dialog.Close(); };
dialog.AcceptButton = button1; //press enter to accept
label1.Text = message; //prompt the user to type something
label1.AutoSize = true; //incase text is longer than label, text don't get chopped off
textBox1.Text = defaultValue;
//If ok is pressed, return the user input text, else return empty string
return dialog.ShowDialog() == DialogResult.OK ? textBox1.Text : "";
}
}
}