There only two glitches within this code that I can't seem to get 100% spot on. However, this will create a functioning menu (text based) on your form at runtime. I plan to develop an actual control exactly like it. It uses a custom class I developed called LocationBounceEffect that basically bounces an integer back and forth between two points (A start location and the provided offset on the provided axis). I'm using that class to bounce the selected menu item back and forth as I go through the menu items. For my implementation example I will be using a timer to update my bounce. I will fully comment the implementation source so that you understand what's going on and why.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace Menu
{
public partial class Form1 : Form
{
/// <summary>
/// This example shows how to use the menu classes I've created,
/// and as well as why each line is important.
///
/// There are some optional lines (such as the LBE).
///
/// Thank you for using my code snippets, and just put a thanks to
/// me into your disclaimer or credits.
///
/// Jamie K. Davis - Studio 41 Games (Owner/Head Developer)
/// </summary>
public Form1()
{
InitializeComponent();
}
// Declare a new menu object (setting the location of the menu at 15x 15y).
// You may optionally use the secondary constructor allowing you
// to change the selected item's forecolor from the default (white).
Menu myMenu = new Menu(new Point(15, 15));
// Declare a few menu entries to add to the menu;
// They are all using the second constructor of the MenuEntry class.
// The second constructor as you can see accepts a string for text,
// and a color for the forecolor.
MenuEntry Entry1 = new MenuEntry("Play", Color.Red);
MenuEntry Entry2 = new MenuEntry("Options", Color.Red);
MenuEntry Entry3 = new MenuEntry("Credits", Color.Red);
MenuEntry Entry4 = new MenuEntry("Exit", Color.Red);
private void Form1_Load(object sender, EventArgs e)
{
// Add all 4 menu entries to the menu.
myMenu.AddEntry(Entry1);
myMenu.AddEntry(Entry2);
myMenu.AddEntry(Entry3);
myMenu.AddEntry(Entry4);
// This is essential; using a loop go through and add
// each menu entry to the controls of the form
// (so they can be visible on the form).
for (int i = 0; i < myMenu.Items.Count; i++)
this.Controls.Add(myMenu.Items[i]);
// Start the timer to update the bounce of the currently selected
// Menu entry.
Updater.Start();
// Update the menu at runtime to allow selection color process to happen.
myMenu.Update();
}
// Timer_Tick Event
private void UpdateMenu(object sender, EventArgs e)
{
// Update the bounce of the selected menu item on timer tick.
myMenu.UpdateBounce();
}
// Form_KeyDown Event
private void NavigateMenu(object sender, KeyEventArgs e)
{
// Navigate through the menu using the keyboard.
myMenu.Navigate(e);
// Update the menu so the user can see the results of their
// navigation.
myMenu.Update();
}
}
}
Enjoy,
Jamie - Studio 41 Games (Owner/Head Developer)