![]() |
![]() |
![]() |
![]() |
![]() |
FTP | ![]() |
![]() |
CCD | ![]() |
![]() |
Buscar | ![]() |
![]() |
Trucos | ![]() |
![]() |
Trabajo | ![]() |
![]() |
Foros | ![]() |
#1
|
|||
|
|||
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. |
#2
|
||||
|
||||
Si funciona, no lo toques
![]()
__________________
La otra guía de estilo | Búsquedas avanzadas | Etiquetas para código | Colabora mediante Paypal |
#3
|
||||
|
||||
Una mejora simple es convertir las `opciones` y los `indices` en enums, crearles un método para imprimir el texto y hacer solo switch con enums. No quedara muy reducido pero es mas claro.
__________________
El malabarista. |
#4
|
|||
|
|||
Hay casos que no siempre es así. Eso de tener dos Switch como que no. Se puede reducir.
En cuanto al enum. ¿Qué más da si enum y un array, el rollo es conseguir esa información.
__________________
http://electronica-pic.blogspot.com....n-arduino.html Manuales de electrónica general, PIC y Arduino. |
#5
|
|||
|
|||
Dentro de un tiempo lo hago con otro ejemplo desde cero para que se entienda mejor.
__________________
http://electronica-pic.blogspot.com....n-arduino.html Manuales de electrónica general, PIC y Arduino. |
#6
|
||||
|
||||
Cita:
En términos generales, es mejor modelar con `tipos` que con `algoritmos`. Por ejemplo, si quieres definir que algo esta ordenado, puedes hacer un algoritmo de `sort` O (mejor) usas una estructura como un `Btree`, que esta ordenada por construcción. Igual, es lo mismo decir que tienes 2 posiciones en pantalla con `[1,2]` que con `Point{x:1,y:2}`, pero la segunda es mas clara. El uso de enums aquí hace mas correcto el código, ya que crea una lista finita y explicita de opciones, en cambio, un integer es 'valido' para todo el rango del 'i32'.
__________________
El malabarista. |
#7
|
|||
|
|||
Lo podrías hacer de muchas formas, pero revisa la siguientes bibliotecas, tal vez te sean de utilidad.
https://github.com/gui-cs/Terminal.Gui https://github.com/MV10/dotnet-curses https://spectreconsole.net/ https://github.com/lechu445/ConsoleMenu Así de buenas me iría primero por Terminal.Gui Y es relativamente sencillo iniciar con esta biblioteca. Código:
dotnet new console dotnet add package Terminal.Gui Código:
dotnet tool install --global TerminalGuiDesigner https://imgur.com/a/2Ojlk53 Al principio te podría parecer un poco complejo, pero podrías obtener un resultado más profesional Además podrías seguir el siguiente tutorial para aprender a usarlo. https://markjames.dev/blog/developin...player-csharp/ |
![]() |
|
|
![]() |
||||
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 |
![]() |
|