Какое событие вызывается при нажатии клавиш - C#
Формулировка задачи:
у меня такой вопрос как при помощи событий отследить нажатие кнопки ф1 и открыть форму 2
(это нужно сделать именно при помощи событий не прибегая к встроенным функциям)
Решение задачи: «Какое событие вызывается при нажатии клавиш»
textual
Листинг программы
namespace sobitia
{
class Program
{
public delegate void FireEventHandler(object senter, FireEventArgs args);
public class NewTown
{
private int build, BuildNumber;
private int day, days;
private Police polisman;
private Ambulance ambulanceman;
private FireDetect fireman;
public event FireEventHandler Fire;
private Random rnd = new Random();
private int m = 3, n = 1000;
public NewTown(int TSize, int Days)
{
BuildNumber = rnd.Next(TSize);days = Days;
polisman = new Police(this);
ambulanceman = new Ambulance(this);
fireman = new FireDetect(this);
polisman.On(); ambulanceman.On(); fireman.On();
}
protected virtual void OnFire(FireEventArgs e) { if (Fire != null)Fire(this, e); }
public void LifeOurTown()
{
for(day=1;day<=days;day++)
for (build = 1; build <= BuildNumber; build++)
{
if (rnd.Next(n) <= m)
{
FireEventArgs e = new FireEventArgs(build, day, true);
OnFire(e);
if (e.Permit) Console.WriteLine("Пожар потушен!");
else Console.WriteLine("Пожар продолжается!");
}
}
}
}
public abstract class Receiver
{
private NewTown town;
public Receiver(NewTown town) { this.town = town; }
public void On() {town.Fire+=new FireEventHandler(It_is_Fire); }
public void Off() { town.Fire -= new FireEventHandler(It_is_Fire); town = null; }
public abstract void It_is_Fire(object sender, FireEventArgs e);
}
public class Police : Receiver
{
public Police(NewTown town) : base(town) { }
public override void It_is_Fire(object sender, FireEventArgs e)
{
Console.WriteLine("Пожар в доме {0}.Day {1} "+" Милиция ищет виновных!",e.Build,e.Day);
e.Permit &= true;
}
}
public class FireDetect : Receiver
{
public FireDetect(NewTown town) : base(town) { }
public override void It_is_Fire(object sender, FireEventArgs e)
{
Console.WriteLine("Пожар в доме {0}.Day {1} " + " Пожарные тушат пожар!", e.Build, e.Day);
Random rnd = new Random(e.Build);
if (rnd.Next(10) > 5)
e.Permit &= false;
else
e.Permit &= true;
}
}
public class Ambulance : Receiver
{
public Ambulance(NewTown town) : base(town) { }
public override void It_is_Fire(object sender, FireEventArgs e)
{
Console.WriteLine("Пожар в доме {0}.Day {1} " + " Скорая добивает пострадавших!", e.Build, e.Day);
e.Permit &= true;
}
}
public class FireEventArgs : EventArgs
{
private int build;
private int day;
private bool permit;
public int Build { get { return (build); } }
public int Day { get { return (day); } }
public bool Permit { get { return (permit); } set { permit = value; } }
public FireEventArgs(int build, int day, bool permit)
{
this.build = build; this.day = day; this.permit = permit;
}
}
static void Main(string[] args)
{
Console.WriteLine("Program Life Town");
NewTown smtown = new NewTown(100,100);
smtown.LifeOurTown();
Console.ReadLine();
}
}
}