在 MDI Parent 表单中,我假设您有 toolStripStatusLabel1。如果没有,可以通过单击 menuStrip 控件中的黑色小箭头来添加它。
选项 1
在您的 MDI Parent(假设 frmMain 是 MDI Parent 表单)表单中,您有 StatusStrip,转到 frmMain.Designer.cs 文件并找到该位置
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;
做这个,
public System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;
然后,您可以从您的子页面访问如下。
ToolStripStatusLabel statusStrip=((frmMain)(frmMdiChild.MdiParent)).toolStripStatusLabel1
选项 2
声明一个公共属性,它将返回 toolStripStatusLabel1 控件或方法,您可以在 MDI 父窗体中设置 toolStripStatusLabel1 的文本属性。如果您返回 menuStrip1 本身,则您可以访问该控件的所有属性。如果您声明一个方法,它将设置 toolStripStatusLabel1 的文本属性,那么您只能设置文本。根据您的要求决定您想要什么。
返回 menuStrip1 控件的实现。
public ToolStripStatusLabel GetStatusBar
{
get
{
return this.toolStripStatusLabel1;
}
}
然后从您的子页面中,您可以使用,
ToolStripStatusLabel statusStrip=((frmMain)(frmMdiChild.MdiParent)).GetStatusBar;
选项 3
为了让它更漂亮一点,你可以在一个公共类中声明一个方法。然后您可以在其他子表单中重用它。
public void ShowStatusbarMessage(Form frmMdiChild, string message, NotifierType notificationType)
{
ToolStripStatusLabel statusStrip=((frmMain)(frmMdiChild.MdiParent)).GetStatusBar;
statusStrip.Text = message;
if (notificationType == NotifierType.SuccessInfo)
{
statusStrip.ForeColor = System.Drawing.Color.Green;
}
else if (notificationType == NotifierType.Warning)
{
statusStrip.ForeColor = System.Drawing.Color.Orange;
}
else
{
statusStrip.ForeColor = System.Drawing.Color.Red;
}
}
这里,NotifierType 是一个枚举