При нажатии комбинации кнопок cUocDo должен быть таким:
Текущее состояние: oD
Текущее состояние: cD
Текущее состояние: cU
Текущее состояние: oU
Текущее состояние: cU
Текущее состояние: cD
Текущее состояние: oD
Но уже при нажатии кнопок cU появляется такой результат:
Текущее состояние: oD
Текущее состояние: cD
Текущее состояние: oD
Недопустимый переход
Может, нужно учитывать кнопки, которые были нажаты, но как это сделать?
Код:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class MyForm : Form
{
enum StateType { oD, cD, cU, oU }
StateType State = StateType.oD;
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private void btnClick(Object sender, EventArgs ea)
{
Button bt = (Button)sender;
StateType State = StateType.oD;
try
{
Console.WriteLine("Текущее состояние: {0}", State);
switch (State)
{
case StateType.oD: switch (bt.Text)
{
case "c": State = StateType.cD; break;
default: throw new Exception();
}
break;
case StateType.cD: switch (bt.Text)
{
case "o": State = StateType.oD; break;
case "U": State = StateType.cU; break;
default: throw new Exception();
}
break;
case StateType.cU: switch (bt.Text)
{
case "D": State = StateType.cD; break;
case "o": State = StateType.oU; break;
default: throw new Exception();
}
break;
case StateType.oU: switch (bt.Text)
{
case "c": State = StateType.cU; break;
default: throw new Exception();
}
break;
}
Console.WriteLine("Текущее состояние: {0}", State);
}
catch (Exception)
{
Console.WriteLine("Недопустимый переход");
Console.ReadLine();
}
}
public MyForm()
{
ClientSize = new System.Drawing.Size(600, 400);
StartPosition = FormStartPosition.CenterScreen;
this.Text = "All Laucher";
button1 = new Button();
button1.Name = "c";
button1.Text = "c";
button1.Width = 100;
button1.Height = 30;
button1.Location = new System.Drawing.Point(50, 150);
this.button1.Click += new System.EventHandler(this.btnClick);
button2 = new Button();
button2.Name = "o";
button2.Text = "o";
button2.Width = 100;
button2.Height = 30;
button2.Location = new System.Drawing.Point(50, 50);
this.button2.Click += new System.EventHandler(this.btnClick);
button3 = new Button();
button3.Name = "U";
button3.Text = "U";
button3.Width = 100;
button3.Height = 30;
button3.Location = new System.Drawing.Point((ClientRectangle.Width - button1.Size.Width) / 50, ClientRectangle.Height - 150);
this.button3.Click += new System.EventHandler(this.btnClick);
button4 = new Button();
button4.Name = "D";
button4.Text = "D";
button4.Width = 100;
button4.Height = 30;
button2.Location = new System.Drawing.Point(250, 250);
this.button4.Click += new System.EventHandler(this.btnClick);
Controls.AddRange(new System.Windows.Forms.Control[] { this.button1 });
Controls.AddRange(new System.Windows.Forms.Control[] { this.button2 });
Controls.AddRange(new System.Windows.Forms.Control[] { this.button3 });
Controls.AddRange(new System.Windows.Forms.Control[] { this.button4 });
}
public static void Main()
{
Application.Run(new MyForm());
Console.ReadLine();
}
}