c# - WinForms(Windows 窗体)的等效 JDialog 是什么?

标签 c# winforms jdialog

我尝试使用 WinForms 构建一个应用程序,我需要类似 JDialog 框架的东西来在其中插入几个文本框(Java 中的 JTextField)以及两个按钮(确定和取消),但我还没有找到任何合适的 Windows形式。有什么建议吗?

最佳答案

C#中没有提示对话框。您可以创建一个自定义提示框来执行此操作。

  public static class Prompt
    {
        public static int ShowDialog(string text, string caption)
        {
            Form prompt = new Form();
            prompt.Width = 500;
            prompt.Height = 100;
            prompt.Text = caption;
            Label textLabel = new Label() { Left = 50, Top=20, Text=text };
            NumericUpDown inputBox = new NumericUpDown () { Left = 50, Top=50, Width=400 };
            Button confirmation = new Button() { Text = "Ok", Left=350, Width=100, Top=70 };
            confirmation.Click += (sender, e) => { prompt.Close(); };
            prompt.Controls.Add(confirmation);
            prompt.Controls.Add(textLabel);
            prompt.Controls.Add(inputBox);
            prompt.ShowDialog();
            return (int)inputBox.Value;
        }
    }

然后调用它:

int promptValue = Prompt.ShowDialog("Test", "123");

我从 here 得到这个

或者使用这个:

using( MyDialog dialog = new MyDialog() )
{
    DialogResult result = dialog.ShowDialog();

    switch (result)
    {
    // put in how you want the various results to be handled
    // if ok, then something like var x = dialog.MyX;
    }

}

关于c# - WinForms(Windows 窗体)的等效 JDialog 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31921549/

相关文章:

java - 在非模态模式下等待 jdialog

java - 如何给JDialog添加组件

c# - 将 Int 数组转换为枚举标志

c# - 加载实体实例需要超过 1 秒

c# - Windows Phone 后退按钮未被处理

.net - 在C#.net的OpenFileDialog中更改按钮文本

c# - Json.net 自定义枚举转换器

c# - ListView 中的图像质量差

asp.net - 从基于 Window 的开发转向基于 Web 的开发

java - JDialog 中的 JTextField 在处理后保留值