M
Meta
Alfa
Usuário com poucas negociações! Use sempre o saldo do ForoBeta!
Usando C# em modo console, é possível selecionar alguma opção com as setas do teclado usando isto >. Apenas deve estar marcado com um x.
A ideia é, ao executar o programa, se não encontrar o arquivo.txt, que ele o crie e escreva a opção marcada com o x.
Ao executar o programa novamente, ele me indica a opção marcada que leu no arquivo.txt.
Código que fiz até agora, mas não lê, embora crie o arquivo, é este:
using System;
using System.IO;
namespace Pruebas_Opciones_Consola_02
{
internal class Program
{
static void Main(string[] args)
{
#region Configuração da janela.
// Título da janela.
Console.Title = "Opções";
// Tamanho da janela, x, y ou, largura e altura.
const byte ancho = 20, alto = 5;
Console.SetWindowSize(ancho, alto);
// Remover scroll laterais. Direito e abaixo.
Console.SetBufferSize(ancho, alto);
// Cor de fundo.
Console.BackgroundColor = ConsoleColor.Yellow;
// Cor das letras.
Console.ForegroundColor = ConsoleColor.Black;
// Limpar tela e deixar tudo na cor de fundo.
Console.Clear();
// Visível o cursor. // Não! Assim você o torna invisível.
Console.CursorVisible = false;
#endregion
string[] opciones =
{
" ** TÍTULO ** ", // Posição 0.
" ( ) Opção A ", // 1
" ( ) Opção B ", // 2
" ( ) Opção C ", // 3
" SAIR ", // 4
">", // 5
"x", // 6
" ", // 7
" " // 8
};
string opA = "x";
string opB = " ";
string opC = " ";
Menu();
#region Opções, seleções e salvar arquivo .txt
void OpcionesA()
{
Console.SetCursorPosition(0, 0);
Console.Write(opciones[0]); // ** TÍTULO **
Console.SetCursorPosition(0, 1);
Console.Write(opciones[1]); // ( ) Opção A
Console.SetCursorPosition(0, 2);
Console.Write(opciones[2]); // ( ) Opção B
Console.SetCursorPosition(0, 3);
Console.Write(opciones[3]); // ( ) Opção C
}
void OpcionesB()
{
Console.SetCursorPosition(0, 1);
Console.Write(opciones[4]); // SAIR
Console.SetCursorPosition(0, 2);
Console.Write(opciones[8]); //
Console.SetCursorPosition(0, 3);
Console.Write(opciones[8]); //
}
void SelectorA()
{
Console.SetCursorPosition(0, 1);
Console.Write(opciones[5]); // >
Console.SetCursorPosition(0, 2);
Console.Write(opciones[7]);
Console.SetCursorPosition(0, 3);
Console.Write(opciones[7]);
}
void SelectorB()
{
Console.SetCursorPosition(0, 1);
Console.Write(opciones[7]);
Console.SetCursorPosition(0, 2);
Console.Write(opciones[5]); // >
Console.SetCursorPosition(0, 3);
Console.Write(opciones[7]);
}
void SelectorC()
{
Console.SetCursorPosition(0, 1);
Console.Write(opciones[7]);
Console.SetCursorPosition(0, 2);
Console.Write(opciones[7]);
Console.SetCursorPosition(0, 3);
Console.Write(opciones[5]); // >
}
#endregion
void Menu()
{
sbyte indiceSeleccionado = 0; // Índice selecionado de cada opção do menu.
bool salir = false; // Para sair do menu principal ao INÍCIO.
sbyte SELECCION_OPCIONES_TOTALES = 3; // Total de opções para selecionar e fixa.
// Capturar tecla para depois validar.
ConsoleKey tecla;
// Criar arquivo e salvar o * selecionado nele.
if (File.Exists("Archivo.txt"))
{
string data = File.ReadAllText("Archivo.txt");
opA = Convert.ToString(data);
opB = Convert.ToString(data);
opC = Convert.ToString(data);
}
do
{
//******************************************************************
// Desenho o menu principal.
switch (indiceSeleccionado)
{
case 0:
OpcionesA();
SelectorA();
Console.SetCursorPosition(3, 1);
Console.Write(opA);
Console.SetCursorPosition(3, 2);
Console.Write(opB);
Console.SetCursorPosition(3, 3);
Console.Write(opC);
break;
case 1:
OpcionesA();
SelectorB();
Console.SetCursorPosition(3, 1);
Console.Write(opA);
Console.SetCursorPosition(3, 2);
Console.Write(opB);
Console.SetCursorPosition(3, 3);
Console.Write(opC);
break;
case 2:
OpcionesA();
SelectorC();
Console.SetCursorPosition(3, 1);
Console.Write(opA);
Console.SetCursorPosition(3, 2);
Console.Write(opB);
Console.SetCursorPosition(3, 3);
Console.Write(opC);
break;
case 3:
OpcionesB();
Console.SetCursorPosition(0, 1);
Console.Write(opciones[5]); // >
break;
default:
Console.Write("Fora de alcance. ");
break;
}
// Fim de pintar o menu principal.
//******************************************************************
// Ler tecla inserida pelo usuário.
tecla = Console.ReadKey(true).Key;
// Validar o tipo de tecla.
if (tecla == ConsoleKey.Enter)
{
switch (indiceSeleccionado)
{
case 0:
opA = "x"; opB = " "; opC = " ";
// Se pressionar Enter, escreve no arquivo.
if (tecla == ConsoleKey.Enter)
{
File.WriteAllText("Archivo.txt", opA.ToString());
}
break;
case 1:
opA = " "; opB = "x"; opC = " ";
// Se pressionar Enter, escreve no arquivo.
if (tecla == ConsoleKey.Enter)
{
File.WriteAllText("Archivo.txt", opB.ToString());
}
break;
case 2:
opA = " "; opB = " "; opC = "x";
// Se pressionar Enter, escreve no arquivo.
if (tecla == ConsoleKey.Enter)
{
File.WriteAllText("Archivo.txt", opC.ToString());
}
break;
case 3:
indiceSeleccionado = 0; // Restaurar índice antes de sair.
salir = true; // Sair.
break;
default:
Console.Write("Fora de alcance. ");
break;
}
}
// Você pressionou a tecla seta para Baixo?
if (tecla == ConsoleKey.DownArrow)
{
indiceSeleccionado++;
}
// Então se pressionar a tecla seta para Cima.
if (tecla == ConsoleKey.UpArrow)
{
indiceSeleccionado--;
}
// Se está na última opção, pula para a primeira.
if (indiceSeleccionado > SELECCION_OPCIONES_TOTALES)
{
indiceSeleccionado = 0;
}
// Se está na primeira posição, pula para a última.
if (indiceSeleccionado < 0)
{
indiceSeleccionado = SELECCION_OPCIONES_TOTALES;
}
// Uso a tecla escape como saída.
} while (!salir);
}
}
}
}