0

I have multiple custom controls, and I noticed that all of them share the same event (custom) example : OnMoved etc

What I'm doing now is, copy & paste the same code from controls to controls.

So, are there anyway for me to write custom events that can be shared throughout all my controls in C# WPF?

An example of an event that I use for all my controls :

    Point lastPosition = new Point();
    Point currentPosition = new Point();
    public static void OnMoved(object sender, EventArgs e)
    {
        currentPosition.X = Canvas.GetLeft(explorer);
        currentPosition.Y = Canvas.GetTop(explorer);

        // didn't moved
        if (currentPosition.X == lastPosition.X || currentPosition.Y == lastPosition.Y)
        {
            return;
        }

        lastPosition.X = Canvas.GetLeft(explorer);
        lastPosition.Y = Canvas.GetTop(explorer);
    }
Xeon
  • 246
  • 3
  • 15
  • 1
    You might want to look at [My Example](http://stackoverflow.com/a/15580293/643085) on how to properly make a UI where you can "drag" items around in WPF, by using proper commonly used accepted standard WPF practices such as `DataBinding` and `DataTemplate`s, as opposed to a procedural event-based approach. – Federico Berasategui Jul 11 '14 at 03:56
  • Oh, I have downloaded it, it's really cool. But unfortunately, I haven't learn MVVM yet so the code doesn't really make any sense to me :( – Xeon Jul 28 '14 at 02:49
  • if you have any doubts about it, feel free to post a question and let me know. – Federico Berasategui Jul 28 '14 at 03:17

3 Answers3

1

It depends on what exactly you need the event to do, but you could place the event into a shared class:

public class MyEvents
{
    public static void SomeEvent(object sender, EventArgs e)
    {
        MessageBox.Show("hi");
    }
}

And then just subscribe to it from wherever you need to:

SomeButton.Click += MyEvents.SomeEvent;
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
0

You can create a base class that has a public virtual event, and the event will appear in any classes that derive from the base class. That would keep you from having to copy and paste the same code over and over.

user2920518
  • 318
  • 1
  • 8
  • Well, my controls are already inherited from UserControl, and I don't think C# allows multi inheritance – Xeon Jul 11 '14 at 03:43
  • @Xeon so looks like you are trying to fix the issue caused by bad design, normally you should have a base class for all your controls, this base class of course should inherit the `UserControl`. – King King Jul 11 '14 at 04:07
  • There is a way I did this a long time ago, because I needed to do exactly what you are trying to accomplish. I'll have to pull up the project tomorrow at the office to see exactly how I made it work. – user2920518 Jul 11 '14 at 04:15
0

Yes you can! :D The only things you need to have present are:

-> Same Events (The Args of the event have to be exactly the same. -> They are going to do the same.

The bad thing is that you cannot mix controls with events. For example, you can create a .Click event for a button so it closes your application, but if you wish this to do the same when you press the key "F8" it won't work because the Event arguments are different ~

You can try using a method that makes the same stuff in all your events. Example:

private void _Close()
{
Process.GetCurrentProcess().Close();
}

And you can close with "F5" pressed in the form or with a button click or typyng in a textbox "Close".

button.Click += Button_Close;
private void Button_Close(Object o, RoutedEventArgs e)
{
_Close();
}

this.KeyDown += This_Close;

private void This_Close(Object o, KeyEventArgs e)
{
     if(e.KeyCode == Key.F5) _Close();
}

TextBox.TextChanged += Text_Close;
private void Text_Close(Object o, TextChangedEventArgs e)
{
if(TextBox.Text == "Close") _Close();
}
c_str
  • 369
  • 1
  • 4
  • 14