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 820073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:24:59+00:00 2026-05-15T02:24:59+00:00

I have this function which evaluates the contents of a textbox (or combobox or

  • 0

I have this function which evaluates the contents of a textbox (or combobox or maskedtextbox) control and, in conjunction with a KeyPress event, will either allow or disallow the input. It is quite useful for dates or textboxes for which only numeric input is valid. It allows a set number of digits after a single decimal point, if specified in the function call. It also allows the backspace character if the textbox is full.

I would like it to allow me to input what would otherwise be valid text when the textbox is full but one or more characters are highlighted (and would therefore be replaced by the keypress character. Can anyone show me how to do that, please?

''' <summary>
''' Validates that input is numeric.  
''' Allows one decimal place unless intDecimal is less than 1
''' Will allow a set number of numbers after the decimal place.
''' </summary>
''' <param name="strKeyPress">The key which has been pressed</param>
''' <param name="strText">Current text of the textbox</param>
''' <param name="intPosition">Current cursor position in textbox</param>
''' <param name="intDecimal">Number of decimal places desired.</param>
''' <returns>Boolean: true means that input is numeric, false means it is not.</returns>
''' <remarks>Used with a keypress event to validate input.  Do not handle input if function returns false.</remarks>
Public Function InputIsNumeric(ByVal strKeyPress As String, ByVal strText As String, ByVal intPosition As Integer, ByVal intDecimal As Integer) As Boolean
    Dim dot As Integer
    Dim ch As String
    Dim returnValue As Boolean

    If Not Char.IsDigit(CType(strKeyPress, Char)) Then returnValue = True
    If strKeyPress = "-" And intPosition = 0 Then returnValue = False 'allow negative number 
    If strKeyPress = "." And strText.IndexOf(".") = -1 And intDecimal > 0 Then returnValue = False 'allow single decimal point 
    dot = strText.IndexOf(".")
    If dot > -1 Then 'limit to set decimal places 
        ch = strText.Substring(dot + 1)
        If ch.Length > (intDecimal - 1) Then returnValue = True
    End If
    If strKeyPress = Chr(8) Then returnValue = False 'allow Backspace 
    Return returnValue
End Function
  • 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-15T02:24:59+00:00Added an answer on May 15, 2026 at 2:24 am

    You can add a couple of parameters (intLengthOfHighlightedText and intLengthOfControl) and pass Textbox.SelectedText and TextboxName.MaxLength (assuming single line text control). Then just work that out in your function. Otherwise you can pass the value of the whole textbox control (make it overloaded or do the work for the combobox, etc).

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

Sidebar

Related Questions

I have this function which is supposed to set a certain time format to
I have this function which pings my server for a specific change in data:
I have this function which gives me ( for a current date ) -
I have this function which returns a datatype InetAddress[] public InetAddress [] lookupAllHostAddr(String host)
Hi have made this function which is made to replicate an error that I
I have a function which calculates distance from database records. This function is called
We have this anonymous function in our code, which is part of the jQuery's
I have a function (function1) which requires sta mode. I'd like tocall this function
I have this istuation. I have a setTimeout to a function in which I
I have a function which looks something like this, it returns a noncopyable class

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.