I've recently posted a code snippet of a working 2D text menu for Windows Forms Applications. However, as I was going through and revising this snippet (trying to make it better and add functionality to it) I've created two new problems. The first problem is that my text is not showing up on the form anymore (kind of the biggest problem), and the second is that my value is scrolling too fast on the key press events. I have a seperate class managing the events so that I can clean up source code in the main form .cs file. So here is my source code, hopefully you guys can help me figure out what I changed that made my text disappear. :/
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
{
public Form1()
{
InitializeComponent();
}
Menu myMenu = new Menu(new Point(15, 15));
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)
{
myMenu.AddEntry(Entry1);
myMenu.AddEntry(Entry2);
myMenu.AddEntry(Entry3);
myMenu.AddEntry(Entry4);
// UPDATE: Changed myMenu.SelectedIndex to myMenu.Length
// Effect: Displayed the first label (not the others though).
for (int i = 0; i < myMenu.SelectedIndex; i++)
this.Controls.Add(myMenu.Items[i]);
// Timer
Updater.Start();
}
// Timer Tick
private void UpdateMenu(object sender, EventArgs e)
{
myMenu.Update();
}
// Form1_KeyDown
private void NavigateMenu(object sender, KeyEventArgs e)
{
myMenu.Navigate(e);
}
// Trying to complete navigation tests to get rid of INDEX OUT OF RANGE exception. This part was just part of that.
// Form1_KeyPress
private void Navigate(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Up)
this.Text += "UP";
else if (e.KeyChar == (char)Keys.Down)
this.Text += "DOWN";
e.Handled = true;
}
}
public class MenuEntry
{
public string Text = "";
public Point Location;
public Color Color;
public Font Font = new Font("OCR A Extended", 8.25f, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
public MenuEntry(string Text)
{
this.Text = Text;
Color = Color.White;
}
public MenuEntry(string Text, Color Color)
{
this.Text = Text;
this.Color = Color;
}
public MenuEntry(string Text, Font Font, Color Color)
{
this.Text = Text;
this.Font = Font;
this.Color = Color;
}
public MenuEntry(string Text, string FontName, float FontSize, Color Color)
{
this.Text = Text;
this.Font = new Font(FontName, FontSize, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
this.Color = Color;
}
}
public class Menu
{
private Input Input = new Input();
private List<MenuEntry> MenuItems;
public List<Label> Items { get; private set; }
public Point Location { get; private set; }
public int Length { get; private set; }
public int SelectedIndex { get; private set; }
public Color SelectionColor { get; private set; }
public Menu(Point Location)
{
MenuItems = new List<MenuEntry>();
this.Length = 0;
this.SelectedIndex = 0;
this.Location = Location;
this.SelectionColor = Color.White;
Items = new List<Label>();
}
public Menu(Point Location, Color SelectionColor)
{
MenuItems = new List<MenuEntry>();
this.Length = 0;
this.SelectedIndex = 0;
this.Location = Location;
this.SelectionColor = SelectionColor;
this.Items = new List<Label>();
}
public void AddEntry(MenuEntry Entry)
{
MenuItems.Add(Entry);
Items.Add(new Label());
if (this.SelectedIndex == 0)
MenuItems[Length].Location = this.Location;
else
MenuItems[Length].Location = new Point(this.Location.X, MenuItems[Length - 1].Location.Y + Items[Length].Size.Height + 5);
Items[Length].BackColor = Color.Transparent;
Items[Length].Text = Entry.Text;
Items[Length].Font = Entry.Font;
Items[Length].ForeColor = Entry.Color;
Items[Length].Location = MenuItems[Length].Location;
Length++;
}
public void Navigate(KeyEventArgs e)
{
if (Input.NavigatingUp(e))
{
if (SelectedIndex <= 0)
SelectedIndex = MenuItems.Count;
else
SelectedIndex--;
}
else if (Input.NavigatingDown(e))
{
if (SelectedIndex >= MenuItems.Count)
SelectedIndex = 0;
else
SelectedIndex++;
}
}
public void Update()
{
Items[SelectedIndex].ForeColor = this.SelectionColor;
}
}
public class Input
{
private bool Up = false;
private bool Down = false;
public bool NavigatingUp(KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
Up = true;
Thread.Sleep(10);
}
else
Up = false;
return Up;
}
public bool NavigatingDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.Down)
{
Down = true;
Thread.Sleep(10);
}
else
Down = false;
return Down;
}
}
}
Thanks,
Jamie