开发者

Basic GUI Event Handling Questions C#

Good Afternoon,

I have some very basic questions on GUI Event Handling. Firstly with C# how can we link events to objects - I am guessing event handlers? If so can each handler use separate code? - How can the event handler locate the objects it must manipulate?

I have a rough idea of how it works in JAVA. Pointing me towards a reference would be fine - I have already trawled Google for answers to no av开发者_运维问答ail.

Many Thanks, J


Firstly with C# how can we link events to objects - I am guessing event handlers? If so can each handler use separate code?

Yes, each event handler can have its own code:

class A {
    public event EventHandler SomeEvent;
}

class B {
    public B(A a) {
        a.SomeEvent += (sender, e) => { Console.WriteLine("B's handler"); };
    }
}

class C {
    public C(A a) {
        a.SomeEvent += (sender, e) => { Console.WriteLine("C's handler"); };
    }
}

How can the event handler locate the objects it must manipulate?

I'm going to oversimplify this a lot, but event handlers are essentially wrappers around the observer pattern. EventHandlers like any other Delegate type hold a list of subscribers in a method invocation list (see Delegate.GetInvocationList). You can think of it like this:

class EventHandler {
    LinkedList<Action<object, EventArgs>> subscribers =
        new LinkedList<Action<object, EventArgs>>();

    public void Add(Action<object, EventArgs> f) {
        subscribers.AddLast(f);
    }

    public void Remove(Action<object, EventArgs> f) {
        subscribers.Remove(f);
    }

    public void Invoke(object sender, EventArgs e) {
        foreach(Action<object, EventArgs> f in subscribers)
            f(sender, e);
    }
}

(The code above is pretty far removed from the actual implementation details of the real event handler class. Delegate types are immutable, so adding a handler returns a new Delegate with the handler added rather than mutating the handler in place. I believe their Add/Remove methods have a lot of threading voodoo in them as well.)

Since the delegate instance holds a reference to each of its subscribers, it has direct access to any object it manipulates.


Each event is in fact a delegate - you register the event handler with this delegate using this notation:

myButton.Click += new EventHandler(myEventHandler);

This way, when the button click event fires, any event handlers on its invocation list (in this case this will include the myEventHandler function.

For each button, you do the same and register the event handler you want to fire (can be the same one for multiple buttons).

Perhaps this MSDN topic will shed some light: Events and Delegates.


Using designer in Visual Studio you will have an easy way with GUI objects handlers. By clicking the 'lightning' button you will see all available events for a partticular control. By clicking twice on selected event the IDE will generate a method stub for you.

    private void button_Click(object sender, EventArgs e)
    {
        //sender is the object which actually raised the event
        ((Button)sender).Text = "Clicked Me";
    }

The event handler is added like this(done automatically in the *.Designer.cs file)

    button.Click += new System.EventHandler(this.button_Click);


That depends on which UI library you are using. In WPF, you define the event handler in XAML:

<Button Click="HelloWorldClickEventHandler">Hello World</Button>

// this is in your .cs code file
private void HelloWorldClickEventHandler(object sender, RoutedEventArgs e)
{
    // some code
}

In WinForms, you attach the handlers in the constructor of your form:

public MyForm() {
    // ...
    HelloWorldButton.Click += new EventHandler(HelloWorldClickEventHandler);
}

private void HelloWorldClickEventHandler(object sender, EventArgs e)
{
    // some code
}

Of course, using a IDE such as Visual Studio simplifies this process a lot.

How can the event handler locate the objects it must manipulate?

There are two ways to access the relevant objects:

  • Since the methods are instance methods of the window (WPF) or form (WinForms), they can access all the UI resources directly (i.e., you can use this.MyLabel in your code).

  • The first parameter, sender, is by convention the UI control that raised the event. This is useful if you use one event handler for multiple UI controls.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜