gpt4 book ai didi

c# - Windows 窗体中的提示对话框

转载 作者:IT王子 更新时间:2023-10-29 03:36:00 24 4
gpt4 key购买 nike

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

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

MessageBox 很好,但用户无法输入内容。

我希望用户输入任何可能的文本。

最佳答案

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

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");

更新:

添加了默认按钮(输入键)和基于评论和 another question 的初始焦点.

关于c# - Windows 窗体中的提示对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5427020/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com