Home > OS >  Method doesnt bind to event handler in C#
Method doesnt bind to event handler in C#

Time:12-31

Im trying to use MVC pattern in winforms but when I try invoke event it is always null. I pass GameView form to Game class to which I bind CityButtonPressed method to event but when I run it it seems like if nothing was binded.

public partial class GameView : Form,IGameView
{
    public event EventHandler CityButtonPressed;

    public GameView()
    {
        InitializeComponent();
    }


    private void button3_Click(object sender, EventArgs e)
    {

    }

    protected virtual void OnButtonPressed(EventArgs e)
    {
        if (CityButtonPressed == null)   <----its always null
            MessageBox.Show("null");  
        CityButtonPressed?.Invoke(this, e);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        OnButtonPressed(EventArgs.Empty);
    }
}

even though i bind it here

class Game
{
    private readonly IGameView _gameView;
    private CityMenuView _cityMenuView = new CityMenuView();
    private List<Mesto> citiesList = new List<Mesto>();


    public Game(IGameView view)
    {
        _gameView = view;
        citiesList.Add(new Mesto("Bratislava"));
        citiesList.Add(new Mesto("Praha"));
        citiesList.Add(new Mesto("Brno"));

        _gameView.CityButtonPressed  = CityButtonPressed;

    }

    public void CityButtonPressed(object sender,EventArgs e)
    {
        _cityMenuView.MestoName = "Bratislava";
        _cityMenuView.ShowDialog();
    }
}

What am I doing wrong?

CodePudding user response:

Here's a cut down version of your code that shows it works.

void Main()
{
    var view = new GameView();
    var game = new Game(view);
    view.OnButtonPressed();
}

public interface IGameView
{
    event EventHandler CityButtonPressed;
}

class Game
{
    private readonly IGameView _gameView;


    public Game(IGameView view)
    {
        _gameView = view;
        _gameView.CityButtonPressed  = CityButtonPressed;
    }

    public void CityButtonPressed(object sender, EventArgs e)
    {
        Console.WriteLine("Pressed");
    }
}

public partial class GameView : IGameView
{
    public event EventHandler CityButtonPressed;

    public void OnButtonPressed()
    {
        if (CityButtonPressed == null) 
            Console.WriteLine("null");
        CityButtonPressed?.Invoke(this, new EventArgs());
    }
}

When I run that I get Pressed on the console. All I did is refactor your code to get it down to the minimal amount required for it to run.

It would seem you're not wiring it up correctly or your not showing us the actual minimal amount of code required.

CodePudding user response:

Make sure that the Game class is instantiated with the GameView instance before you invoke CityButtonPressed. For example:

private Game game;
    protected virtual void OnButtonPressed(EventArgs e)
    {
      game=  new Game(this);
        if (CityButtonPressed == null)   
            MessageBox.Show("null");  
        CityButtonPressed?.Invoke(this, e);
    }

It is not a good solution for you because you will need the Game instance somewhere but the CityButtonPressed should not be null anymore.

  • Related