tags:

views:

96

answers:

4

let's say i have a form and his child and i want the child to trigger his father without them knowing each other in other words i want the child to be generic

for example let's say i have a form button and a richTextBox and i want with evey click to change the richTextBox text

i want form and button to not know each other how can i do it ? i tries this one :

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());
    }


}

the easiest way is to do :

    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;
        }
    }

in the father form ...

I think my all concept is crap can someone in light me ? but i mistake here and it's circular can someone help me here ?...

A: 

In your example, the button knows that the parent Form has a delegate called myFartherDelegate. Why not just handle the Button.Click event on the parent Form?

Your code would look something like this

  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;
      }
    }    
  }

The button is not at all aware of the Parent form, just a normal button. Of course the parent registers it's interest in the Click event of the button. If you need a looser coupling than this, then it might help to explain the requirement in more detail.

Chris Taylor
it seemes like a good solution but lets say i have 100 buttons is it the best way to implement it ?
yoav.str
@yoav.str, will all the buttons be doing the same thing when clicked? Because that is what I believe will happen in the solution proposed by @Zachary Yates. All the buttons on the parent form implementing IMyInterface will all call MyAction, so 100 buttons on the Form all doing the same thing or have I missed something? Presumably you want each button to have a unique action, so you would wire an approriate event handler to each button, of course you could always wire the same handler to every button and then check the 'sender' argument to decide on a specific action.
Chris Taylor
+2  A: 

Firstly your talking about child elements on a form, not actual sub classes of a form. Right?

So you want to remove the dependency between the form and the button. This could be solved with design patterns. You could have button implement an interface of say IButton. Then in form use either a Factory class to create the button and return an IButton reference instead of MyButton. Or, use dependency injection instead of a factory.

Either way i do not see an advantage to what you are doing, can you explain the problem in more detail?

JonWillis
can you show this in simple example ?what is this design pattern name ? can i use reflection in order to decide the class name in runtime(this is the main pherpes ...)?
yoav.str
Working on it now, nevermind Yates answer seems to have what your looking for.
JonWillis
+2  A: 

So perhaps what you are looking for is more of an interface implementation:

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();
        }
    }
}
Zachary Yates
incredible !!! the best solution ....
yoav.str
A: 

If you want a child to do something to his father, the father has to at least know that A child exists. He doesn;t have to know the specifics, but if he's supposed to react to his child then he needs an event handler, callback, etc that the child will invoke.

This can be accomplished with a basic principle called the Dependency Inversion Principle. Classes that depend upon external classes should not depend upon concrete implementations, but upon abstractions of the implementation. Parent should not need a specific Child, just something that looks like one and acts like one. Children, by the same token, should not have to know their specific Parent, but if interaction is required, the Child must have some way to tell the Parent things even if it doesn't know the Parent's listening.

Try a Parent (your form) that contains an IChild reference (the button) and a public event handler. Then, create a Child that implements IChild and has an event it can raise. Then, you need a third class that creates (or is given) the Parent and Child, gives the Parent the Child (as an IChild), and hooks the Child's event to the Parent's event handler. Now you have a parent who only knows it has something resembling a child, and you have a child who has a flag he can wave when something important happens, but doesn't have to know who's listening.

KeithS
o want to hear more about this pattern is there an example i can look in ?
yoav.str
This is probably best represented by the Factory pattern. The third class, call it a ParentFactory, would be asked for a "hydrated" Parent, and the Factory would know that Parent needed a Child in order to be useful. It would instantiate both, hook them up, and hand them out. For basic reading on the use of interfaces to abstract dependencies, look up loose coupling, Liskov Substitution Principle, and Dependency Inversion Principle.
KeithS