Hello,
I try to make a class (called voltage) to use in a Usercontrol (called switch). I like to know when one of the propertys of the type voltage change by means of an event triggerd in the voltage class, and then trigger a event in the class switch to notify the user. (see also my last post)
here is the code for the voltage class:

Events problems.
Andreas Kranister
Sankar N
Guy F
FYI, I've found that first pasteing the copied code fragment into Word will "lock" the proper formatting. Then copy/paste into the forum box from Word and the code will retain it's proper format.
Rhubarb
Malleswar
Well, let's give my own answer.
I think that in the example above, I've changed the instance of the MyFirstBubble object Bubble to a not instantiated value of Bub when I do this:
public MyFirstBubble Bub
{
get { return Bubble; }
set { Bubble = value; }
}
When I change the set part of the property to:
set { Bubble.A = value.A;}
Things seem to work as I intended.
Regards
Rob Warning
stswordman
It then subscribes to the VoltageChanged event of those Voltage objects.
However, you did not post the code for the Voltage class, so I cannot see where the VoltageChanged event is fired.
Also, your 'myVoltageChanged' event is not subscribed to anywhere in that code, so it will always be null. (Unless code you didn't post actually subscribes to it.)
Waqas Nasir
Ok Matthew, here is my voltage class:
The myVoltageChanged is supposed to be an event of the switch component. it is listed in the propertys grid of the switch component. like the stateChanged event.(witch is working fine).
Hope you can shine some light on my problem. for me it's still a mistery.
regards
Sam Hobbs
Yes I try'd to format it a bit by hand. Maybe something for the MSDN guys to make it possible to copy formatted code directly from the C# IDE.
It looks to me that the code "a.VoltageChanged += new VoltageEventHandler(This_VoltageChanged);" Dus not add a VoltageEventHandler to the VoltageChanged event in the Voltage class. because during runtime it stays null altrough the OnVoltageChanged Methode is called. I will try to make a barebone class with just a eventhandler in it and try too hook it up to an empty usercontrol. maybe that way I find out what went wrong. but if in the mean time you find it out. . .
Thanks Regards
Rob
pkarun
OK I've had some time for some experiments. and I think what I found is realy interesting.
The problem is not in the fact that the event are not hooked up correctly but in the fact that I create some voltage objects and later on try to access them as Public properties.
Look at the more simple but equivalent code below. I've made an (nonsense) class myFirstBubble, and used that in a empty usercontrol object, I called BubblerControl. First I see the same behavior as before. Then afterwards I commented out some lines and it worked!
The only thing is I do not completely understand why. and also I do not Know how to make the object in my control accessible now.
public class MyFirstBubble
{
private int a;
public event EventHandler AChanged;
public int A
{
get { return a; }
set
{
if (a != value)
{
a = value;
MessageBox.Show("Inner BubbleEvent A=" + a.ToString());
OnAChanged(EventArgs.Empty);
}
}
}
private void OnAChanged(EventArgs e)
{
if (AChanged != null) //AChanged remains null.
{
AChanged(this, e);
}
}
}
public partial class BubblerControl : UserControl
{
private MyFirstBubble Bubble = new MyFirstBubble();
public event EventHandler BubbleChanged;
public BubblerControl()
{
InitializeComponent();
Bubble.AChanged += new EventHandler(BubbleOn);
}
public MyFirstBubble Bub //This Code makes it fail!!!
{
get { return Bubble; }
set { Bubble = value; }
}
private void BubbleOn(Object sender, EventArgs e)
{
OnBubbleChanged(EventArgs.Empty);
}
private void OnBubbleChanged(EventArgs e)
{
if (BubbleChanged != null)
{
BubbleChanged(this, e);
}
}
private void BubblerControl_Click(object sender, EventArgs e) //Change from inside.
{
Bubble.A += 1;
}
}
With the code below it works.
public partial class BubblerControl : UserControl
{
private MyFirstBubble Bubble = new MyFirstBubble();
public event EventHandler BubbleChanged;
public BubblerControl()
{
InitializeComponent();
Bubble.AChanged += new EventHandler(BubbleOn);
}
//public MyFirstBubble Bub //Now it works!!
//{ // My Eventhandler gets hooked up on to the myFirstBubble object.
// get { return Bubble; }
// set { Bubble = value; }
//}
private void BubbleOn(Object sender, EventArgs e)
{
OnBubbleChanged(EventArgs.Empty);
}
private void OnBubbleChanged(EventArgs e)
{
if (BubbleChanged != null)
{
BubbleChanged(this, e);
}
}
private void BubblerControl_Click(object sender, EventArgs e)
{
Bubble.A += 1;
}
}
Hope sombody can tell me how I should do this the correct way.
regards
Rob Warning
A. Brosten
Yes I did copy paste. I am used to make the local vatiables in lower case, and the atribule names that relates to these varialbles in uppercase. But I see what you mean! but if I change them in lowercase, It still will not work. To be a little more complete I will past in the complete code of my switch-component. I have the impression that I do something wrong with hooking up the eventhandler to the voltage component. because still the VoltageChanged Stays null in the voltage class when I put a breakpoint there.
regards Rob