00001 using System;
00002 using System.Drawing;
00003 using System.Collections;
00004 using System.ComponentModel;
00005 using System.Windows.Forms;
00006 using System.Data;
00007
00008 namespace Tamir.SharpSsh.jsch.examples
00009 {
00013 public class InputForm : System.Windows.Forms.Form
00014 {
00015 private static InputForm inForm;
00016 public System.Windows.Forms.TextBox textBox1;
00017 private System.Windows.Forms.Button btnOK;
00018 private System.Windows.Forms.Button btnCancel;
00022 private System.ComponentModel.Container components = null;
00023
00024 public InputForm()
00025 {
00026 InitializeComponent();
00027 }
00028
00029 private static InputForm Instance
00030 {
00031 get{
00032 if(inForm==null) inForm = new InputForm();
00033 return inForm;
00034 }
00035 }
00036
00037 public static string GetUserInput(string title, string devaultValue)
00038 {
00039 return GetUserInput(title, devaultValue, false);
00040 }
00041
00042 public static string GetUserInput(string title, bool password)
00043 {
00044 return GetUserInput(title, "", password);
00045 }
00046
00047
00048 public static string GetUserInput(string title, string devaultValue, bool password)
00049 {
00050 Instance.Text = title;
00051 Instance.textBox1.Text = devaultValue;
00052 Instance.PasswordField = password;
00053 if(Instance.PromptForInput())
00054 {
00055 return Instance.textBox1.Text;
00056 }
00057 else
00058 {
00059 Console.WriteLine("Canceled by user");
00060 Environment.Exit(0);
00061 return null;
00062 }
00063 }
00064
00065 public static string GetFileFromUser(string msg)
00066 {
00067 OpenFileDialog chooser = new OpenFileDialog();
00068 chooser.Title = msg;
00069 DialogResult returnVal = chooser.ShowDialog();
00070 if(returnVal == DialogResult.OK)
00071 {
00072 return chooser.FileName;
00073 }
00074 else
00075 {
00076 Console.WriteLine("Canceled by user");
00077 Environment.Exit(0);
00078 return null;
00079 }
00080 }
00081
00082 public static bool PromptYesNo(String str)
00083 {
00084 DialogResult returnVal = MessageBox.Show(
00085 str,
00086 "SharpSSH",
00087 MessageBoxButtons.YesNo,
00088 MessageBoxIcon.Warning);
00089 return (returnVal==DialogResult.Yes);
00090 }
00091
00092 public static void ShowMessage(String message)
00093 {
00094 MessageBox.Show(
00095 message,
00096 "SharpSSH",
00097 MessageBoxButtons.OK,
00098 MessageBoxIcon.Asterisk);
00099 }
00100
00104 protected override void Dispose( bool disposing )
00105 {
00106 if( disposing )
00107 {
00108 if (components != null)
00109 {
00110 components.Dispose();
00111 }
00112 }
00113 base.Dispose( disposing );
00114 }
00115
00116 #region Windows Form Designer generated code
00121 private void InitializeComponent()
00122 {
00123 this.textBox1 = new System.Windows.Forms.TextBox();
00124 this.btnOK = new System.Windows.Forms.Button();
00125 this.btnCancel = new System.Windows.Forms.Button();
00126 this.SuspendLayout();
00127
00128
00129
00130 this.textBox1.Location = new System.Drawing.Point(56, 24);
00131 this.textBox1.Name = "textBox1";
00132 this.textBox1.Size = new System.Drawing.Size(160, 20);
00133 this.textBox1.TabIndex = 0;
00134 this.textBox1.Text = "";
00135
00136
00137
00138 this.btnOK.Location = new System.Drawing.Point(56, 64);
00139 this.btnOK.Name = "btnOK";
00140 this.btnOK.TabIndex = 1;
00141 this.btnOK.Text = "OK";
00142 this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
00143
00144
00145
00146 this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
00147 this.btnCancel.Location = new System.Drawing.Point(144, 64);
00148 this.btnCancel.Name = "btnCancel";
00149 this.btnCancel.TabIndex = 2;
00150 this.btnCancel.Text = "Cancel";
00151 this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
00152
00153
00154
00155 this.AcceptButton = this.btnOK;
00156 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
00157 this.CancelButton = this.btnCancel;
00158 this.ClientSize = new System.Drawing.Size(264, 110);
00159 this.Controls.Add(this.btnCancel);
00160 this.Controls.Add(this.btnOK);
00161 this.Controls.Add(this.textBox1);
00162 this.Name = "InputForm";
00163 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
00164 this.Text = "Form1";
00165 this.ResumeLayout(false);
00166
00167 }
00168 #endregion
00169
00170 bool okBtnClicked = false;
00171
00172 private void btnOK_Click(object sender, System.EventArgs e)
00173 {
00174 okBtnClicked = true;
00175 this.Hide();
00176 }
00177
00178 private void btnCancel_Click(object sender, System.EventArgs e)
00179 {
00180 okBtnClicked = false;
00181 this.textBox1.Text = "";
00182 this.Hide();
00183 }
00184
00185 public bool PromptForInput()
00186 {
00187 this.ShowDialog();
00188 return okBtnClicked;
00189 }
00190
00191 public string getText()
00192 {
00193 return textBox1.Text;
00194 }
00195
00196 public bool PasswordField
00197 {
00198 get
00199 {
00200 return (textBox1.PasswordChar.Equals(0));
00201 }
00202 set
00203 {
00204 if (value)
00205 textBox1.PasswordChar='*';
00206 else
00207 textBox1.PasswordChar=(char)0;
00208 }
00209 }
00210 }
00211 }