无法从form2将menue strip成员变为true [英] Can not turn menue strip members to true from form2

查看:80
本文介绍了无法从form2将menue strip成员变为true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hellow

在Myproject中,我有2个表单(Form1和Form2)

i想要在Form1或者在Form1中禁用或启用menuestrip的一个成员(formatToolStripMenuItem) Form2

我得到的错误是非静态字段,方法或属性需要对象引用

Myproject.form1.formatToolStripMenuItem



假设Form1有以下3种方法

Hellow
in Myproject ,i have 2 forms (Form1 and Form2)
i want to disable or enable one member of the menuestrip (formatToolStripMenuItem) either in Form1 or in Form2
the error i have got is "an object refrence is required for the non-static field ,method,or property
Myproject.form1.formatToolStripMenuItem"

assume Form1 has the following 3 methods

    private void Form1_Load(object sender, EventArgs e)
    {
     //some codes then
     TurnOff_RestOfToolStripItems();
}

private void TurnOff_RestOfToolStripItems()
    {
        formatToolStripMenuItem.Enabled = false;
    }

public static void TurnOn_RestOfToolStripItems_22()//<-------Error
{
        formatToolStripMenuItem.Enabled = true;
}


Form2中的
我写了这样的方法


in Form2 i wrote method like this

private void SomeCalculation()
{
//few codes then
Form1.TurnOn_RestOfToolStripItems_22();// i could not turn menue strip to true
}



i感谢您的帮助


i appreciate your kind help

推荐答案

我发布了第二个(更详细的)解决方案,因为对帖子的回复不支持格式化文本。

I'm posting a 2nd (more detailed) solution since replies to the thread doesn't support formatted text.


  1. 公开一个 Form1 方法,允许启用/禁用其工具条项目。


  1. Expose a Form1 method that allows its toolstrip items to be enabled/disabled.
public void EnableAdditionalToolstripItems
    (bool enable)
{
    if (enable)
        this.TurnOn_RestOfToolStripItems_22();
    else
        this.TurnOf_RestOfToolStripItems_22();
}



  • Form2 中,定义参考到 Form1


  • In Form2, define a reference to Form1.

    public Form2 : Form
    {
        ...
    
        public Form1 ReferenceToForm1 {
            get;
            set;
        }
    }
    



  • Form1 即将显示 Form2 ,将其传递给 Form1


  • When Form1 is about to display Form2, pass it a reference to Form1.

    // Construct and display Form2 from Form1
    Form2 f2 = new Form2();
    f2.ReferenceToForm1 = this;
    f2.ShowDialog(this);
    



  • Form2 现在可以启用/禁用其父 Form1 的工具条件通过调用:


  • Form2 can now enable/disable its parent Form1's toolstrip items by calling:

    this.ReferenceToForm1.EnableAdditionalToolstripItems (true/false);
    



  • / ravi




    1. TurnOn_RestOfToolStripItems_22()应该是一个实例方法,而不是 static
    2. Form2 需要一个 Form1 实例的引用(例如 _form1 ) 。
    3. 鉴于该引用,SomeCalculation()应该被重写为:


    1. TurnOn_RestOfToolStripItems_22() should be an instance method, not static.
    2. Form2 needs a reference (e.g. _form1) to a Form1 instance.
    3. Given that reference, SomeCalculation() should be rewritten as:
    private void SomeCalculation()
    {
      //few codes then
      this._form1.TurnOn_RestOfToolStripItems_22();
    }
    



    就是说,表格不应直接修改另一个表格的用户界面。 举起一个事件以表明具体情况会更好;然后,其他对象可以对该事件作出反应,并以任何合适的方式修改自己。 这种关注点分离允许松散耦合(因此更容易维护)类。



    / ravi

    That being said, a Form shouldn't directly modify the UI of another Form.  It's better to raise an event to indicate a specific situation; other objects can then react to that event and modify themselves in any way the deem fit.  This "separation of concerns" allows for loose coupling (and therefore more easily maintained) classes.

    /ravi


    这篇关于无法从form2将menue strip成员变为true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆