事件demo

时间:2024-01-15 22:13:20
 delegate void MoveEventHandle(object source,MoveEventArgs e);
public class MoveEventArgs:EventArgs
{
public int newPosition;
public bool cancel;
public MoveEventArgs(int newposition)
{
this.newPosition=newposition;
}
}
class Slider
{
int position;
public event MoveEventHandle Move;
public int Position
{
get
{
return position;
}
set
{
if(position!=value)
{
if(Move!=null)
{
MoveEventArgs args=new MoveEventArgs(value);
Move(this,args);
if(args.cancel)
return;
}
position=value;
}
}
}
}
class Form
{
static void Main()
{
Slider slider=new Slider();
slider.Move+=new MoveEventHandle(slider_Move);
slider.Position=;
slider.Position=;
}
static void slider_Move(object source,MoveEventArgs e)
{
if(e.newPosition<)
console.writeline("OK");
else
{
e.Cancle=true;
console.writeline("Can't go that higjh");
}
}
}