Windows窗体中的提示对话框

use*_*667 104 .net c# dialog prompt winforms

我正在使用,System.Windows.Forms但奇怪的是,没有能力创建它们.

如何在没有javascript的情况下获得类似javascript提示对话框的内容?

MessageBox很不错,但用户无法输入输入.

Bas*_*Bas 258

您需要创建自己的"提示"对话框.你也许可以为此创建一个类.

public static class Prompt
{
    public static string ShowDialog(string text, string caption)
    {
        Form prompt = new Form()
        {
            Width = 500,
            Height = 150,
            FormBorderStyle = FormBorderStyle.FixedDialog,
            Text = caption,
            StartPosition = FormStartPosition.CenterScreen
        };
        Label textLabel = new Label() { Left = 50, Top=20, Text=text };
        TextBox textBox = new TextBox() { Left = 50, Top=50, Width=400 };
        Button confirmation = new Button() { Text = "Ok", Left=350, Width=100, Top=70, DialogResult = DialogResult.OK };
        confirmation.Click += (sender, e) => { prompt.Close(); };
        prompt.Controls.Add(textBox);
        prompt.Controls.Add(confirmation);
        prompt.Controls.Add(textLabel);
        prompt.AcceptButton = confirmation;

        return prompt.ShowDialog() == DialogResult.OK ? textBox.Text : "";
    }
}
Run Code Online (Sandbox Code Playgroud)

并称之为:

string promptValue = Prompt.ShowDialog("Test", "123");
Run Code Online (Sandbox Code Playgroud)

更新:

根据评论和其他问题添加了默认按钮(输入键)和初始焦点.

  • 如何将其扩展到 A)有一个取消按钮和 B)在返回之前以某种方式验证文本字段中的文本? (2认同)

小智 46

添加引用Microsoft.VisualBasic并将其用于您的C#代码:

string input = Microsoft.VisualBasic.Interaction.InputBox("Prompt", 
                       "Title", 
                       "Default", 
                       0, 
                       0);
Run Code Online (Sandbox Code Playgroud)

  • 这表示命名空间`Microsoft.VisualBasic`中不存在`Interaction` (3认同)
  • 我知道使用自定义解决方案可能会更好,但我正在寻找一种快速且简单的解决方案,这是最好的。真的谢谢大家。 (2认同)

Mar*_*mić 15

在Windows窗体中本机没有这样的东西.

您必须为此创建自己的表单或:

使用Microsoft.VisualBasic参考.

Inputbox是为.Net与VB6兼容而带入的遗留代码 - 所以我建议不要这样做.

  • 对于“Microsoft.VisualBasic.Compatibility”命名空间来说也是如此。“Microsoft.VisualBasic”更多的是一组基于 .Net 的帮助程序库,根本不是 VB 特有的。 (2认同)

Sea*_*rle 7

将VisualBasic库导入C#程序通常不是一个好主意(不是因为它们不起作用,而是因为兼容性,样式和升级能力),但是你可以调用Microsoft.VisualBasic.Interaction.InputBox()显示您正在寻找的那种盒子.

如果你可以创建一个Windows.Forms对象,那将是最好的,但你说你不能这样做.

  • 为什么这不是一个好主意?什么是可能的"兼容性"和"升级能力"问题?我同意"风格化"的说法,大多数C#程序员不愿意使用名为`VisualBasic`的命名空间中的类,但这只是在他们的脑海里.那种感觉没有现实.它也可以称为`Microsoft.MakeMyLifeEasierWithAlreadyImplementedMethods`命名空间. (23认同)
  • **不正确.**这是"Microsoft.VisualBasic.Compatibility"子命名空间,而不是整个事物."Microsoft.VisualBasic"命名空间中包含许多"核心"功能; 它不会去任何地方.微软已经威胁要"落日"VB 6,而不是VB.NET.他们已经多次承诺*它不会去任何地方.有些人似乎还没有弄清楚差异...... (19认同)
  • 通常,Microsoft.VisualBasic包仅用于简化从VB 6升级代码.微软一直威胁要永久性地使用VB(尽管这可能永远不会发生),因此无法保证对此命名空间的未来支持.此外,.Net的一个优点是可移植性 - 相同的代码将在任何安装了.Net框架的平台上运行.但是,不保证Microsoft.VisualBasic可以在任何其他平台上使用(包括,它的价值,.Net移动,根本不可用). (3认同)

Gid*_*der 6

Bas 的答案理论上可以让你陷入内存问题,因为 ShowDialog 不会被处理。我认为这是一个更合适的方法。还要提到 textLabel 可以用更长的文本读取。

public class Prompt : IDisposable
{
    private Form prompt { get; set; }
    public string Result { get; }

    public Prompt(string text, string caption)
    {
        Result = ShowDialog(text, caption);
    }
    //use a using statement
    private string ShowDialog(string text, string caption)
    {
        prompt = new Form()
        {
            Width = 500,
            Height = 150,
            FormBorderStyle = FormBorderStyle.FixedDialog,
            Text = caption,
            StartPosition = FormStartPosition.CenterScreen,
            TopMost = true
        };
        Label textLabel = new Label() { Left = 50, Top = 20, Text = text, Dock = DockStyle.Top, TextAlign = ContentAlignment.MiddleCenter };
        TextBox textBox = new TextBox() { Left = 50, Top = 50, Width = 400 };
        Button confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 70, DialogResult = DialogResult.OK };
        confirmation.Click += (sender, e) => { prompt.Close(); };
        prompt.Controls.Add(textBox);
        prompt.Controls.Add(confirmation);
        prompt.Controls.Add(textLabel);
        prompt.AcceptButton = confirmation;

        return prompt.ShowDialog() == DialogResult.OK ? textBox.Text : "";
    }

    public void Dispose()
    {
        //See Marcus comment
        if (prompt != null) { 
            prompt.Dispose(); 
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

执行:

using(Prompt prompt = new Prompt("text", "caption")){
    string result = prompt.Result;
}
Run Code Online (Sandbox Code Playgroud)

  • 善用内存管理。但是,添加取消按钮时会失败,因为此时 `prompt` 为空。允许取消提示的一个简单修复是将 `public void Dispose()` 中的 `prompt.Dispose();` 替换为 `if (prompt != null) { prompt.Dispose(); }` (2认同)