简体   繁体   English

从子c#在MDI父状态栏中显示消息

[英]Display Message in MDI Parent Status Bar from Child c#

This is simplest possible thing, but I cant update text on status bar. 这是最简单的事情,但是我无法更新状态栏上的文本。 I just started working in c# but cannot find solution. 我刚刚开始使用C#工作,但是找不到解决方案。 I tried below code: 我尝试下面的代码:

Mdiparent 中间的

public void StutasText(string text)
{
    toolStripStatusLabel.Text = text;
}

Child form 子表格

 MDIParent1 obj = new MDIParent1();
 obj.StutasText("Hello world");
 obj.Refresh();

Its not showing status text in the status bar. 它不会在状态栏中显示状态文本。 Where did I go wrong? 我哪里做错了?

((mdiMain)MdiParent).toolStripStatusLabel.Text = "My Text";
//but you must change the modifier property of toolStripStatusLabel to public etc

You're creating a new instance of MDIParent1, not using the instance that is shown/the instance your child form belongs to. 您正在创建MDIParent1的新实例,而不使用显示的实例/您的子表单所属的实例。

You could try using 您可以尝试使用

this.MdiParent 

instead of 代替

new MDIParent1()

In the MDI Parent form, I assume you have toolStripStatusLabel1. 在MDI父窗体中,我假设您具有toolStripStatusLabel1。 If you dont have, you can add this by clicking on the little black arrow in the menuStrip control. 如果没有,可以通过单击menuStrip控件中的黑色小箭头来添加它。

Option 1 选项1

In your MDI Parent (let's assume, frmMain is the MDI Parent form) form where you have the StatusStrip, goto frmMain.Designer.cs file and find the place 在您的MDI父级(假设,frmMain是MDI父级表单)窗体中,您拥有StatusStrip,转到frmMain.Designer.cs文件并找到该位置

private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;

make this, 做这个

public System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;

Then from your child pages you can access like below. 然后,您可以从您的子页面访问,如下所示。

ToolStripStatusLabel statusStrip=((frmMain)(frmMdiChild.MdiParent)).toolStripStatusLabel1

Option 2 选项2

Declare a public property which will return the toolStripStatusLabel1 control or method where you can set the text property of the toolStripStatusLabel1 in the MDI Parent form. 声明一个公共属性,该属性将返回toolStripStatusLabel1控件或方法,您可以在其中设置MDI父窗体中toolStripStatusLabel1的text属性。 If you return the menuStrip1 itself then you have access to all the properties of that control. 如果返回menuStrip1本身,则可以访问该控件的所有属性。 If you declare a method, which will set the text property of the toolStripStatusLabel1 then you can only set the text. 如果声明一个方法,该方法将设置toolStripStatusLabel1的text属性,则只能设置文本。 Decide on what you want, based upon your requirement. 根据您的要求决定所需的内容。

An implementation which returns the menuStrip1 control. 返回menuStrip1控件的实现。

 public ToolStripStatusLabel GetStatusBar
 {
  get
   {
     return this.toolStripStatusLabel1;
   }
 }

then from your child pages, you can use, 然后在您的子页面上,您可以使用

ToolStripStatusLabel statusStrip=((frmMain)(frmMdiChild.MdiParent)).GetStatusBar;

Option 3 选项3

To make it little bit more prettier, you can declare a method in a common class. 为了使其更美观,可以在通用类中声明一个方法。 Then you can reuse this in other child forms. 然后,您可以在其他子窗体中重用它。

 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;

       }

   }

Here, NotifierType is an enum 在这里,NotifierType是一个枚举

((frmMDI)this.MdiParent).yourcontrol.yourproperty=yourvalue; ((frmMDI)this.MdiParent).yourcontrol.yourproperty =您的值; frmMDI is unique name of MDI form. frmMDI是MDI表单的唯一名称。

First In "mdi parent name".Designer.cs, change the type or member private to public 首先在“ mdi父名称” .Designer.cs中,将类型或成员private更改为public

Second In your code add the next code (("mdi parent name")MdiParent).toolStripStatusLabel.Text = "your text"; 第二个在您的代码中添加下一个代码(("mdi parent name")MdiParent).toolStripStatusLabel.Text = "your text";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM