I am trying 开发者_Python百科to see a reason to use a delegate, and from what I know, a delegate basically what's for an event.
The event I would have would be in Windows Forms. I have a panel that starts not visible, but when it becomes visible everything in the background becomes disabled (a popup).
I could have it so that whatever makes the panel visible will also cause everything to become disabled, as I usually do. But could I set up a delegate or an event, so whenever that panel is visible, it calls a method that disables everything?
I just can't figure how to work that out with a delegate.
How about:
panel.VisibleChanged += (sender, args) =>
{
if (panel.Visible) // Just become visible
{
// Disable everything else
}
};
System.Windows.Forms.Control
s (including Panels) have a VisibleChanged event you can bind to. So
myPanel.VisibleChanged += OnMyPanelVisibleChanged;
Or inline:
myPanel.VisibleChanged += (sender, e) => this.Enabled = myPanel.Visible;
精彩评论