Resolved Array receive only two numerical digits in each index

titojd

Well-known member
Joined
Oct 12, 2018
Messages
57
Programming Experience
1-3
I have problems in my textbox array, there are 15 indexes,
each index receives two digits, however if I type letters it is accepting.

I wanted it to accept only numerical digits, two numbers per index,

Anyone willing to give me some information on how I can proceed with this?
I am very grateful
my code
C#:
private TextBox[] ResultText = new TextBox[15];

Sem Título.png

C#:
 private void GeraControles()
        {
            int i, esquerda = 105, topo = 230;
            for (i = 0; i < 15; i++)
            {
                ResultText[i] = new TextBox();
                //ResultText[i].Name = "label" + i;
                //ResultText[i].Text = ResultText[i].Name;
                //ResultText[i].Anchor = AnchorStyles.Right;
                //ResultText[i].Location = new Point(ResultText[i].Location.X + 20, ResultText[i].Location.Y + 300);
                ResultText[i].Left = esquerda;
                ResultText[i].Top = topo;
                ResultText[i].Anchor = new AnchorStyles();
                ResultText[i].BackColor = Color.Gray;
                ResultText[i].ForeColor = Color.White;
                ResultText[i].Size = new Size(45, 30);
                ResultText[i].Font = new Font("", 15, FontStyle.Bold);
                ResultText[i].TabIndex = 2;
                ResultText[i].BorderStyle = BorderStyle.FixedSingle;
                this.Controls.Add(ResultText[i]);
                esquerda += (ResultText[i].Width + 5);
                ResultText[i].Tag = i;
            }
        }
 
Solution
If you can dynamically create textboxes and position them on screen, you can dynamically add key press event handlers to each of those textboxes.
Did you look at the various key press events for your text boxes?
 
If you can dynamically create textboxes and position them on screen, you can dynamically add key press event handlers to each of those textboxes.
 
Solution
I tried to put KeyPress in the textBox Control ( GeraControles() )

C#:
 ResultText[i].KeyPress += new KeyPressEventHandler(keypressed);
then I created the events
C#:
private bool nonNumberEntered = false;
        private void ResultText_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            // Initialize the flag to false.
            nonNumberEntered = false;

            // Determine whether the keystroke is a number from the top of the keyboard.
            if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
            {
                // Determine whether the keystroke is a number from the keypad.
                if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
                {
                    // Determine whether the keystroke is a backspace.
                    if (e.KeyCode != Keys.Back)
                    {
                        // A non-numerical keystroke was pressed.
                        // Set the flag to true and evaluate in KeyPress event.
                        nonNumberEntered = true;
                    }
                }
            }
            //If shift key was pressed, it's not a number.
            if (Control.ModifierKeys == Keys.Shift)
            {
                nonNumberEntered = true;
            }
        }
        private void keypressed(Object o, KeyPressEventArgs e)
        {
            // Check for the flag being set in the KeyDown event.
            if (nonNumberEntered == true)
            {
                // Stop the character from being entered into the control since it is non-numerical.
                e.Handled = true;
            }
        }
but I was successful with my code, it keeps entering text and numbers more than two.
 
I managed to solve my problem here,
I created an 'IntNumber' method in the class program
C#:
 static class Program
    {
        /// <summary>
        /// Ponto de entrada principal para o aplicativo.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmInicial());
        }
        public static void IntNumber(KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)  && e.KeyChar !=8)
            {
                e.Handled = true;
            }
        }
    }
in the form, I added it to the method that creates the textBox

C#:
ResultText[i].MaxLength = 2;
ResultText[i].KeyPress += new KeyPressEventHandler(keypressed);

right after i chemi my method

C#:
 private void keypressed(Object o, KeyPressEventArgs e)
        {
            Program.IntNumber(e);
        }
Thanks anyway @Skydrive opened my eyes
 

Latest posts

Back
Top Bottom