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

时间:2016-01-13 00:20:22

标签: c# winforms

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

 /* 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按钮,用于触发Add对象(输入对话框)。这是我的Add class:

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类完成他们的工作。我怎么能这样做,主表单知道我什么时候添加名称,以便他们可以读取我放的文本?

3 个答案:

答案 0 :(得分:2)

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

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。

答案 1 :(得分:1)

这就是你在C#

中调用模态对话框的方法
Add dlg = new Add();
if(dlg.ShowDialog() == DialogResult.OK)
{
    lastName = dlg.LastName;
    firstName = dlg.FirstName;
}

要执行此操作,您需要在表单DialogResult.OK关闭时返回Add。所以你需要的形式

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

答案 2 :(得分:1)

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

// 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 class:

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