/VirtualDesktop/Shell/CommonFileDialogs/CommonFileDialogTextBox.cs

# · C# · 101 lines · 49 code · 18 blank · 34 comment · 6 complexity · 9b51697b526dd66d0ea7922ebefdd438 MD5 · raw file

  1. //Copyright (c) Microsoft Corporation. All rights reserved.
  2. using System;
  3. using System.Diagnostics;
  4. namespace Microsoft.WindowsAPICodePack.Dialogs.Controls
  5. {
  6. /// <summary>
  7. /// Defines the text box controls in the Common File Dialog.
  8. /// </summary>
  9. public class CommonFileDialogTextBox : CommonFileDialogControl
  10. {
  11. /// <summary>
  12. /// Creates a new instance of this class.
  13. /// </summary>
  14. public CommonFileDialogTextBox() : base(string.Empty) { }
  15. /// <summary>
  16. /// Creates a new instance of this class with the specified text.
  17. /// </summary>
  18. /// <param name="text">The text to display for this control.</param>
  19. public CommonFileDialogTextBox(string text) : base(text) { }
  20. /// <summary>
  21. /// Creates a new instance of this class with the specified name and text.
  22. /// </summary>
  23. /// <param name="name">The name of this control.</param>
  24. /// <param name="text">The text to display for this control.</param>
  25. public CommonFileDialogTextBox(string name, string text) : base(name, text) { }
  26. internal bool Closed { get; set; }
  27. /// <summary>
  28. /// Gets or sets a value for the text string contained in the CommonFileDialogTextBox.
  29. /// </summary>
  30. public override string Text
  31. {
  32. get
  33. {
  34. if (!Closed)
  35. {
  36. SyncValue();
  37. }
  38. return base.Text;
  39. }
  40. set
  41. {
  42. if (customizedDialog != null)
  43. {
  44. customizedDialog.SetEditBoxText(this.Id, value);
  45. }
  46. base.Text = value;
  47. }
  48. }
  49. /// <summary>
  50. /// Holds an instance of the customized (/native) dialog and should
  51. /// be null until after the Attach() call is made.
  52. /// </summary>
  53. private IFileDialogCustomize customizedDialog;
  54. /// <summary>
  55. /// Attach the TextBox control to the dialog object
  56. /// </summary>
  57. /// <param name="dialog">Target dialog</param>
  58. internal override void Attach(IFileDialogCustomize dialog)
  59. {
  60. Debug.Assert(dialog != null, "CommonFileDialogTextBox.Attach: dialog parameter can not be null");
  61. // Add a text entry control
  62. dialog.AddEditBox(this.Id, this.Text);
  63. // Set to local instance in order to gate access to same.
  64. customizedDialog = dialog;
  65. // Sync unmanaged properties with managed properties
  66. SyncUnmanagedProperties();
  67. Closed = false;
  68. }
  69. internal void SyncValue()
  70. {
  71. // Make sure that the local native dialog instance is NOT
  72. // null. If it's null, just return the "textValue" var,
  73. // otherwise, use the native call to get the text value,
  74. // setting the textValue member variable then return it.
  75. if (customizedDialog != null)
  76. {
  77. string textValue;
  78. customizedDialog.GetEditBoxText(this.Id, out textValue);
  79. base.Text = textValue;
  80. }
  81. }
  82. }
  83. }