c# - how does child and parent comuncate
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)
{
开发者_JS百科 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 ?...
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();
}
}
}
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?
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.
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.
精彩评论