/nspector/Common/Helper/InputBox.cs

https://github.com/Orbmu2k/nvidiaProfileInspector · C# · 92 lines · 75 code · 17 blank · 0 comment · 6 complexity · 19b31bfe3f2f08b61e7c7fbbd02098ce MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Text.RegularExpressions;
  5. using System.Windows.Forms;
  6. namespace nspector.Common.Helper
  7. {
  8. internal class InputBox
  9. {
  10. internal static DialogResult Show(string title, string promptText, ref string value, List<string> invalidInputs, string mandatoryFormatRegExPattern, int maxLength)
  11. {
  12. var form = new Form();
  13. var label = new Label();
  14. var textBox = new TextBox();
  15. var buttonOk = new Button();
  16. var buttonCancel = new Button();
  17. var imageBox = new PictureBox();
  18. EventHandler textchanged = delegate (object sender, EventArgs e)
  19. {
  20. bool mandatory_success = Regex.IsMatch(textBox.Text, mandatoryFormatRegExPattern);
  21. if (textBox.Text == "" || textBox.Text.Length > maxLength || !mandatory_success)
  22. {
  23. imageBox.Image = nspector.Properties.Resources.ieframe_1_18212;
  24. buttonOk.Enabled = false;
  25. return;
  26. }
  27. foreach (string invStr in invalidInputs)
  28. {
  29. if (textBox.Text.ToUpper() == invStr.ToUpper())
  30. {
  31. imageBox.Image = Properties.Resources.ieframe_1_18212;
  32. buttonOk.Enabled = false;
  33. return;
  34. }
  35. }
  36. imageBox.Image = Properties.Resources.ieframe_1_31073_002;
  37. buttonOk.Enabled = true;
  38. };
  39. textBox.TextChanged += textchanged;
  40. form.Text = title;
  41. label.Text = promptText;
  42. textBox.Text = value;
  43. textBox.MaxLength = maxLength;
  44. imageBox.Image = Properties.Resources.ieframe_1_18212;
  45. buttonOk.Text = "OK";
  46. buttonCancel.Text = "Cancel";
  47. buttonOk.DialogResult = DialogResult.OK;
  48. buttonCancel.DialogResult = DialogResult.Cancel;
  49. buttonOk.Enabled = false;
  50. label.SetBounds(9, 20, 372, 13);
  51. textBox.SetBounds(12, 36, 352, 20);
  52. buttonOk.SetBounds(228, 72, 75, 23);
  53. buttonCancel.SetBounds(309, 72, 75, 23);
  54. imageBox.SetBounds(368, 36, 16, 16);
  55. label.AutoSize = true;
  56. imageBox.Anchor = AnchorStyles.Top | AnchorStyles.Right;
  57. textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
  58. buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
  59. buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
  60. form.ClientSize = new Size(396, 107);
  61. form.Controls.AddRange(new Control[] { label, textBox, imageBox, buttonOk, buttonCancel });
  62. form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
  63. form.FormBorderStyle = FormBorderStyle.FixedDialog;
  64. form.StartPosition = FormStartPosition.CenterParent;
  65. form.MinimizeBox = false;
  66. form.MaximizeBox = false;
  67. form.AcceptButton = buttonOk;
  68. form.CancelButton = buttonCancel;
  69. textchanged(form, new EventArgs());
  70. DialogResult dialogResult = form.ShowDialog();
  71. value = textBox.Text;
  72. return dialogResult;
  73. }
  74. }
  75. }