1

I know it has been asked before but after about an hour of searching I am unable to figure out the simplest and easiest way to add a parameter to an event handler. By default, the template for these handlers can only accept (object sender, RoutedEventArgs e) arguments. I find it hard to believe that there isn't a clean and easy way to do this because I imagine this problem occurs quite frequently. However I am new to WPF so if someone could provide some guidance on this issue my code is below.

When clicking on this button

<Button Height="23" VerticalAlignment="Bottom" Margin="150, 0, 0, 2" Content="Terminate All Processes" Width="135" HorizontalAlignment="Left" Click="TerminateAll_Click" Name="TerminateAll"/>

I need an event to fire off that closes all my processes. To do this though, i need to pass the list of all the processes to the event handler and I have yet to discover an easy way of doing this. Thank you for any help you can provide.

Edit: This is my .cs file

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        ObservableCollection<Proc> procs = new ObservableCollection<Proc>();

        Processes.getProcs(ref procs);
        lview.ItemsSource = procs;
    }

    private void TerminateAllProcesses(ObservableCollection<Proc> procs)
    {
        foreach (Proc p in procs)
        {
            if (!p.Pro.HasExited) { p.Pro.Kill(); }
        }
    }

    public void TerminateAll_Click(object sender, RoutedEventArgs e)
    {

    }
}
3
  • Where does that list come from? How does the button know about the list in order to pass it to the Click event? Commented Jul 28, 2015 at 15:31
  • Why do you want to send any additional parameter? In the .cs file you can acess to any object you want Commented Jul 28, 2015 at 15:35
  • The list i need to pass to the button click event is an observable collection created in MainWindow() if that answers your question. I could pass it to the event handler if it would let me change the default parameter list. Commented Jul 28, 2015 at 15:36

1 Answer 1

4

I need to pass the list of all the processes to the event handler

Why? The button fires the event, so it has to have a known parameter list. Plus, it has no knowledge of the list of processes, so it wouldn't know what to pass in anyway. However, there's nothing from stopping you from firing off another method from the click event:

private void TerminateAll_Click(object sender, RoutedEventArgs e) 
{
    List<string> processes = // get the list
    TerminateAll(processes);
}

public void TerminateAll(List<string> processes)
{
   foreach(string process in processes)
     Terminate(process);
}
private void Terminate(string process)
{
  // terminate the process
}
Sign up to request clarification or add additional context in comments.

7 Comments

This is the structure i was playing with. I most likely worded my question in a confusing way, but my main issue is retrieving my already formed list inside of TerminateAll_Click. In other words, what is the best way to go about "getting the list" without having to loop through and reform it? Maybe i am just completely missing something, but the idea was to pass the list to the event so that i already had processes to call TerminateAll with.
@mbrem Not sure what you mean by "loop through and reform it". If the list is already a member of the form class you should be able to access it directly from within the event handler.
FYI I used string just as an example. If your list holds references to Process objects or something else you could certainly use that.
My list is defined and created in my MainWindow. Thus it is out of the TerminateAll_Click event's scope so i don't know how it could be accessed directly. Again i am new to WPF so we may be thinking entirely different things.
@mbrem I would make the list a class member instead of a local variable to MainWindow. That way it can be accessed by other class members (including TerminateAll_Click). That is the standard way to share data across form members.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.