• 0

C# - Limit Numbers Entered?


Question

9 answers to this question

Recommended Posts

  • 0

Do you mean after the 16th character nothing happens if the user presses a character key? There's no simple way of doing that AFAIK. If you mean "a way to validate if the user input doesn't exceed 16 characters", then just take it as a string (string line = Console.ReadLine()) and string.Length will give you the number of characters it contains.

  • 0

I don't believe you can do it with console input, because when you ask for input via a console the program pauses until it gets input. You could always check the persons input and if it is more than 16 characters, read back that it is limited to 16 characters then ask for input again....but you can't actively check how many characters they typed in a console window until they submit the input to the program.

If you are making a GUI, you would simply make a text box limited to 16 characters.

EDIT: You could possibly do a GUI popup from a console application that limits the characters in the text box to 16 characters....but it wouldn't be a plain console app at that point.

  • 0

Consoles are much simpler than Winforms Windows in terms of what they're able to do. A console is just a dumb terminal window that performs input and output, whereas a Winforms window is a complex beast that uses many Windows APIs to achieve its goals, it's just looks to be easier because a lot of the complicated stuff gets done for you by Microsoft.

The simplest thing you can do is just accept whatever the user inputs (regardless of how many numbers are in it), and trim/validate the input when the user hits return.

  • 0
  On 14/05/2011 at 16:52, syzygy said:

I did a GUI version of this console app and used the MaxLength code. Pretty easy stuff. I find Windows Form programming a ton easier than console programming. As for the console app, what would the code to warn the user that the character limit is 16, look like?

I have not written C# code in quite awhile so I cannot give you example code, but in short the easiest way I would see it working is:

-begin loop

-Ask for input

-if input is valid (16 characters or less) exit loop

-otherwise output warning about character limit and repeat loop (which would start with asking for input again)

-end loop

The text validation would determine whether the code continues (and loops back to the beginning) or breaks out of the loop to continue with your code, based on whether or not the input text is valid.

Depending on what the person is inputting, it may be better to just trim the input to 16 characters and continue. But that all depends on how valuable the input is and whether or not anything important to the code can come after the 16th character.

  • 0

        static void Main(string[] args)
        {
            String input = getInput(16);
        }

        static String getInput(int length)
        {
            Char[] chars = new Char[length];
            ConsoleKeyInfo keyInfo;
            int count = 0;
            bool done = false;

            while (!done)
            {
                keyInfo = Console.ReadKey();

                switch (keyInfo.KeyChar)
                {
                    case '\b':
                        if (count > 0)
                        {
                            Console.Write(" \b");
                            count--;
                        }
                        break;
                    case '\r':
                        done = true;
                        break;
                    default:
                        Console.Write("\b;
                        if (count < chars.Length)
                        {
                            Console.Write(keyInfo.KeyChar);
                            chars[count] = keyInfo.KeyChar;
                            count++;
                        }
                        break;
                }
            }

            Console.WriteLine();
            return new String(chars);
        }

Its hackey, but it works

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.