Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C# have some function that Input a String with a Form?

I want the write a simple application, that input some strings, and do something with it.

I search for a function that is similar like Delphi's InputBox or InputQuery, what is read a string from a predefined form. This is like MessageBox is very useful to avoid by-hand form creation.

For example:

p1 := InputBox('Type the parameter', 'Parameter 1');
...

But I don't found any function. Can anybody know about it?

Thanks: dd

like image 639
durumdara Avatar asked Dec 07 '22 21:12

durumdara


2 Answers

You can use the InputBox from the VisualBasic namespace:

var str1 = Microsoft.VisualBasic.Interaction.InputBox("Name:", "", "", 100, 100);

Just because it is in the VisualBasic namespace doesn't mean it is bad!

like image 186
Daniel Hilgarth Avatar answered Dec 15 '22 00:12

Daniel Hilgarth


using System.Windows.Forms;
using System.Drawing;

public static DialogResult InputBox(string title, string promptText, ref string value)
{
  Form form = new Form();
  Label label = new Label();
  TextBox textBox = new TextBox();
  Button buttonOk = new Button();
  Button buttonCancel = new Button();

  form.Text = title;
  label.Text = promptText;
  textBox.Text = value;

  buttonOk.Text = "OK";
  buttonCancel.Text = "Cancel";
  buttonOk.DialogResult = DialogResult.OK;
  buttonCancel.DialogResult = DialogResult.Cancel;

  label.SetBounds(9, 20, 372, 13);
  textBox.SetBounds(12, 36, 372, 20);
  buttonOk.SetBounds(228, 72, 75, 23);
  buttonCancel.SetBounds(309, 72, 75, 23);

  label.AutoSize = true;
  textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
  buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
  buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

  form.ClientSize = new Size(396, 107);
  form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
  form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
  form.FormBorderStyle = FormBorderStyle.FixedDialog;
  form.StartPosition = FormStartPosition.CenterScreen;
  form.MinimizeBox = false;
  form.MaximizeBox = false;
  form.AcceptButton = buttonOk;
  form.CancelButton = buttonCancel;

  DialogResult dialogResult = form.ShowDialog();
  value = textBox.Text;
  return dialogResult;
}

taken from: http://www.csharp-examples.net/inputbox/

like image 43
jgauffin Avatar answered Dec 15 '22 00:12

jgauffin