C#是否有一些使用表单输入字符串的函数

本文关键字:输入 表单 字符串 函数 是否 | 更新日期: 2023-09-27 17:56:19

我想写一个简单的应用程序,输入一些字符串,然后用它做一些事情。

我搜索一个类似于Delphi的InputBox或InputQuery的函数,即从预定义表单中读取字符串的内容。这就像 MessageBox 对于避免手动创建表单非常有用。

例如:

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

但我没有找到任何功能。有人能知道吗?

谢谢: 日嘎

C#是否有一些使用表单输入字符串的函数

您可以使用 VisualBasic 命名空间中的 InputBox:

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

仅仅因为它在 VisualBasic 命名空间中并不意味着它是坏的!

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;
}

取自: http://www.csharp-examples.net/inputbox/

试试这个: MSDN 论坛 - 简单的对话框提示在哪里(类似于消息框)?

引用接受的答案:

没有这样的事情... 除非你 正在使用 Visual Basic 或专门使用 添加对 Microsoft.VisualBasic Assembly。

添加对 Microsoft.VisualBasic,您可以使用 代码如下所示:

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

我想说我发现这个 对话在视觉上"丑陋"——我 会建议你自己动手 对话。

您可以使用

输入字段编写自己的窗口,也可以使用Microsoft Visual Basic .NET Runtime程序集中的Microsoft.VisualBasic命名空间。

你可以是:

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

看看这篇文章。我认为这对你有帮助。

.

NET因此是C#,没有像Delphi中的InputBox或Java中的showInputDialog方法的输入对话框那样的内置输入形式。

上。忘记了可以使用的Visual Basic程序集。