Click here to Skip to main content
15,894,630 members
Articles / Programming Languages / C#

A Simple State Machine

Rate me:
Please Sign up or sign in to vote.
4.67/5 (28 votes)
23 Jan 2014CPOL3 min read 122.1K   4.6K   117  
Create loose coupled States using a Finite State Automation (FSM) model.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Reflection; 

namespace AbstractFSM
{
    public partial class frmTest : Form
    {
        public frmTest()
        {
            InitializeComponent();
        }

        private StatesManager m_player = null;
        private void frmTest_Load(object sender, EventArgs e)
        {
            //fill the playlist
            lstSongs.Items.Clear();
            for (int i = 0; i <= 19; i++)
            {
                lstSongs.Items.Add(i.ToString("000") + " - Song" + i.ToString());     
            }
            //
            m_player = new MediaPlayerStateManager(this); 
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmTest());
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            m_player.ChangeState(sender, new StateEventArgs((int)StateEvents.Stop));
            EnablePlayerControl();
        }

        private void btnPlay_Click(object sender, EventArgs e)
        {
            m_player.ChangeState(sender, new StateEventArgs((int)StateEvents.Play));
            EnablePlayerControl();
        }

        private void btnPause_Click(object sender, EventArgs e)
        {
            m_player.ChangeState(sender, new StateEventArgs((int)StateEvents.Pause));
            EnablePlayerControl();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            m_player.ChangeState(sender, new StateEventArgs((int)StateEvents.Previos));
            EnablePlayerControl();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            m_player.ChangeState(sender, new StateEventArgs((int)StateEvents.Next));
            EnablePlayerControl();
        }

        private void lstSongs_SelectedIndexChanged(object sender, EventArgs e)
        {
            grpPlayPanel.Enabled = (lstSongs.SelectedIndex != -1); 
        }

        private void btnClearStatesList_Click(object sender, EventArgs e)
        {
            lstStates.Items.Clear();   
        }
        //
        private void EnablePlayerControl()
        {
            btnStop.Enabled = m_player.CheckState(this, new StateEventArgs((int)StateEvents.Stop));
            btnPlay.Enabled = m_player.CheckState(this, new StateEventArgs((int)StateEvents.Play));
            btnPause.Enabled = m_player.CheckState(this, new StateEventArgs((int)StateEvents.Stop));
            btnPrevious.Enabled = m_player.CheckState(this, new StateEventArgs((int)StateEvents.Previos));
            btnNext.Enabled = m_player.CheckState(this, new StateEventArgs((int)StateEvents.Next));
        }
    }

    public enum StateEvents
    {
        Stop, Play, Pause, Previos, Next
    }
    
    //////////////////////////////////////////////////////////////////////////////


}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Canada Canada
Has been engaging in software development for over 15 years. Has deliberately chosen this path and has no compunction about it at all. Has a nasty habit of sharing his 'out-of-office' code hoping to get some positive feedback. Smile | :)

Comments and Discussions