Prompt Dialog in Windows Forms

前端 未结 11 1415
名媛妹妹
名媛妹妹 2020-11-30 19:47

I am using System.Windows.Forms but strangely enough don\'t have the ability to create them.

How can I get something like a javascript prompt dialog, wi

相关标签:
11条回答
  • 2020-11-30 20:16

    It's generally not a real good idea to import the VisualBasic libraries into C# programs (not because they won't work, but just for compatibility, style, and ability to upgrade), but you can call Microsoft.VisualBasic.Interaction.InputBox() to display the kind of box you're looking for.

    If you can create a Windows.Forms object, that would be best, but you say you cannot do that.

    0 讨论(0)
  • 2020-11-30 20:17

    Bas Brekelmans's answer is very elegant in its simplicity. But, I found that for an actual application a little more is needed such as:

    • Grow form appropriately when message text is too long.
    • Doesn't automatically popup in middle of screen.
    • Doesn't provide any validation of user input.

    The class here handles these limitations: http://www.codeproject.com/Articles/31315/Getting-User-Input-With-Dialogs-Part-1

    I just downloaded source and copied InputBox.cs into my project.

    Surprised there isn't something even better though... My only real complaint is it caption text doesn't support newlines in it since it uses a label control.

    0 讨论(0)
  • 2020-11-30 20:18

    Add reference to Microsoft.VisualBasic and use this into your C# code:

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

    To add the refernce: right-click on the References in your Project Explorer window then on Add Reference, and check VisualBasic from that list.

    0 讨论(0)
  • 2020-11-30 20:24

    Other way of doing this: Assuming that you have a TextBox input type, Create a Form, and have the textbox value as a public property.

    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();
        }
    }
    

    In the main form, this will be the code:

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

    ;

    This way, the code looks cleaner:

    1. If validation logic is added.
    2. If various other input types are added.
    0 讨论(0)
  • 2020-11-30 20:24

    here's my refactored version which accepts multiline/single as an option

       public string ShowDialog(string text, string caption, bool isMultiline = false, int formWidth = 300, int formHeight = 200)
            {
                var prompt = new Form
                {
                    Width = formWidth,
                    Height = isMultiline ? formHeight : formHeight - 70,
                    FormBorderStyle = isMultiline ? FormBorderStyle.Sizable : FormBorderStyle.FixedSingle,
                    Text = caption,
                    StartPosition = FormStartPosition.CenterScreen,
                    MaximizeBox = isMultiline
                };
    
                var textLabel = new Label
                {
                    Left = 10,
                    Padding = new Padding(0, 3, 0, 0),
                    Text = text,
                    Dock = DockStyle.Top
                };
    
                var textBox = new TextBox
                {
                    Left = isMultiline ? 50 : 4,
                    Top = isMultiline ? 50 : textLabel.Height + 4,
                    Multiline = isMultiline,
                    Dock = isMultiline ? DockStyle.Fill : DockStyle.None,
                    Width = prompt.Width - 24,
                    Anchor = isMultiline ? AnchorStyles.Left | AnchorStyles.Top : AnchorStyles.Left | AnchorStyles.Right
                };
    
                var confirmationButton = new Button
                {
                    Text = @"OK",
                    Cursor = Cursors.Hand,
                    DialogResult = DialogResult.OK,
                    Dock = DockStyle.Bottom,
                };
    
                confirmationButton.Click += (sender, e) =>
                {
                    prompt.Close();
                };
    
                prompt.Controls.Add(textBox);
                prompt.Controls.Add(confirmationButton);
                prompt.Controls.Add(textLabel);
    
                return prompt.ShowDialog() == DialogResult.OK ? textBox.Text : string.Empty;
            }
    
    0 讨论(0)
提交回复
热议问题