kolaron

InputDialog

Aug 12th, 2016
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.59 KB | None | 0 0
  1. string value = "Document 1";
  2. if (Tmp.InputBox("New document", "New document name:", ref value) == DialogResult.OK)
  3. {
  4.   myDocument.Name = value;
  5. }
  6.  
  7.  
  8. using System.Windows.Forms;
  9. using System.Drawing;
  10.  
  11. public static DialogResult InputBox(string title, string promptText, ref string value)
  12. {
  13.   Form form = new Form();
  14.   Label label = new Label();
  15.   TextBox textBox = new TextBox();
  16.   Button buttonOk = new Button();
  17.   Button buttonCancel = new Button();
  18.  
  19.   form.Text = title;
  20.   label.Text = promptText;
  21.   textBox.Text = value;
  22.  
  23.   buttonOk.Text = "OK";
  24.   buttonCancel.Text = "Cancel";
  25.   buttonOk.DialogResult = DialogResult.OK;
  26.   buttonCancel.DialogResult = DialogResult.Cancel;
  27.  
  28.   label.SetBounds(9, 20, 372, 13);
  29.   textBox.SetBounds(12, 36, 372, 20);
  30.   buttonOk.SetBounds(228, 72, 75, 23);
  31.   buttonCancel.SetBounds(309, 72, 75, 23);
  32.  
  33.   label.AutoSize = true;
  34.   textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
  35.   buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
  36.   buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
  37.  
  38.   form.ClientSize = new Size(396, 107);
  39.   form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
  40.   form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
  41.   form.FormBorderStyle = FormBorderStyle.FixedDialog;
  42.   form.StartPosition = FormStartPosition.CenterScreen;
  43.   form.MinimizeBox = false;
  44.   form.MaximizeBox = false;
  45.   form.AcceptButton = buttonOk;
  46.   form.CancelButton = buttonCancel;
  47.  
  48.   DialogResult dialogResult = form.ShowDialog();
  49.   value = textBox.Text;
  50.   return dialogResult;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment