summaryrefslogtreecommitdiffstats
path: root/ComicRackWebViewer/InputBox.cs
blob: e01123a42a534da6da405a459d795a8f630856d9 (plain)
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
/*
 * Created by SharpDevelop.
 * User: jeroen
 * Date: 03/14/2013
 * Time: 19:34
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

  
using System;
using System.Drawing;
using System.Windows.Forms;

namespace ComicRackWebViewer {

  // source: http://www.csharp-examples.net/inputbox-class/
  
  public class InputBox
  {
    public static DialogResult Show(string title, string promptText, ref string value)
    {
      return Show(title, promptText, ref value, null);
    }
  
    public static DialogResult Show(string title, string promptText, ref string value, InputBoxValidation validation)
    {
      Form form = new Form();
      Label label = new Label();
      TextBox textBox = new TextBox();
      Button buttonOk = new Button();
      Button buttonCancel = new Button();
  
      form.Text = title;
      label.Text = promptText;
      textBox.Text = value;
  
      buttonOk.Text = "OK";
      buttonCancel.Text = "Cancel";
      buttonOk.DialogResult = DialogResult.OK;
      buttonCancel.DialogResult = DialogResult.Cancel;
  
      label.SetBounds(9, 20, 372, 13);
      textBox.SetBounds(12, 36, 372, 20);
      buttonOk.SetBounds(228, 72, 75, 23);
      buttonCancel.SetBounds(309, 72, 75, 23);
  
      label.AutoSize = true;
      textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
      buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
      buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
  
      form.ClientSize = new Size(396, 107);
      form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
      form.ClientSize = new Size(Math.Max(300,label.Right+10), form.ClientSize.Height);
      form.FormBorderStyle = FormBorderStyle.FixedDialog;
      form.StartPosition = FormStartPosition.CenterScreen;
      form.MinimizeBox = false;
      form.MaximizeBox = false;
      form.AcceptButton = buttonOk;
      form.CancelButton = buttonCancel;
      if (validation != null) {
        form.FormClosing += delegate(object sender, FormClosingEventArgs e) {
          if (form.DialogResult == DialogResult.OK) {
            string errorText = validation(textBox.Text);
            if (e.Cancel = (errorText != "")) {
              MessageBox.Show(form, errorText, "Validation Error",
                              MessageBoxButtons.OK, MessageBoxIcon.Error);
              textBox.Focus();
            }
          }
        };
      }
      DialogResult dialogResult = form.ShowDialog();
      value = textBox.Text;
      return dialogResult;
    }
  }
  public delegate string InputBoxValidation(string errorMessage);

}