我正在使用容器(mdi parent)打开主菜单.主菜单允许用户连接到数据库并打开其他程序.我正在尝试在容器(父窗体)上显示您连接的数据库,但是我在将字符串从主菜单传递到容器时遇到问题.当用户单击连接按钮时,我不知何故需要容器有一个事件监听器来监听子窗体中的按钮单击.在子窗体上单击连接按钮时,它会将变量传递给父窗体.我该怎么做呢?
也许你可以使用一个活动.因此,每次在子表单上更改数据库名称时,您都可以在父表单上进行回调
儿童
public partial class Child : Form { public event DatabaseChangeHandler DatabaseChanged; public delegate void DatabaseChangeHandler(string newDatabaseName); public Child() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //When the database changes if (this.DatabaseChanged != null) { this.DatabaseChanged("The New Name"); } } }
亲
public partial class Parent : Form { private Child childForm; public Parent() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // Open the child form childForm = new Child(); childForm.DatabaseChanged += childForm_DatabaseChanged; childForm.ShowDialog(); } void childForm_DatabaseChanged(string newDatabaseName) { // This will get called everytime you call "DatabaseChanged" on child label1.Text = newDatabaseName; } }