Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Otros entornos y lenguajes > .NET
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

 
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 26-07-2024
REHome REHome is offline
Miembro
 
Registrado: jul 2003
Ubicación: España
Posts: 475
Poder: 22
REHome Va por buen camino
Reducir código

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);
        }
    }
}
__________________
http://electronica-pic.blogspot.com....n-arduino.html Manuales de electrónica general, PIC y Arduino.
Responder Con Cita
 



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Reducir lineas de codigo JuanOrtega Varios 2 23-02-2015 11:26:37
Reducir Tamaño DLL en XE3 elcharlie Varios 6 24-04-2013 18:38:05
Reducir Imagenes jerosol Gráficos 11 01-02-2011 16:06:17
Como reducir mi log Gustavo Gowdak MS SQL Server 2 04-06-2007 23:43:06
reducir tamaño del LOG orodriguezc MS SQL Server 3 09-03-2004 14:50:35


La franja horaria es GMT +2. Ahora son las 19:11:57.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi