如何制作自己的输入对话框

本文关键字:对话框 输入 自己的 何制作 | 更新日期: 2023-09-27 18:30:06

所以问题是,我想制作一个输入对话框,当点击主框架中的按钮时,它会触发输入对话框。我们可以在对话框中键入单词,然后单击输入对话框上的"添加"按钮,这样主框架上的文本框就可以显示我刚刚输入的字符串。然而,我目前面临着这样做的困难。因为对话框和主框架是两个模块,这意味着当我点击主框架上的按钮触发输入对话框时,它不能等到我输入字符串,按钮事件处理程序完成了它们的工作,所以它永远无法读取我的输入。不管怎样,这是我的代码:

 /* some code */
 // add first name and last name
 private void addButton_Click(object sender, EventArgs e)
 {
    Add add = new Add();   // declare a Add object
    add.Visible = true;    // show the Add frame
    if (add.isOK)
    {
        firstName = add.getFirstName();
        lastName = add.getLastName();
        textArea.Text = "First Name: " + firstName + "'r'n";    // textArea is a multiple line textbox
        textArea.Text += "Last Name: " + lastName;
    }
}
/* some code */

上面是主框架上的"添加"按钮,用于触发"添加"对象(输入对话框)。这是我的Add类:

namespace DatabaseToAPI
{
    public partial class Add : Form
    {
        private string firstName;
        private string lastName;
        public bool isOK = false;  // to check if name is provided
        public Add()
        {
            InitializeComponent();
            this.Visible = false;  // only become visible when the main form's Add button is clicked
        }
        public string getFirstName()
        {
            return firstName;  // return the first name
        }
        public string getLastName()
        {
            return lastName;   // return the last name
        }
        // Add button event handler
        private void addButton_Click(object sender, EventArgs e)
        {
            if (firstNameBox.Text != "" && lastNameBox.Text != "")   // if name is provided
            {
                firstName = firstNameBox.Text;
                lastName = lastNameBox.Text;
                isOK = true;    // name has provided
                this.Close();
            }
            else
            {
                MessageBox.Show("Please provide both first name and last name", "Sorry", MessageBoxButtons.OK);
            }
        }
        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
 }

正如您所看到的,Add类基本上就像一个输入对话框,当在主窗体上单击Add按钮时,Add窗体就会出现。唯一的问题是,主框架上的输入对话框触发按钮的事件处理程序不能等到我输入名称并单击按钮来表示我完成了,它只是简单地遍历代码,而不需要等待Add类完成它们的工作。我如何才能让主表单知道我何时添加名称,以便他们可以阅读我输入的文本?

如何制作自己的输入对话框

以下是对话框的一般模式:

private void addButton_Click(object sender, EventArgs e)
{
   using(var add = new Add()) // so that it will be disposed after usage
   {
       if (add.ShowDialog(this) == DialogResult.OK)
       {
           firstName = add.getFirstName();
           lastName = add.getLastName();
           textArea.Text = "First Name: " + firstName + "'r'n";
           textArea.Text += "Last Name: " + lastName;
       }
   }
}

在对话框代码中,您应该删除isOK,并在相关按钮事件中使用DialogResult=DialogResult.OK/DialogResult.Cancel。

这就是在C# 中调用模式对话框的方式

Add dlg = new Add();
if(dlg.ShowDialog() == DialogResult.OK)
{
    lastName = dlg.LastName;
    firstName = dlg.FirstName;
}

为此,您需要在表单Add关闭时返回DialogResult.OK。所以在表格中你需要

public partial class Add : Form
{
    public Add()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        this.DialogResult=DialogResult.OK;
    }
    private void button2_Click(object sender, EventArgs e)
    {
        this.Close();
    }
    public string FirstName { get { return textBox1.Text; } }
    public string LastName { get { return textBox2.Text; } }
}

好的,感谢回答的人,我成功了。我们基本上将表单视为一个对话框。这是代码:

// add first name and last name
private void addButton_Click(object sender, EventArgs e)
{
    Add add = new Add();
    add.ShowDialog(this);
    try {
        if (add.DialogResult == DialogResult.OK)
        {
            firstName = add.getFirstName();
            lastName = add.getLastName();
            textArea.Text = "First Name: " + firstName + "'r'n";
            textArea.Text += "Last Name: " + lastName;
        }
    } 
    catch
    {
    }
}

这是Add类:

namespace DatabaseToAPI
{
    public partial class Add : Form
    {
        private string firstName;
        private string lastName;
        public Add()
        {
            InitializeComponent();
            this.Visible = false;
        }
        public string getFirstName()
        {
            return firstName;
        }
        public string getLastName()
        {
            return lastName;
        }
        private void addButton_Click(object sender, EventArgs e)
        {
             if (firstNameBox.Text != "" && lastNameBox.Text != "")
            {
                firstName = firstNameBox.Text;
                lastName = lastNameBox.Text;
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("Please provide both first name and last name", "Sorry", MessageBoxButtons.OK);
            }
        }
        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}