SamplesInput.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. Licensed Materials - Property of IBM
  3. IBM Cognos Products: DOCS
  4. (C) Copyright IBM Corp. 2005
  5. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
  6. IBM Corp.
  7. */
  8. using System;
  9. using System.Drawing;
  10. using System.Collections;
  11. using System.ComponentModel;
  12. using System.Windows.Forms;
  13. namespace SamplesCommon
  14. {
  15. /// <summary>
  16. /// A simple text input dialog.
  17. /// </summary>
  18. public class SamplesInput : System.Windows.Forms.Form {
  19. private System.Windows.Forms.Label descriptionText;
  20. private System.Windows.Forms.TextBox dataText;
  21. private System.Windows.Forms.Button okButton;
  22. private System.Windows.Forms.Button cancelButton;
  23. /// <summary>
  24. /// Required designer variable.
  25. /// </summary>
  26. private System.ComponentModel.Container components = null;
  27. public bool isOKed = false;
  28. public bool isCanceled = false;
  29. public SamplesInput() {
  30. //
  31. // Required for Windows Form Designer support
  32. //
  33. InitializeComponent();
  34. }
  35. /// <summary>
  36. /// Clean up any resources being used.
  37. /// </summary>
  38. protected override void Dispose( bool disposing ) {
  39. if( disposing ) {
  40. if(components != null) {
  41. components.Dispose();
  42. }
  43. }
  44. base.Dispose( disposing );
  45. }
  46. #region Windows Form Designer generated code
  47. /// <summary>
  48. /// Required method for Designer support - do not modify
  49. /// the contents of this method with the code editor.
  50. /// </summary>
  51. private void InitializeComponent() {
  52. System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(SamplesInput));
  53. this.descriptionText = new System.Windows.Forms.Label();
  54. this.dataText = new System.Windows.Forms.TextBox();
  55. this.okButton = new System.Windows.Forms.Button();
  56. this.cancelButton = new System.Windows.Forms.Button();
  57. this.SuspendLayout();
  58. //
  59. // descriptionText
  60. //
  61. this.descriptionText.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
  62. | System.Windows.Forms.AnchorStyles.Right)));
  63. this.descriptionText.Location = new System.Drawing.Point(16, 16);
  64. this.descriptionText.Name = "descriptionText";
  65. this.descriptionText.Size = new System.Drawing.Size(344, 23);
  66. this.descriptionText.TabIndex = 0;
  67. this.descriptionText.Text = "(input description)";
  68. this.descriptionText.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
  69. //
  70. // dataText
  71. //
  72. this.dataText.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
  73. | System.Windows.Forms.AnchorStyles.Right)));
  74. this.dataText.Location = new System.Drawing.Point(16, 48);
  75. this.dataText.Name = "dataText";
  76. this.dataText.Size = new System.Drawing.Size(344, 20);
  77. this.dataText.TabIndex = 1;
  78. this.dataText.Text = "";
  79. this.dataText.KeyDown += new KeyEventHandler(this.dataText_OnKeyPress);
  80. //
  81. // okButton
  82. //
  83. this.okButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  84. this.okButton.DialogResult = System.Windows.Forms.DialogResult.OK;
  85. this.okButton.Location = new System.Drawing.Point(200, 80);
  86. this.okButton.Name = "okButton";
  87. this.okButton.TabIndex = 2;
  88. this.okButton.Text = "OK";
  89. this.okButton.Click += new System.EventHandler(this.okButton_Click);
  90. //
  91. // cancelButton
  92. //
  93. this.cancelButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  94. this.cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  95. this.cancelButton.Location = new System.Drawing.Point(288, 80);
  96. this.cancelButton.Name = "cancelButton";
  97. this.cancelButton.TabIndex = 3;
  98. this.cancelButton.Text = "Cancel";
  99. //
  100. // SamplesInput
  101. //
  102. this.AcceptButton = this.okButton;
  103. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  104. this.CancelButton = this.cancelButton;
  105. this.ClientSize = new System.Drawing.Size(376, 110);
  106. this.ControlBox = false;
  107. this.Controls.Add(this.cancelButton);
  108. this.Controls.Add(this.okButton);
  109. this.Controls.Add(this.dataText);
  110. this.Controls.Add(this.descriptionText);
  111. this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  112. this.MaximizeBox = false;
  113. this.MinimizeBox = false;
  114. this.Name = "SamplesInput";
  115. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
  116. this.Text = "(Input Title)";
  117. this.ResumeLayout(false);
  118. }
  119. #endregion
  120. /// <summary>
  121. /// Get input from the user.
  122. ///
  123. /// Shows the window as a modal dialog, and returns the string input.
  124. /// Throws SamplesInputException if the user clicks Cancel instead.
  125. /// </summary>
  126. /// <param name="description">Descriptive text for the input dialog.</param>
  127. /// <param name="default_value">Default value (use "" if none).</param>
  128. /// <returns>The text input by the user.</returns>
  129. public string getInput( string title, string description, string default_value ) {
  130. if ( (title == null) || (0 == title.CompareTo("")) )
  131. {
  132. this.Text = "";
  133. }
  134. else
  135. {
  136. this.Text = title;
  137. }
  138. if ( (description == null) || (0 == description.CompareTo("")) )
  139. {
  140. descriptionText.Text = "";
  141. }
  142. else
  143. {
  144. descriptionText.Text = description;
  145. }
  146. if ( (default_value == null) || (0 == default_value.CompareTo("")) )
  147. {
  148. dataText.Text = "";
  149. }
  150. else
  151. {
  152. dataText.Text = default_value;
  153. }
  154. DialogResult result = ShowDialog();
  155. if( result == DialogResult.Cancel )
  156. {
  157. throw new SamplesInputException( "INPUT_CANCELLED_BY_USER" );
  158. }
  159. return dataText.Text;
  160. }
  161. private void okButton_Click(object sender, System.EventArgs e)
  162. {
  163. isOKed = true;
  164. if ( (dataText.Text == null) || (0 == dataText.Text.CompareTo("")) )
  165. {
  166. MessageBox.Show("Please enter a name for the report to be created.");
  167. }
  168. else
  169. {
  170. this.Close();
  171. }
  172. }
  173. private void dataText_OnKeyPress(object sender, KeyEventArgs e)
  174. {
  175. if (e.KeyCode == Keys.Enter)
  176. {
  177. okButton_Click(sender, e);
  178. }
  179. }
  180. }
  181. /// <summary>
  182. /// Exception thrown by the simple text input dialog when you click Cancel.
  183. /// </summary>
  184. public class SamplesInputException : System.Exception
  185. {
  186. public SamplesInputException( string message )
  187. : base( message )
  188. {
  189. }
  190. }
  191. }