2011-03-24 130 views
80

我正在使用System.Windows.Forms但奇怪的是没有能力创建它们。Windows窗体中的提示对话框

我怎样才能得到像JavaScript提示对话框,没有JavaScript的东西?

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

+0

你能后的你想要做什么的代码示例? – 2011-03-24 23:55:41

+0

你在寻找什么样的输入,给予更多的细节。 [CommonDialog](http://msdn.microsoft.com/en-us/library/system.windows.forms.commondialog.aspx)看看继承它的类吗?他们中的任何一个都适合你吗? – 2011-03-24 23:55:49

+13

有趣的是,三个人如何要求OP提供更多的细节和代码示例,但很明显他的意思是*“就像一个JavaScript提示对话框”*。 – 2013-02-05 04:53:34

回答

208

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

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最初的焦点。

+1

如何扩展它到A)有一个取消按钮和B)在返回之前以某种方式验证文本字段中的文本? – ewok 2012-06-26 16:08:40

+0

@ewok只需创建一个表单,Forms Designer将帮助您按照自己想要的方式进行布局。 – 2013-02-05 03:32:13

+0

不错的快速解决方案!我只是加了 提示符。高= 150;在ShowDialog之前获取 以获取窗口上的所有控件。 – werner 2013-02-22 09:18:44

15

有没有这样的事情在本地Windows窗体。

你必须创建为或你自己的形式:

使用Microsoft.VisualBasic参考。

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

+6

+1,指出这是VB6兼容性的遗留代码。 – harsimranb 2014-02-11 21:50:18

6

将VisualBasic库导入到C#程序中并不是一个真正的好主意(不是因为它们不起作用,而仅仅是为了兼容性,样式和升级能力),但您可以调用Microsoft.VisualBasic.Interaction .InputBox()显示您正在查找的框的种类。

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

+18

为什么这不是一个好主意?什么是可能的“兼容性”和“升级能力”问题?我会同意“风格上”的说法,大多数C#程序员宁愿不使用名为'VisualBasic'的命名空间中的类,但这只是他们的头脑。这种感觉没有现实。它也可以称为'Microsoft.MakeMyLifeEasierWithAlreadyImplementedMethods'命名空间。 – 2011-05-19 03:54:37

+3

一般而言,Microsoft.VisualBasic包旨在简化仅从VB 6升级代码。微软一直威胁永久性的日落VB(尽管这可能永远不会实际发生),所以对这个命名空间的未来支持是不能保证的。此外,.Net的优势之一应该是可移植性 - 相同的代码可以在安装了.Net框架的任何平台上运行。然而,Microsoft.VisualBasic不能保证在任何其他平台上可用(包括它的价值,.Net移动,根本不可用)。 – 2011-06-07 15:40:52

+15

**不正确。**这是'Microsoft.VisualBasic.Compatibility'子命名空间,不是全部。许多“核心”功能包含在'Microsoft.VisualBasic'命名空间中;它不会去任何地方。微软已经威胁要“日落”VB 6,而不是VB.NET。他们一再承诺*不会去任何地方。有些人似乎还没有想出区别... – 2011-06-07 15:41:53

34

添加参考Microsoft.VisualBasic并使用该到你的C#代码:

string input = Microsoft.VisualBasic.Interaction.InputBox("Prompt", 
         "Title", 
         "Default", 
         0, 
         0); 
+1

这就是说'Interaction'不存在于命名空间'Microsoft.VisualBasic'中 – 2017-02-21 01:44:24

+0

要使用此方法,您需要添加一个就像他说的那样,引用了Microsoft.VisualBasic。 要做到这一点,您必须右键单击“项目浏览器”窗口中的引用,然后单击添加引用,然后从该列表中选中VisualBasic。 – kisslory 2018-02-06 21:48:43

+0

这比自定义分类解决方案稍好,因为它支持高分辨率的屏幕 – 2018-02-07 18:17:48

1

巴斯Brekelmans的答案是非常优雅,在它的简单性。但是,我发现对于实际应用,需要多一点,例如:

  • 当消息文本太长时适当增长表单。
  • 不会在屏幕中间自动弹出。
  • 不提供任何用户输入验证。

这里的类处理这些限制: http://www.codeproject.com/Articles/31315/Getting-User-Input-With-Dialogs-Part-1

我刚刚下载源和复制InputBox.cs到我的项目。

惊讶没有什么更好的,虽然...我唯一真正的抱怨是它标题文本不支持换行符,因为它使用标签控件。

4

其他做法的方法: 假设您有一个TextBox输入类型, 创建一个窗体,并将该文本框的值作为公共属性。

public partial class TextPrompt : Form 
{ 
    public string Value 
    { 
     get { return tbText.Text.Trim(); } 
    } 

    public TextPrompt(string promptInstructions) 
    { 
     InitializeComponent(); 

     lblPromptText.Text = promptInstructions; 
    } 

    private void BtnSubmitText_Click(object sender, EventArgs e) 
    { 
     Close(); 
    } 

    private void TextPrompt_Load(object sender, EventArgs e) 
    { 
     CenterToParent(); 
    } 
} 

在主要形式,这将是代码:

var t = new TextPrompt(this, "Type the name of the settings file:"); 
t.ShowDialog() 

;

这样,代码看起来更清洁:

  1. 如果添加验证逻辑。
  2. 如果添加了各种其他输入类型。
1

基于巴斯Brekelmans以上的工作,我也创建了两个推导 - >“输入”对话框,允许你从用户接收两个文本值和一个布尔(文本框和CheckBox):

public static class PromptForTextAndBoolean 
{ 
    public static string ShowDialog(string caption, string text, string boolStr) 
    { 
     Form prompt = new Form(); 
     prompt.Width = 280; 
     prompt.Height = 160; 
     prompt.Text = caption; 
     Label textLabel = new Label() { Left = 16, Top = 20, Width = 240, Text = text }; 
     TextBox textBox = new TextBox() { Left = 16, Top = 40, Width = 240, TabIndex = 0, TabStop = true }; 
     CheckBox ckbx = new CheckBox() { Left = 16, Top = 60, Width = 240, Text = boolStr }; 
     Button confirmation = new Button() { Text = "Okay!", Left = 16, Width = 80, Top = 88, TabIndex = 1, TabStop = true }; 
     confirmation.Click += (sender, e) => { prompt.Close(); }; 
     prompt.Controls.Add(textLabel); 
     prompt.Controls.Add(textBox); 
     prompt.Controls.Add(ckbx); 
     prompt.Controls.Add(confirmation); 
     prompt.AcceptButton = confirmation; 
     prompt.StartPosition = FormStartPosition.CenterScreen; 
     prompt.ShowDialog(); 
     return string.Format("{0};{1}", textBox.Text, ckbx.Checked.ToString()); 
    } 
} 

...并与选择的多个选项之一的(文本框和组合框)沿文:

public static class PromptForTextAndSelection 
{ 
    public static string ShowDialog(string caption, string text, string selStr) 
    { 
     Form prompt = new Form(); 
     prompt.Width = 280; 
     prompt.Height = 160; 
     prompt.Text = caption; 
     Label textLabel = new Label() { Left = 16, Top = 20, Width = 240, Text = text }; 
     TextBox textBox = new TextBox() { Left = 16, Top = 40, Width = 240, TabIndex = 0, TabStop = true }; 
     Label selLabel = new Label() { Left = 16, Top = 66, Width = 88, Text = selStr }; 
     ComboBox cmbx = new ComboBox() { Left = 112, Top = 64, Width = 144 }; 
     cmbx.Items.Add("Dark Grey"); 
     cmbx.Items.Add("Orange"); 
     cmbx.Items.Add("None"); 
     Button confirmation = new Button() { Text = "In Ordnung!", Left = 16, Width = 80, Top = 88, TabIndex = 1, TabStop = true }; 
     confirmation.Click += (sender, e) => { prompt.Close(); }; 
     prompt.Controls.Add(textLabel); 
     prompt.Controls.Add(textBox); 
     prompt.Controls.Add(selLabel); 
     prompt.Controls.Add(cmbx); 
     prompt.Controls.Add(confirmation); 
     prompt.AcceptButton = confirmation; 
     prompt.StartPosition = FormStartPosition.CenterScreen; 
     prompt.ShowDialog(); 
     return string.Format("{0};{1}", textBox.Text, cmbx.SelectedItem.ToString()); 
    } 
} 

都需要相同usings:

using System; 
using System.Windows.Forms; 

打电话给他们,像这样:

打电话给他们,像这样:

PromptForTextAndBoolean.ShowDialog("Jazz", "What text should accompany the checkbox?", "Allow Scat Singing"); 

PromptForTextAndSelection.ShowDialog("Rock", "What should the name of the band be?", "Beret color to wear"); 
0

Microsoft.VisualBasic.Interaction.InputBox有几个缺点。 如果用户点击确定或取消,如何管理?你不能。

你应该写自己的班级或使用类似OOKII Dialogs'InputControl。 OOKII很棒。

-2

下面是VB.NET

Public Function ShowtheDialog(caption As String, text As String, selStr As String) As String 
    Dim prompt As New Form() 
    prompt.Width = 280 
    prompt.Height = 160 
    prompt.Text = caption 
    Dim textLabel As New Label() With { _ 
     .Left = 16, _ 
     .Top = 20, _ 
     .Width = 240, _ 
     .Text = text _ 
    } 
    Dim textBox As New TextBox() With { _ 
     .Left = 16, _ 
     .Top = 40, _ 
     .Width = 240, _ 
     .TabIndex = 0, _ 
     .TabStop = True _ 
    } 
    Dim selLabel As New Label() With { _ 
     .Left = 16, _ 
     .Top = 66, _ 
     .Width = 88, _ 
     .Text = selStr _ 
    } 
    Dim cmbx As New ComboBox() With { _ 
     .Left = 112, _ 
     .Top = 64, _ 
     .Width = 144 _ 
    } 
    cmbx.Items.Add("Dark Grey") 
    cmbx.Items.Add("Orange") 
    cmbx.Items.Add("None") 
    cmbx.SelectedIndex = 0 
    Dim confirmation As New Button() With { _ 
     .Text = "In Ordnung!", _ 
     .Left = 16, _ 
     .Width = 80, _ 
     .Top = 88, _ 
     .TabIndex = 1, _ 
     .TabStop = True _ 
    } 
    AddHandler confirmation.Click, Sub(sender, e) prompt.Close() 
    prompt.Controls.Add(textLabel) 
    prompt.Controls.Add(textBox) 
    prompt.Controls.Add(selLabel) 
    prompt.Controls.Add(cmbx) 
    prompt.Controls.Add(confirmation) 
    prompt.AcceptButton = confirmation 
    prompt.StartPosition = FormStartPosition.CenterScreen 
    prompt.ShowDialog() 
    Return String.Format("{0};{1}", textBox.Text, cmbx.SelectedItem.ToString()) 
End Function 
0

一个例子巴斯的回答可以让你在memorytrouble理论上,因为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() 
    { 
     prompt.Dispose(); 
    } 
} 

实现:

using(Prompt prompt = new Prompt("text", "caption")){ 
    string result = prompt.Result; 
} 
相关问题