Prompt Dialog in Windows Forms(Windows 窗体中的提示对话框)
问题描述
我正在使用 System.Windows.Forms
但奇怪的是没有能力创建它们.
I am using System.Windows.Forms
but strangely enough don't have the ability to create them.
如何在没有 javascript 的情况下获得类似 javascript 提示对话框的内容?
How can I get something like a javascript prompt dialog, without javascript?
MessageBox 不错,但用户无法输入.
MessageBox is nice, but there is no way for the user to enter an input.
我希望用户输入任何可能的文本输入.
I want the user to enter any text input possible.
推荐答案
您需要创建自己的提示对话框.您也许可以为此创建一个类.
You need to create your own Prompt dialog. You could perhaps create a class for this.
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 : "";
}
}
并称它为:
string promptValue = Prompt.ShowDialog("Test", "123");
更新:
添加了默认按钮(输入键)和基于评论的初始焦点和另一个问题.
Added default button (enter key) and initial focus based on comments and another question.
这篇关于Windows 窗体中的提示对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Windows 窗体中的提示对话框


- 输入按键事件处理程序 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04