Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6548951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:02:01+00:00 2026-05-25T12:02:01+00:00

I working on a project that includes to call a certain type of method

  • 0

I working on a project that includes to call a certain type of method to each control, i have this code:

 Private Sub txtBcNum1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtBcNum1.KeyPress

    If Char.IsDigit(e.KeyChar) Or e.KeyChar = Chr(8) Then
        e.Handled = False
    Else
        e.Handled = True
    End If

End Sub

This code works like a charm if i want to allow only numbers and backspace on my textbox.

Problem:
I have 15 textboxes( txtBcNum1,txtBcNum2,….,txtBcNum15 ), what’s the best way to call this function inside KeyPress method on each textboxes with out manually adding it to KeyPress method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:02:02+00:00Added an answer on May 25, 2026 at 12:02 pm

    You can add more than one control to the Handles clause:

    Private Sub txtBcNum1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
                Handles txtBcNum1.KeyPress, txtBcNum2.KeyPress, txtBcNum3.Keypress, ...
    

    This way your sub will be called for every control. If you want to reuse this code across different forms, it might be a good idea to create a new control, inherited from Textbox, which might be called NumericTextBox and handle its own KeyPress event. That way you wouldn’t have to add any code in forms that use this control.

    Also, have you considered using the built-in NumericUpDown control which serves the same purpose?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working a project that does not have a trunk / branches /
We have a project that includes all of our re-usable classes, third-party library JARs
I have a VS 2005/MSBuild 2.0 project (let's call it Project A) that I
I'm working on a project that includes a javascript widget that can be wrapped
I am working on an Android application (I will call this ProjectA) that is
I have a bit old project that I would call legacy. Some characteristics of
Suppose I have this simple app working : # model class Project has_many :numbers
I'm working on a project that includes a page of ASP.Net web parts. It
I am working with a Visual Studio 2005 C++ solution that includes multiple projects
I'm working on project that lets users choose some scientific authors and columnists and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.