SamplesListInput.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. /// Summary description for SamplesListInput.
  17. /// </summary>
  18. public class SamplesListInput : System.Windows.Forms.Form
  19. {
  20. private System.Windows.Forms.Button buttonOK;
  21. private System.Windows.Forms.Button buttonCancel;
  22. private System.Windows.Forms.Label descriptionText;
  23. private System.Windows.Forms.ComboBox dataList;
  24. /// <summary>
  25. /// Required designer variable.
  26. /// </summary>
  27. private System.ComponentModel.Container components = null;
  28. public SamplesListInput()
  29. {
  30. //
  31. // Required for Windows Form Designer support
  32. //
  33. InitializeComponent();
  34. //
  35. // TODO: Add any constructor code after InitializeComponent call
  36. //
  37. }
  38. /// <summary>
  39. /// Clean up any resources being used.
  40. /// </summary>
  41. protected override void Dispose( bool disposing )
  42. {
  43. if( disposing )
  44. {
  45. if(components != null)
  46. {
  47. components.Dispose();
  48. }
  49. }
  50. base.Dispose( disposing );
  51. }
  52. #region Windows Form Designer generated code
  53. /// <summary>
  54. /// Required method for Designer support - do not modify
  55. /// the contents of this method with the code editor.
  56. /// </summary>
  57. private void InitializeComponent()
  58. {
  59. this.descriptionText = new System.Windows.Forms.Label();
  60. this.dataList = new System.Windows.Forms.ComboBox();
  61. this.buttonOK = new System.Windows.Forms.Button();
  62. this.buttonCancel = new System.Windows.Forms.Button();
  63. this.SuspendLayout();
  64. //
  65. // descriptionText
  66. //
  67. this.descriptionText.Location = new System.Drawing.Point(16, 24);
  68. this.descriptionText.Name = "descriptionText";
  69. this.descriptionText.Size = new System.Drawing.Size(352, 23);
  70. this.descriptionText.TabIndex = 0;
  71. this.descriptionText.Text = "(Label Input)";
  72. //
  73. // dataList
  74. //
  75. this.dataList.BackColor = System.Drawing.SystemColors.Window;
  76. this.dataList.Location = new System.Drawing.Point(16, 48);
  77. this.dataList.Name = "dataList";
  78. this.dataList.Size = new System.Drawing.Size(344, 21);
  79. this.dataList.TabIndex = 1;
  80. this.dataList.KeyDown += new KeyEventHandler(this.dataList_OnKeyPress);
  81. //
  82. // buttonOK
  83. //
  84. this.buttonOK.DialogResult = System.Windows.Forms.DialogResult.OK;
  85. this.buttonOK.Location = new System.Drawing.Point(200, 80);
  86. this.buttonOK.Name = "buttonOK";
  87. this.buttonOK.TabIndex = 2;
  88. this.buttonOK.Text = "OK";
  89. this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);
  90. //
  91. // buttonCancel
  92. //
  93. this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  94. this.buttonCancel.Location = new System.Drawing.Point(288, 80);
  95. this.buttonCancel.Name = "buttonCancel";
  96. this.buttonCancel.TabIndex = 3;
  97. this.buttonCancel.Text = "Cancel";
  98. this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click);
  99. //
  100. // SamplesListInput
  101. //
  102. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  103. this.ClientSize = new System.Drawing.Size(376, 110);
  104. this.ControlBox = false;
  105. this.Controls.Add(this.buttonCancel);
  106. this.Controls.Add(this.buttonOK);
  107. this.Controls.Add(this.dataList);
  108. this.Controls.Add(this.descriptionText);
  109. this.Name = "SamplesListInput";
  110. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
  111. this.Text = "SamplesListInput";
  112. this.ResumeLayout(false);
  113. }
  114. #endregion
  115. private void buttonCancel_Click(object sender, System.EventArgs e)
  116. {
  117. this.Close();
  118. }
  119. public string getInput( string title, string description, string[] itemList, int selectedIndex)
  120. {
  121. if ( (title == null) || (0 == title.CompareTo("")) )
  122. {
  123. this.Text = "";
  124. }
  125. else
  126. {
  127. this.Text = title;
  128. }
  129. if ( (description == null) || (0 == description.CompareTo("")) )
  130. {
  131. descriptionText.Text = "";
  132. }
  133. else
  134. {
  135. descriptionText.Text = description;
  136. }
  137. int nbItems = itemList.GetLength(0);
  138. for (int i=0; i<nbItems; i++)
  139. {
  140. dataList.Items.Add(itemList[i]);
  141. }
  142. dataList.SelectedIndex = selectedIndex;
  143. string dataValue = "";
  144. DialogResult result = ShowDialog();
  145. if( result == DialogResult.Cancel )
  146. {
  147. throw new SamplesInputException( "INPUT_CANCELLED_BY_USER" );
  148. }
  149. else if (result == DialogResult.OK )
  150. {
  151. dataValue = (string)dataList.SelectedItem;
  152. if ( (dataValue == null) || (0 == dataValue.CompareTo("")) )
  153. {
  154. MessageBox.Show("Invalid value selected.");
  155. throw new SamplesInputException( "INPUT_CANCELLED_BY_USER" );
  156. }
  157. }
  158. return dataValue;
  159. }
  160. private void buttonOK_Click(object sender, System.EventArgs e)
  161. {
  162. this.Close();
  163. }
  164. private void dataList_OnKeyPress(object sender, KeyEventArgs e)
  165. {
  166. if (e.KeyCode == Keys.Enter)
  167. {
  168. buttonOK_Click(sender, e);
  169. }
  170. }
  171. /// <summary>
  172. /// Exception thrown by the simple text input dialog when you click Cancel.
  173. /// </summary>
  174. public class SamplesInputException : System.Exception
  175. {
  176. public SamplesInputException( string message )
  177. : base( message )
  178. {
  179. }
  180. }
  181. }
  182. }