Click here to Skip to main content
Click here to Skip to main content

Text-based menu class for Console Applications

By , 25 Jan 2003
Rate this:
Please Sign up or sign in to vote.

Introduction

Firstly, this is my very first article and it will probably contain mistakes and the language will not be perfect, but I will keep it updated and I will edit it until it gets good, so, I need your feedback.

Almost every console-based program written in C# will make use of Console.ReadLine() to read in data. Mostly, the programmer will use a if-else statement to handle the data input, specially when inputting choices from menus. This interesting class will teach you the basics of collections and delegates while providing a good framework for a more powerful menu class.

The Menu class and its interface

The menu class is very simple to use; just instantiate it and use the Add method to add new options to menu, and finally, use the Show method to print it and let it handle the user's choice.

Here is how it works: each menu option is a MenuItem class. It is nothing but an object containing the option's descriptions and a delegate, an object that points to a function, that will be called when and if the option is choosen. This delegate will point to a function that will handle the choice.

The MenuItems are stored in an ArrayList, a very useful collection class. A collection class is self-explainatory: it holds a collection of objects, often providing search, remove and object manipulation functionalities. The ArrayList is pretty simple; the name Array comes from that fact it can be transformed into an Array of any type, and I will use it because the Menu class does not need the power of a Hashtable collection or a Dictionary collection, more enhanced collection classes. ArrayList is just fine for us.

Creating and showing a menu

Firstly, write the functions that will handle the user's choice and the delegates. You will see how simple is that. Our Menu class uses the MenuCallback delegate, which returns nothing and has no parameters. Whenever you use a delegate, you need to

  • Create a new delegate type and
  • Use the new keyword to store a new delegate.
private static MenuCallback mcOption1 = new MenuCallback(Option1);
private static MenuCallback mcOption2 = new MenuCallback(Option2);

private static void Option1()
{
  Console.WriteLine("Option 1 choosen.");
}

private static void Option2()
{
  Console.WriteLine("Option 2 choosen.");
}

Secondly, create a new Menu instance and add the delegates and options to it. Then, show it. This is simple, too.

Menu m = new Menu();
m.Add("Option 1", mcOption1);
m.Add("Option 2", mcOption2);
m.Show();

Now, it is over. The Menu class will handle the user's input and call the desired function using the delegate you've created. The sample will show everything better.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Kiff

Brazil Brazil
No Biography provided

Comments and Discussions

 
GeneralDemo Pinmemberspencepk31-Mar-11 11:25 
Questionwhats the point? Pinmemberllitsanylg14-Aug-07 6:42 
AnswerRe: whats the point? Pinmemberspencepk31-Mar-11 11:02 
QuestionAnd where's the demo project??? PinmemberChopper26-Jan-03 23:13 
Confused | :confused:
 
Ultimate all-in-one XP-Style UI multiplatform solution: Tooltips, XP Menus, Hyperlinks, Drawing Graphics and formatted documents, plus powerful binary resource reuse. All Free 100%, visit www.tooltips.net now. The most powerful integrate UI library for developers in VC++, C#, Java, VB, Delphi, Borland C++, Borland C++ Builder, as well as any COM-compatible platform; Unified approach, cross-language OS, full .NET integration.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 26 Jan 2003
Article Copyright 2003 by Kiff
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid