Данный класс не позволит пользователю писать в окно консоли какой-либо текст или цифры.
Создаём класс Locker.cs
И просто вызываете в удобном для Вас месте:
Создаём класс Locker.cs
C#:
using System;
/* Author r3xq1 */
public class Locker
{
public static string ConsoleInput(bool status = true)
{
while (true)
{
ConsoleKeyInfo a = Console.ReadKey(status);
string str = string.Empty;
if (int.TryParse(a.KeyChar.ToString(), out int i) != char.IsDigit(a.KeyChar))
{
Console.Write(a.KeyChar);
str += a.KeyChar;
}
if (a.Key == ConsoleKey.Backspace)
{
int currentCursorPos = Console.CursorLeft;
Console.SetCursorPosition(currentCursorPos, Console.CursorTop);
if (str.Length > 0)
{
str = str.Substring(0, str.Length - 1);
}
Console.SetCursorPosition(currentCursorPos + str.Length, Console.CursorTop);
}
}
}
}
C#:
Locker.ConsoleInput();