TextEdit.xaml.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System.Windows;
  2. namespace InABox.Wpf.Editors
  3. {
  4. public partial class TextEdit : ThemableWindow
  5. {
  6. public TextEdit()
  7. {
  8. InitializeComponent();
  9. }
  10. public TextEdit(string caption, string text = "")
  11. {
  12. InitializeComponent();
  13. Title = caption;
  14. TextEditor.Text = text;
  15. }
  16. public TextEdit(string caption, string prompt, string text = "")
  17. {
  18. InitializeComponent();
  19. Title = caption;
  20. Prompt.Content = prompt;
  21. TextEditor.Text = text;
  22. }
  23. public string Text
  24. {
  25. get => TextEditor.Text;
  26. set => TextEditor.Text = value;
  27. }
  28. private void OK_Click(object sender, RoutedEventArgs e)
  29. {
  30. DialogResult = true;
  31. Close();
  32. }
  33. private void Cancel_Click(object sender, RoutedEventArgs e)
  34. {
  35. DialogResult = false;
  36. Close();
  37. }
  38. public static bool Execute(string caption, ref string text)
  39. {
  40. var editor = new TextEdit(caption, text);
  41. if (editor.ShowDialog() == true)
  42. {
  43. text = editor.Text;
  44. return true;
  45. }
  46. return false;
  47. }
  48. public static bool Execute(string caption, string prompt, ref string text)
  49. {
  50. var editor = new TextEdit(caption, prompt, text);
  51. if (editor.ShowDialog() == true)
  52. {
  53. text = editor.Text;
  54. return true;
  55. }
  56. return false;
  57. }
  58. private void ThemableWindow_Loaded(object sender, RoutedEventArgs e)
  59. {
  60. TextEditor.Focus();
  61. TextEditor.SelectAll();
  62. }
  63. private void TextEditor_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
  64. {
  65. switch (e.Key)
  66. {
  67. case System.Windows.Input.Key.Enter:
  68. DialogResult = true;
  69. Close();
  70. break;
  71. case System.Windows.Input.Key.Escape:
  72. DialogResult = false;
  73. Close();
  74. break;
  75. }
  76. }
  77. }
  78. }