Prompt Dialog in Windows Forms

前端 未结 11 1416
名媛妹妹
名媛妹妹 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:10

    Unfortunately C# still doesn't offer this capability in the built in libs. The best solution at present is to create a custom class with a method that pops up a small form. If you're working in Visual Studio you can do this by clicking on Project >Add class

    Visual C# items >code >class

    Name the class PopUpBox (you can rename it later if you like) and paste in the following code:

    using System.Drawing;
    using System.Windows.Forms;
    
    namespace yourNameSpaceHere
    {
        public class PopUpBox
        {
            private static Form prompt { get; set; }
    
            public static string GetUserInput(string instructions, string caption)
            {
                string sUserInput = "";
                prompt = new Form() //create a new form at run time
                {
                    Width = 500, Height = 150, FormBorderStyle = FormBorderStyle.FixedDialog, Text = caption,
                    StartPosition = FormStartPosition.CenterScreen, TopMost = true
                };
                //create a label for the form which will have instructions for user input
                Label lblTitle = new Label() { Left = 50, Top = 20, Text = instructions, Dock = DockStyle.Top, TextAlign = ContentAlignment.TopCenter };
                TextBox txtTextInput = new TextBox() { Left = 50, Top = 50, Width = 400 };
    
                ////////////////////////////OK button
                Button btnOK = new Button() { Text = "OK", Left = 250, Width = 100, Top = 70, DialogResult = DialogResult.OK };
                btnOK.Click += (sender, e) => 
                {
                    sUserInput = txtTextInput.Text;
                    prompt.Close();
                };
                prompt.Controls.Add(txtTextInput);
                prompt.Controls.Add(btnOK);
                prompt.Controls.Add(lblTitle);
                prompt.AcceptButton = btnOK;
                ///////////////////////////////////////
    
                //////////////////////////Cancel button
                Button btnCancel = new Button() { Text = "Cancel", Left = 350, Width = 100, Top = 70, DialogResult = DialogResult.Cancel };
                btnCancel.Click += (sender, e) => 
                {
                    sUserInput = "cancel";
                    prompt.Close();
                };
                prompt.Controls.Add(btnCancel);
                prompt.CancelButton = btnCancel;
                ///////////////////////////////////////
    
                prompt.ShowDialog();
                return sUserInput;
            }
    
            public void Dispose()
            {prompt.Dispose();}
        }
    }
    

    You will need to change the namespace to whatever you're using. The method returns a string, so here's an example of how to implement it in your calling method:

    bool boolTryAgain = false;
    
    do
    {
        string sTextFromUser = PopUpBox.GetUserInput("Enter your text below:", "Dialog box title");
        if (sTextFromUser == "")
        {
            DialogResult dialogResult = MessageBox.Show("You did not enter anything. Try again?", "Error", MessageBoxButtons.YesNo);
            if (dialogResult == DialogResult.Yes)
            {
                boolTryAgain = true; //will reopen the dialog for user to input text again
            }
            else if (dialogResult == DialogResult.No)
            {
                //exit/cancel
                MessageBox.Show("operation cancelled");
                boolTryAgain = false;
            }//end if
        }
        else
        {
            if (sTextFromUser == "cancel")
            {
                MessageBox.Show("operation cancelled");
            }
            else
            {
                MessageBox.Show("Here is the text you entered: '" + sTextFromUser + "'");
                //do something here with the user input
            }
    
        }
    } while (boolTryAgain == true);
    

    This method checks the returned string for a text value, empty string, or "cancel" (the getUserInput method returns "cancel" if the cancel button is clicked) and acts accordingly. If the user didn't enter anything and clicked OK it will tell the user and ask them if they want to cancel or re-enter their text.

    Post notes: In my own implementation I found that all of the other answers were missing 1 or more of the following:

    • A cancel button
    • The ability to contain symbols in the string sent to the method
    • How to access the method and handle the returned value.

    Thus, I have posted my own solution. I hope someone finds it useful. Credit to Bas and Gideon + commenters for your contributions, you helped me to come up with a workable solution!

提交回复
热议问题