This seems so intuitive that I feel like I must be missing something obvious... Anyways, this is the first time I've tried to create events in a base class, so I don't know.
In the ListBox class I want to add a handler to the clicked event which can be found in the Control class (see the AddNewListBoxItem() method in ListBox). The handler should "select" the ListBoxItem, so that it will be drawn highlighted. Both the Listbox and the ListBoxItem inherit from Control class.
Although, the event fires in the base class (Control), the event is null, so the subscriber(ListBox) never gets the news.
Also, I apologize if my code is unprofessional or hard to read, I'm just a hobbyist; your recommendations and critiques regarding my practices are welcome also welcome.
I tried to post all of the code, but a dialog box warned me about your wrath, so the code can also be found here ->
GitHub - MayhemusMaximus/MapEditor: An XNA Solution to Map Editing[
^]
Control Class:
public delegate void Clicked(object sender, EventArgs e);
public event Clicked clicked;
public virtual void OnClicked(EventArgs e)
{
if (clicked != null)
clicked(this.child, e);
}
private void handleMouseButtonEvents()
{
if (mouseIsOver)
{
if(Statics.PreviousMouseState.LeftButton == ButtonState.Pressed
&& Statics.CurrentMouseState.LeftButton == ButtonState.Released)
OnClicked(EventArgs.Empty);
}
}
ListBox Class:
namespace MapEditor.Controls
{
public class ListBox : Control
{
...
public void AddNewListBoxItem(Texture2D texture, string text, Color imageColor)
{
...
ListBoxItem listBoxItem = new ListBoxItem(image, label);
listBoxItem.clicked += listBoxItem_clicked;
this.ListBoxItems.Add(listBoxItem);
}
void listBoxItem_clicked(object sender, EventArgs e)
{
foreach(ListBoxItem listBoxItem in ListBoxItems)
{
if (listBoxItem.IsSelected)
listBoxItem.IsSelected = false;
}
((ListBoxItem)sender).IsSelected = true;
}
}
}
What I have tried:
I have rewritten this from scratch a couple of times, moved the subscription around within the method, searching Google, and asking I.T. staff at my work.