Keypress event for textbox created at runtime.

I'm using the C# code below to create a button at runtime and run an event for it.
How can I create a textbox at run time with a keypress event to make sure it only accepts alphanumeric keys?

        private void cmdPrime_Click(object sender, EventArgs e)
        {
            Button b1 = new Button();
            b1.Text = "Click me";
            b1.Left = 50;
            b1.Top = 50;
            b1.Text = "Enter the number below which you want all primes";
            b1.Click += new EventHandler(FindThePrimes);//EventHandler is pre-defined delegate.  Right click and select goto definition
            //Without the delegate I could not trigger an event for button added at runtime
            this.Controls.Add(b1);
            TextBox t1 = new TextBox();
            t1+=new 
            
        }
private void FindThePrimes(object sender, EventArgs e)
        {
            Stopwatch s = new Stopwatch();
            s.Start();
            GetListOfPrimesFromBeginning a = new GetListOfPrimesFromBeginning();
            a.PrintList(800000);
            GetListOfPrimesFromEnd b = new GetListOfPrimesFromEnd();
            b.PrintList(1000000, 800001);
 s.Stop();
            Environment.Exit(0);
        }

Open in new window

AlHal2Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MishaProgrammerCommented:
Try this code:
        private void button1_Click(object sender, EventArgs e)
        {
            Button b1 = new Button();
            b1.Text = "Click me";
            b1.Left = 50;
            b1.Size = new Size(100,25);
            
            b1.Top = 50;
            b1.Text = "Enter the number below which you want all primes";
            b1.Click += new EventHandler(FindThePrimes);//EventHandler is pre-defined delegate.  Right click and select goto definition
            //Without the delegate I could not trigger an event for button added at runtime
            this.Controls.Add(b1);
            TextBox t1 = new TextBox();
            t1.Left = 150;
            t1.Top = 150;
            t1.Size = new Size(100, 25);
            t1.KeyPress += new KeyPressEventHandler(TextBox1_KeyPress);
            this.Controls.Add(t1);
            
        }

        private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsLetterOrDigit(e.KeyChar);
        }

Open in new window


If you wnat to allow user to input symbols '.' or ',' for double, you also can set up it in TextBox1_KeyPress method.
NorieAnalyst Assistant Commented:
You can use this to add the textbox and the KeyPress handler.
        TextBox t1 = new TextBox();
        this.Controls.Add(t1);
        t1.KeyPress += new KeyPressEventHandler(keypressed);

Open in new window

To prevent anything other than alphanumeric you can try this.
    private void keypressed(Object o, KeyPressEventArgs e)
    {
        if (((e.Key < Key.NumPad0)||(e.Key > Key.NumPad9))&&((e.Key < Key.A)||(e.Key > Key.Z)))
       {
           e.Handled = true;
       }

    }

Open in new window

AlHal2Author Commented:
In the FindThePrimes module I want to pass the value entered in the text box instead of 800000.  How do I do this if the textbox is created at runtime?
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

NorieAnalyst Assistant Commented:
Try naming the textbox when you create it and then use this.Controls(textboxname) to refer to it in FindThePrimes.
AlHal2Author Commented:
I called the textbox txtEnterPrime.  Then I typed this

int i = this.Controls("txtEnterPrime");

It's saying Non-invocable member 'Control.Controls' cannot be used like a method.
MishaProgrammerCommented:
Try this code to get control by name:
TextBox i = this.Controls["txtEnterPrime"] as TextBox;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AlHal2Author Commented:
Thanks both.
NorieAnalyst Assistant Commented:
Both?:)
AlHal2Author Commented:
Thanks to Norie and Misha.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.