且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从父窗体获取值

更新时间:2023-12-06 12:54:40

我建议你在 EmployeeTransferForm 类(你想要获取属性值的表单)中定义一些事件并实现它们在 Humanresources(您要访问其属性的表单)中.我不建议在面向对象架构中传递整个 Form 对象.

I would suggest you to define some events in EmployeeTransferForm class (form where you want to get the properties' values) and implement them in Humanresources (form that you want to access the properties of). I would not recommend passing the whole Form object in object oriented architecture.

所以,EmployeeTransferForm 的代码可以是这样的:

So, the code form the EmployeeTransferForm can look like this:

public class EmployeeTransferForm: Form
{
    public delegate Text GetTextHandler();

    GetTextHandler getSampleTextFromTextBox = null;
    public event GetTextHandler GetSampleTextFromTextBox
    {
        add { getSampleTextFromTextBox += value; }
        remove { getSampleTextFromTextBox -= value; }
    }

    //the rest of the code in the class
    //...
}

对于Humanresources,就像这样:

class Humanresources : Form
{
    private void button1_Click(object sender, EventArgs e)
    {
        Administraror.Humanresource.EmployeeTransferForm emptranfrm = new Administraror.Humanresource.EmployeeTransferForm();
        emptranfrm.GetSampleTextFromTextBox += new EmployeeTransferForm.GetTextHandler(GetSampleTextFromTextBox_EventHandler);
        emptranfrm.ShowDialog();
    }


    Text GetSampleTextFromTextBox_EventHandler()
    {
        return myTextBox.Text;
    }

    //the rest of the code in the class
    //...
}