Prompt Dialog in Windows Forms

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

提交回复
热议问题