c# - 子和父如何命令

时间:2010-09-03 19:30:07

标签: c# winforms

让我说我有一个表格和他的孩子 我希望孩子在没有他们相识的情况下触发他的父亲 换句话说,我希望孩子是通用的

例如,假设我有一个表单按钮和一个richTextBox,我想用evey点击来改变richTextBox文本

我希望表单和按钮不相互了解我该怎么办? 我试试这个:

public partial class Form1 : Form
{

    delegate void myfatherDelgate();
    static int msgCounter = 0 ;

    public Form1()
    {
        InitializeComponent();
        button1 = new myButton();
        myfatherDelgate += myMethod();

    }
    public void myMethod()
    {
        switch (msgCounter)
        {
            case 1:
                {
                    richTextBox1.Text = "first click";
                    msgCounter++;
                }
            case 2:
                {
                    richTextBox1.Text = "second click";
                }
            defult: this.Close;
        }



    }


}

public class mybutton:Button     {         static int myint;

    protected override void OnClick(EventArgs e)
    {
        base.OnClick(e);
        this.Parent.Invoke(myfatherDelgate());
    }


}

最简单的方法是:

    private void button1_Click(object sender, EventArgs e)
    {
        switch (msgCounter)
        {
            case 1:
                {
                    richTextBox1.Text = "first click";
                    msgCounter++;
                }
            case 2:
                {
                    richTextBox1.Text = "second click";
                }
            defult: this.Close;
        }
    }

以父亲形式......

我认为我的所有概念都是废话,有人可以点亮我吗? 但是我在这里犯了错误并且这是圆形的,有人可以帮助我吗?...

4 个答案:

答案 0 :(得分:2)

首先,您在谈论表单上的子元素,而不是表单的实际子类。正确?

因此,您希望删除表单和按钮之间的依赖关系。这可以通过设计模式来解决。你可以让按钮实现一个说IButton的界面。然后在表单中使用Factory类创建按钮并返回IButton引用而不是MyButton。或者,使用依赖注入而不是工厂。

无论哪种方式,我都没有看到你正在做什么的优势,你能更详细地解释这个问题吗?

答案 1 :(得分:2)

所以也许您正在寻找的更多是接口实现:

public interface IMyInterface {
    void MyAction();
}

public partial class form1 : Form, IMyInterface {
    public void MyAction() {
        richTextBox1.Text = "first click";
        ...
    }
}

public class button1 : Button {
    protected override void OnClick(EventArgs e) {
        var parent = this.Parent as IMyInterface;
        if( parent != null ) {
            parent.MyAction();
        }
    }
}

答案 2 :(得分:0)

在您的示例中,该按钮知道父Form有一个名为myFartherDelegate的委托。为什么不在父窗体上处理Button.Click事件?

您的代码看起来像这样

  public partial class Form1 : Form
  {
    static int msgCounter = 1;
    public Form1()
    {
      InitializeComponent();
      button1.Click += new EventHandler(button1_Click);
    }

    void button1_Click(object sender, EventArgs e)
    {
      switch (msgCounter)
      {
        case 1:
          {
            richTextBox1.Text = "first click";
            msgCounter++;
          }
          break;
        case 2:
          {
            richTextBox1.Text = "second click";
          }
          break;
        default: this.Close(); break;
      }
    }    
  }

该按钮完全不知道父窗体,只是普通按钮。当然,父母会在按钮的Click事件中注册它。如果你需要一个比这更宽松的耦合,那么它可能有助于更详细地解释这个要求。

答案 3 :(得分:0)

如果你想让孩子对父亲做点什么,父亲必须至少知道孩子存在。他不必知道具体细节,但如果他应该对他的孩子作出反应,那么他需要一个孩子会调用的事件处理程序,回调等。

这可以通过称为依赖性倒置原则的基本原理来实现。依赖于外部类的类不应该依赖于具体的实现,而应该依赖于实现的抽象。父母不应该需要一个特定的孩子,只需要看起来像一个孩子,就像一个孩子一样。同样,孩子不应该知道他们的特定父母,但如果需要互动,孩子必须有一些方法告诉父母的事情,即使它不知道父母的倾听。

尝试包含IChild引用(按钮)和公共事件处理程序的父(您的表单)。然后,创建一个实现IChild的Child并且它可以引发一个事件。然后,您需要创建(或给定)Parent和Child的第三个类,给Parent the Child(作为IChild),并将Child的事件挂钩到Parent的事件处理程序。现在你有一个父母,他只知道它有类似孩子的东西,你有一个孩子,当有重要的事情发生时,他可以挥动旗帜,但不必知道谁在听。

相关问题