Observer Pattern/Delegates

B

bigsincebirth

HAPPY THANKSGIVING EVERYONE! I'd be really THANKFUL for any help.
:wink:

I'm a java programmer who has recently been assigned to a c# project.
I'm having trouble communicating between my forms. The scenario is I
have two forms each with one button. A click on the first button
instantiates the second form and Shows it. A click on the button in
the second class should send a message to the first class and change
its Size property. I'm attempting to use a delegate class and am
following examples on line but i keep receiving an error:

error CS0123: Method 'Rotater.Form1.rotateChanged()'
does not match delegate 'void Rotater.Form2.RotateHandler(object,
Rotater.RotateEventArgs)'


static void Main()
{
Application.Run(new Form1());
}

[b:d412ed1819]public void Subscribe(Form2 f2)
{

f2.OnRotateChange += new Form2.RotateHandler(rotateChanged);
}
public void button1_Click(object sender, System.EventArgs e)
{
f2 = new Form2();
this.Subscribe(f2);
}[/b:d412ed1819]
[b:d412ed1819]public void rotateChanged()
{
this.ClientSize = new System.Drawing.Size(600, 400);
}[/b:d412ed1819]using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Rotater
{
/// <summary>
/// Summary description for Form2.
/// </summary>
public class Form2 : System.Windows.Forms.Form
{

[b:d412ed1819]public delegate void RotateHandler(object sender,
RotateEventArgs e);
public event RotateHandler OnRotateChange;[/b:d412ed1819]
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form2()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(104, 128);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);

}
#endregion

[b:d412ed1819]private void button1_Click(object sender,
System.EventArgs e)
{
RotateEventArgs rea = new RotateEventArgs();
if(OnRotateChange != null)
{
OnRotateChange(this,rea);
}

}[/b:d412ed1819] }
}

using System;

namespace Rotater
{
/// <summary>
/// Summary description for RotateEventArgs.
/// </summary>
public class RotateEventArgs : EventArgs
{
private int screenHeight;
private int screenWidth;
private int imageHeight;
private int imageWidth;

public RotateEventArgs(){}
public RotateEventArgs(int sH, int sW, int iH, int iW)
{
this.screenHeight= sH;
this.screenWidth= sW;
this.imageHeight= iH;
this.imageWidth= iW;
//
// TODO: Add constructor logic here
//
}
}
}

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
G

Guest

hi
change your function in the form1 like this

public void rotateChanged(object o,RotateEventArgs e)
{ ...}
your subscriber function should have the signature of the delegate which u
used to define the event

Happy coding ...
and welcome to C#...

regards
Ansil
Dimensions
Technopark
TVM
(e-mail address removed)

bigsincebirth said:
HAPPY THANKSGIVING EVERYONE! I'd be really THANKFUL for any help.
:wink:

I'm a java programmer who has recently been assigned to a c# project.
I'm having trouble communicating between my forms. The scenario is I
have two forms each with one button. A click on the first button
instantiates the second form and Shows it. A click on the button in
the second class should send a message to the first class and change
its Size property. I'm attempting to use a delegate class and am
following examples on line but i keep receiving an error:

error CS0123: Method 'Rotater.Form1.rotateChanged()'
does not match delegate 'void Rotater.Form2.RotateHandler(object,
Rotater.RotateEventArgs)'


static void Main()
{
Application.Run(new Form1());
}

[b:d412ed1819]public void Subscribe(Form2 f2)
{

f2.OnRotateChange += new Form2.RotateHandler(rotateChanged);
}
public void button1_Click(object sender, System.EventArgs e)
{
f2 = new Form2();
this.Subscribe(f2);
}[/b:d412ed1819]
[b:d412ed1819]public void rotateChanged()
{
this.ClientSize = new System.Drawing.Size(600, 400);
}[/b:d412ed1819]using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Rotater
{
/// <summary>
/// Summary description for Form2.
/// </summary>
public class Form2 : System.Windows.Forms.Form
{

[b:d412ed1819]public delegate void RotateHandler(object sender,
RotateEventArgs e);
public event RotateHandler OnRotateChange;[/b:d412ed1819]
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form2()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(104, 128);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);

}
#endregion

[b:d412ed1819]private void button1_Click(object sender,
System.EventArgs e)
{
RotateEventArgs rea = new RotateEventArgs();
if(OnRotateChange != null)
{
OnRotateChange(this,rea);
}

}[/b:d412ed1819] }
}

using System;

namespace Rotater
{
/// <summary>
/// Summary description for RotateEventArgs.
/// </summary>
public class RotateEventArgs : EventArgs
{
private int screenHeight;
private int screenWidth;
private int imageHeight;
private int imageWidth;

public RotateEventArgs(){}
public RotateEventArgs(int sH, int sW, int iH, int iW)
{
this.screenHeight= sH;
this.screenWidth= sW;
this.imageHeight= iH;
this.imageWidth= iW;
//
// TODO: Add constructor logic here
//
}
}
}

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top