I have a class, Widget, which is a userControl. Widget contains a custom class, CustomImageViewer.
CustomImageViewer publishes messages via a very simple event. Widget subscribes to this event and, when it is raised, throws it's own event to pass along the CustomImageViewer message, along with some of it's own info.
Here's the code from the Widget class which passes along the message:
void civImgView_NoteSent(string theMessage, cvMessaging.messageStatus theStatus)
{
this.NoteSent("Control " + ID.ToString() + ": " + theMessage, theStatus);
}
The widgets are added to a flow layout panel by a background worker thread. The problem I'm having is that, at random, the above line "this.NoteSent("Control " +..." throws an error stating "object reference not set to an instance of an object".
Changing the above to the following prevents the error:
void civImgView_NoteSent(string theMessage, cvMessaging.messageStatus theStatus)
{
if (this.NoteSent != null)
{
this.NoteSent("Control " + ID.ToString() + ": " + theMessage, theStatus);
}
}
I'm obviously only getting a few of the messages when I do that though...
"NoteSent" is an event defined in the widget class like this:
public partial class cvWidget : UserControl
{
private delegate void delStringOnly(string theString);
private delegate void delNoVar();
public event cvMessaging.delMessageDelegate NoteSent;
public int ID { get; set; }
So what am I missing? How can the event "NoteSent" not exist, if the place where it is raised is WITHIN the class where the error is raised? Does that make sense? How can the Widget class run the "civImgView_NoteSent" sub if it is not instantiated, and if the Widget is, how can the event not be?
Sorry if that's poorly phrased, but hopefully you get what I'm asking. Here's the code for the first part of the class:
public partial class cvWidget : UserControl
{
private delegate void delStringOnly(string theString);
private delegate void delNoVar();
public event cvMessaging.delMessageDelegate NoteSent;
public int ID { get; set; }
private CustomImageViewer civImgView;
public bool isSelected
{
get
{
if (this.civImgView.selectedState == CustomImageViewer.selectedStates.Selected)
{ return true; }
else { return false; }
}
}
private string _filePath;
public string filePath
{
get
{
return _filePath;
}
set
{
_filePath = value;
setFileLabel();
setWidgetImage();
}
}
public cvWidget()
{
InitializeComponent();
this.civImgView = new CleanViewLib.CustomImageViewer(this);
this.Controls.Add(civImgView);
this.civImgView.BackColor = System.Drawing.Color.Gray;
this.civImgView.Location = new System.Drawing.Point(3, 3);
this.civImgView.Name = "civImgView";
this.civImgView.Padding = new System.Windows.Forms.Padding(1);
this.civImgView.Size = new System.Drawing.Size(112, 82);
this.civImgView.TabIndex = 2;
this.civImgView.NoteSent += new cvMessaging.delMessageDelegate(civImgView_NoteSent);
}
void civImgView_NoteSent(string theMessage, cvMessaging.messageStatus theStatus)
{
if (this.NoteSent != null)
{
this.NoteSent("Control " + ID.ToString() + ": " + theMessage, theStatus);
}
}
I know the issue has something to do with the fact that the widgets are added as part of the background thread, but I still don't get how the event can not exist is the object itself that contains it does...
Thanks a bunch.
Rune