开发者

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 examp开发者_JS百科le:

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

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

Thanks: dd


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!


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/


Try this: MSDN Forums - Where is the simple dialog prompt (similar to MessageBox)?

To quote the accepted answer:

There is no such thing... Unless you are using Visual Basic or specially add a reference to the the Microsoft.VisualBasic assembly.

Once you have added a reference to Microsoft.VisualBasic, you can use code like the following:

string s = Microsoft.VisualBasic.Interaction.InputBox("Enter string:", "Data Entry", "", -1, -1); 

I would like to say that I find this dialog to be visually "ugly" -- I would recommend that you roll your own dialog.


You can write your own window with an Input field OR use the Microsoft.VisualBasic namespace from the Microsoft Visual Basic .NET Runtime assembly.

Your could would be:

string result = Interaction.InputBox("Param1","Param2" ...);


Have a look at this post. I think this is helpful to you.


.NET, hence C#, does not have a buit-in input form like InputBox in Delphi or Input Dialog Box via the showInputDialog method in Java.

UPD. Forgot about the Visual Basic assembly with which it is possible.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜