问题描述
在我的应用程序中,我有一个主窗体,它是所有其他窗体的 MdiParent.登录时,默认情况下会打开此表单,我们将从该表单的菜单条导航到其他表单,如下所示:
In my application, I have a mainform which is the MdiParent of all other forms. On login, by default this form is opened and from menustrip in this form we are navigating to other forms like this:
public partial class MainMenuForm : Form
{
public MainMenuForm()
{
InitializeComponent();
this.WindowState = FormWindowState.Maximized;
}
private void humanResourceToolStripMenuItem_Click(object sender, EventArgs e)
{
HumanResourceForm humanresourceform = new HumanResourceForm();
humanresourceform.MdiParent = this;
humanresourceform.Show();
}
}
在 HumanResourceForm
里面,我有一个按钮可以导航到另一个表单,比如 EmployeeTransferForm
:
Inside the HumanResourceForm
I have a button which will navigate to another form say EmployeeTransferForm
:
private void button1_Click(object sender, EventArgs e)
{
Administraror.Humanresource.EmployeeTransferForm emptranfrm = new Administraror.Humanresource.EmployeeTransferForm();
emptranfrm.ShowDialog();
}
现在我的问题是在 EmployeeTransferForm
中我想从 HumanResourceForm
的控件中获取一些值.此外,当 EmployeeTransferForm
打开或处于活动状态时,不应允许用户关闭 HumanResourceForm.
Now my problem is inside the EmployeeTransferForm
I want to get some values from the controls of HumanResourceForm
. Also the user should not be allowed to close the HumanResourceForm when the EmployeeTransferForm
is open or active.
我还想获取 EmployeeTransferForm
中 HumanResourceForm
的 TextBox 的 Text 属性,例如:
I also want to get the Text property of a TextBox of HumanResourceForm
in EmployeeTransferForm
like:
public partial class EmpLoctnChangeForm : Form
{
public EmpLoctnChangeForm( )
{
InitializeComponent();
}
private void EmpLoctnChangeForm_Load(object sender, EventArgs e)
{
intemppk = humanresourceform.txtpk.text;
}
}
期待大家的一些好的建议.
提前致谢.
Expecting some nice advice from all.
Thanks in advance.
推荐答案
我建议你在 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
//...
}
这篇关于从父窗体获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!