Hola:
Este código me ayudaron hace unos años pero no me gusta mucho, usa dos Switch -case, uno para el dibujado y el otro acciones cuando pulsa Enter. Los menús se eligen con las flechas.
¿Es posible mejorarlo que no quede tan feo, malo, mucho códigos de más, etc?
Código:
// El menú principal que muestra nada más ejecutar el programa.
// ** MENÚ PRINCIPAL **
// > ESTADO PRINCIPAL
// NOMBRES ENTRADAS
// NOMBRES SALIDAS
// -------------------
// ** MENÚ PRINCIPAL **
// ENTRADA ANALÓGICA
// CONFIGURACIÓN
// ACERCA DE...
// -------------------
// ** MENÚ PRINCIPAL **
// AYUDA
// EXTRA
// INICIO
using System;
namespace LCD_Menu_con_submenus_Consola_03
{
public class MenuPrincipal
{
public static void Menu_Principal()
{
// Contador de teclas y navegador.
sbyte indiceSeleccionado = 0; // Índice seleccionado de cada opción del menú.
bool salir = false; // Para salir del menú principal al INICIO.
const sbyte SELECCION_OPCIONES_TOTALES = 8; // Total de opciones para seleccionar y fija.
// Capturar tecla para luego validar.
ConsoleKey tecla;
// Limpiar pantalla.
Console.Clear();
do
{
//******************************************************************
// Dibujo el menú principal.
// Cursor invisible.
Console.CursorVisible = false;
string[] OPCIONES =
{
"** MENÚ PRINCIPAL **", // Posición 0.
" ESTADO PRINCIPAL ", // 1
" NOMBRE ENTRADAS ", // 2
" NOMBRE SALIDAS ", // 3
" ENTRADA ANALÓGICA ", // 4
" CONFIGURACIÓN ", // 5
" ACERCA DE... ", // 6
" AYUDA ", // 7
" EXTRA ", // 8
" INICIO ", // 9
" ", // 10
">" // 11
};
switch (indiceSeleccionado)
{
case 0:
Console.SetCursorPosition(0, 0);
Console.Write(OPCIONES[0]); // ** MENÚ PRINCIPAL **
Console.SetCursorPosition(0, 1);
Console.Write(OPCIONES[1]); // > ESTADO PRINCIPAL
Console.SetCursorPosition(0, 1);
Console.Write(OPCIONES[11]); // >
Console.SetCursorPosition(0, 2);
Console.Write(OPCIONES[2]); // NOMBRE ENTRADAS
Console.SetCursorPosition(0, 3);
Console.Write(OPCIONES[3]); // NOMBRE SALIDAS
break;
case 1:
Console.SetCursorPosition(0, 0);
Console.Write(OPCIONES[0]); // ** MENÚ PRINCIPAL **
Console.SetCursorPosition(0, 1);
Console.Write(OPCIONES[1]); // ESTADO PRINCIPAL
Console.SetCursorPosition(0, 2);
Console.Write(OPCIONES[2]); // > NOMBRE ENTRADAS
Console.SetCursorPosition(0, 2);
Console.Write(OPCIONES[11]); // >
Console.SetCursorPosition(0, 3);
Console.Write(OPCIONES[3]); // NOMBRE SALIDAS
break;
case 2:
Console.SetCursorPosition(0, 0);
Console.Write(OPCIONES[0]); // ** MENÚ PRINCIPAL **
Console.SetCursorPosition(0, 1);
Console.Write(OPCIONES[1]); // ESTADO PRINCIPAL
Console.SetCursorPosition(0, 2);
Console.Write(OPCIONES[2]); // NOMBRE ENTRADAS
Console.SetCursorPosition(0, 3);
Console.Write(OPCIONES[3]); // > NOMBRE SALIDAS
Console.SetCursorPosition(0, 3);
Console.Write(OPCIONES[11]); // >
break;
case 3:
Console.SetCursorPosition(0, 0);
Console.Write(OPCIONES[0]); // ** MENÚ PRINCIPAL **
Console.SetCursorPosition(0, 1);
Console.Write(OPCIONES[4]); // > ENTRADA ANALÓGICA
Console.SetCursorPosition(0, 1);
Console.Write(OPCIONES[11]); // >
Console.SetCursorPosition(0, 2);
Console.Write(OPCIONES[5]); // CONFIGURACIÓN
Console.SetCursorPosition(0, 3);
Console.Write(OPCIONES[6]); // ACERCA DE...
break;
case 4:
Console.SetCursorPosition(0, 0);
Console.Write(OPCIONES[0]); // ** MENÚ PRINCIPAL **
Console.SetCursorPosition(0, 1);
Console.Write(OPCIONES[4]); // ENTRADA ANALÓGICA
Console.SetCursorPosition(0, 2);
Console.Write(OPCIONES[5]); // > CONFIGURACIÓN
Console.SetCursorPosition(0, 2);
Console.Write(OPCIONES[11]); // >
Console.SetCursorPosition(0, 3);
Console.Write(OPCIONES[6]); // ACERCA DE...
break;
case 5:
Console.SetCursorPosition(0, 0);
Console.Write(OPCIONES[0]); // ** MENÚ PRINCIPAL **
Console.SetCursorPosition(0, 1);
Console.Write(OPCIONES[4]); // ENTRADA ANALÓGICA
Console.SetCursorPosition(0, 2);
Console.Write(OPCIONES[5]); // CONFIGURACIÓN
Console.SetCursorPosition(0, 3);
Console.Write(OPCIONES[6]); // > ACERCA DE...
Console.SetCursorPosition(0, 3);
Console.Write(OPCIONES[11]); // >
break;
case 6:
Console.SetCursorPosition(0, 0);
Console.Write(OPCIONES[0]); // ** MENÚ PRINCIPAL **
Console.SetCursorPosition(0, 1);
Console.Write(OPCIONES[7]); // > AYUDA
Console.SetCursorPosition(0, 1);
Console.Write(OPCIONES[11]); // >
Console.SetCursorPosition(0, 2);
Console.Write(OPCIONES[8]); // EXTRA
Console.SetCursorPosition(0, 3);
Console.Write(OPCIONES[9]); // INICIO
break;
case 7:
Console.SetCursorPosition(0, 0);
Console.Write(OPCIONES[0]); // ** MENÚ PRINCIPAL **
Console.SetCursorPosition(0, 1);
Console.Write(OPCIONES[7]); // AYUDA
Console.SetCursorPosition(0, 2);
Console.Write(OPCIONES[8]); // > EXTRA
Console.SetCursorPosition(0, 2);
Console.Write(OPCIONES[11]); // >
Console.SetCursorPosition(0, 3);
Console.Write(OPCIONES[9]); // INICIO
break;
case 8:
Console.SetCursorPosition(0, 0);
Console.Write(OPCIONES[0]); // ** MENÚ PRINCIPAL **
Console.SetCursorPosition(0, 1);
Console.Write(OPCIONES[7]); // AYUDA
Console.SetCursorPosition(0, 2);
Console.Write(OPCIONES[8]); // EXTRA
Console.SetCursorPosition(0, 3);
Console.Write(OPCIONES[9]); // > INICIO
Console.SetCursorPosition(0, 3);
Console.Write(OPCIONES[11]); // >
break;
default:
Console.Write("Fuera de rango. ");
break;
}
// Fin de pintar el menú principal.
//******************************************************************
// Leer tecla ingresada por el usuario.
tecla = Console.ReadKey(true).Key;
// Validar el tipo de tecla.
if (tecla == ConsoleKey.Enter)
{
switch (indiceSeleccionado)
{
case 0:
EstadoPrincipal.Estado_Principal();
break;
case 1:
NombreEntradas.NombreEntradas_Principal();
break;
case 2:
NombresSalidas.NombreSalidas_Principal();
break;
case 3:
// Opcion();
break;
case 4:
//
break;
case 5:
AcercaDe.Acerca_De();
break;
case 6:
//
break;
case 7:
//
break;
case 8:
salir = true;
//opcion = 0; // Vuelve al menú principal.
break;
default:
Console.Write("Fuera de rango. ");
break;
}
}
// ¿Has pulsado tecla flecha Abajo?
else if (tecla == ConsoleKey.DownArrow)
{
indiceSeleccionado++;
}
// Entonces si pulsas tecla flecha Arriba.
else if (tecla == ConsoleKey.UpArrow)
{
indiceSeleccionado--;
}
// Si está en la última opción, salta a la primera.
if (indiceSeleccionado > SELECCION_OPCIONES_TOTALES)
{
indiceSeleccionado = 0;
}
// Si está en la primera posición, salta a la última.
if (indiceSeleccionado < 0)
{
indiceSeleccionado = SELECCION_OPCIONES_TOTALES;
}
// Uso la tecla escape como salida.
} while (salir == false);
}
}
}