1

I have an application with a panel control that uses dynamically generated buttons to start programs....an application launcher if you will. The buttons are created at run time and assigned a generic event handler that fires off a "Process.Start()" when clicked. it works fine:(App and _app are just ways to provide which buttons the user has available from a database. In this case ALL buttons are available so there are no tricks)

foreach (App _application in _app)
{
    _application.ApplicationSecurities = _appSecurities;
    _application.LoginInfo = _info;
    Button _but = new Button();
    _but.Font = new Font(FontFamily.GenericSerif, 21, FontStyle.Bold);
    _but.Height = FontHeight + 80;
    _but.Text = _application.ButtonText;
    _but.Tag = _application;
    _but.Click += Button_Click;

    _but.Dock = DockStyle.Top;




    string _workingDirectory = Path.Combine(@"C:\Blah\", _application.ExecutableName);

    string _appPath = Path.Combine(_workingDirectory, _application.LatestVersion);


    _but.ToolTip = _application.LatestVersion;
    _buttons.Add(_but);
    PnlControls.Controls.Add(_but);
}

The problem is, When The user clicks a button, I would like the buttons on the panel to disappear which I do with a

PnlControls.Controls.Clear();

They DO disappear, but if i click around on the panel (which is still up), the application that was represented by that buttons location still fires off a click event and launches when the application closes! I have tried various methods to remove the event handlers from that panel,then repopulate the buttons when the application closes. Heres the latest iteration of the "DisableAllButtons" function:

private void DisableAllButtons()
{
    foreach (Button _but in _buttons)
    {
        PnlControls.Controls.Remove(_but);
        _but.Click -= null;
        _but.Dispose();
    }
    _buttons.Clear();

}

Heres how the process start and above function are called (from within the click event)

private void LaunchApplication(App appToRun) { string _workingDirectory = Path.Combine(@"C:\Blah\", appToRun.ExecutableName);

    string _appPath = Path.Combine(_workingDirectory, appToRun.LatestVersion);

    try
    {
        string _start = Path.Combine(_appPath, appToRun.ExecutableName) + ".exe";

        Process _proc = new Process();
        _proc.StartInfo.FileName = _start;
        _proc.StartInfo.Arguments = appToRun.StartingArguments;
        DisableAllButtons();
        _proc.Start();

        _proc.WaitForExit();
        CreateButtons();
    }
    catch (Exception _e)
    {
        MessageWindow.Show(@"Failed to start application", _e.Message);
    }
}

Why are these buttons firing after they have been removed?

rigamonk
  • 1,179
  • 2
  • 17
  • 45
  • 1
    A couple of things: I haven't seen `_but.Click -= null` in practice like that. I think you need to `_but.Click -= Button_Click`. You could, just as a test (not in production), force the GC to run after disposing the buttons to see if that's the issue. – Cᴏʀʏ Aug 23 '18 at 17:45
  • Thanks for the input. I gave what you suggested a try. But no progress. – rigamonk Aug 23 '18 at 17:49
  • I'd like to see a full code listing. Try what @Cᴏʀʏ suggested in combination with changing the order of operation by removing the event handler then removing the button from the control (rather than the other way around). – Carlo Bos Aug 23 '18 at 18:14
  • If you're still having trouble, and believe the controls are not disposing due to lingering event handlers, you could try some options [over here](https://stackoverflow.com/questions/91778/how-to-remove-all-event-handlers-from-an-event?rq=1) to ensure that all event handlers are removed. – Cᴏʀʏ Aug 23 '18 at 19:12
  • Keep in mind PnlControls.Controls.Clear() will remove the controls from the list but will not call dispose on them nor free them from memory. Might be better to iterate through the list yourself and free each one. Also, since Button_Click is a generic handler, might you have it assigned to other controls besides the buttons in question? Like on the Form itself? – Jeff R. Aug 23 '18 at 20:18

0 Answers0