Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Input of certain character in VB-6 application  (Read 5236 times)

0 Members and 1 Guest are viewing this topic.

lohani

    Topic Starter


    Rookie

    Input of certain character in VB-6 application
    « on: July 08, 2010, 12:18:59 PM »
    Sir,
    I want to know that in VB-6 application
    1- How can we restrict a user to input certain character (Alpha) ie only A to 
        Z in a text box in upper case. For example, as we restrict a user to enter
        only numerics in a text box by passing a condition " if keyascii >=0 and
        keyascii <=9"
    2- How can we check that input is only alpha. For example to check that
        whether input is numeric or not, we pass a condition of IsNumeric(). May
        IsAplha() condition be used in VB-6 or not?. If no, how can we check that
        it is alpha or not other wise error message should appear .
    Thanks in Advance.

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: Input of certain character in VB-6 application
    « Reply #1 on: July 08, 2010, 01:44:19 PM »
    1- How can we restrict a user to input certain character (Alpha) ie only A to 
        Z in a text box in upper case. For example, as we restrict a user to enter
        only numerics in a text box by passing a condition " if keyascii >=0 and
        keyascii <=9"
    by changing keyascii to 0 in a control's "KeyPress" event, you "null" the input as if it never happened. Using the IsAlpha function I provide lower down:
    Code: [Select]
    Private Sub Text1_KeyPress(KeyAscii as Integer)

    KeyAscii = KeyAscii * Abs(IsAlpha(Chr$(KeyAscii)))

    End Sub


    IsAlpha will return either true or false- true is -1, the abs of which is 1 and that is multiplied by keyascii. Basically, if the return is false, keyascii is changed to 0, otherwise it remains the same.

    2- How can we check that input is only alpha. For example to check that
        whether input is numeric or not, we pass a condition of IsNumeric(). May
        IsAplha() condition be used in VB-6 or not?. If no, how can we check that
        it is alpha or not other wise error message should appear .
    Thanks in Advance.
    [/quote]
    There is no IsAlpha Function in VB. But you can easily write your own:
    Code: [Select]
    Public Function IsAlpha(ByVal InputString As String) As Boolean

        Dim uppercaseasc As Integer, I As Long
        For I = 1 To Len(InputString)
            uppercaseasc = Asc(UCase$((Mid$(InputString, I, 1))))
            If Not uppercaseasc >= 65 And uppercaseasc <= 90 Then
                IsAlpha = False
                Exit Function
            End If
           
       
       
        Next
        IsAlpha = True
    End Function

    The above function <Only>  returns true for alphanumeric characters- even space doesn't get through. make adjustments as necessary.
    I was trying to dereference Null Pointers before it was cool.

    Fleexy



      Intermediate

    • OW NEXT TIME I SHOULD TURN IT OFF BEFORE SERVICING
    • Thanked: 2
      • Yes
      • Yes
    • Experience: Experienced
    • OS: Windows XP
    Re: Input of certain character in VB-6 application
    « Reply #2 on: July 15, 2010, 12:25:07 PM »
    This is my favorite way to see if a character is one of a set:

    Code: [Select]
    Function IsAlpha(chr As String)
    If InStr(1, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", chr) <> 0 Then IsAlpha = True
    End Function

    All characters that should pass go in the second argument's literal string.

    InStr is useful :D
    I love .NET!

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: Input of certain character in VB-6 application
    « Reply #3 on: July 15, 2010, 07:05:41 PM »
    This is my favorite way to see if a character is one of a set:

    Code: [Select]
    Function IsAlpha(chr As String)
    If InStr(1, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", chr) <> 0 Then IsAlpha = True
    End Function
    First: completely trivial and meaningless nitpick: if you are going to specify the start argument as 1, just omit it entirely (1 is the default. (the OP requested VB6 so I assume that you are using it (the InStr Function should not be used in VB.NET), and if not, SHAME ON YOU for using the legacy functions in VB.NET!  :P).) Also, why not make the alphabetic string a constant? maybe even settle on the lower case or uppercase sequence only, and use LCase or UCase to change the case of the input before comparison?
     
    IMO an IsAlpha() should mirror the functionality of the built in IsNumeric() Function- when you say IsNumeric("32") you get true, with your IsAlpha, IsAlpha("CB") returns False. Mine attempts to work with strings of any length, in a similar fashion to IsNumeric. Although to truly emulate it's function it should probably Trim() before the loop.


    My version of a single-character IsAlpha function would be this:

    Code: [Select]
    Public Function IsAlpha(char as String) as Boolean
        Dim ucaseasc as Integer
        ucaseasc=Asc(Ucase$(char))
        IsAlpha = (ucaseasc >=65 and ucaseasc <= 90)
    End Function



    And, because I have discovered that C# is superior in nearly every way to my old love VB6, this is the C# version:

    Code: [Select]
    private static bool IsAlpha(String alphastring)
    {
        return alphastring.ToUpper().All((w) => (w >= 65 && w <= 90));
    }
    Yep, a single line.
    lambda expressions are awesome. The great thing is that in C#, and many other object oriented languages, a String is really just an array of Char objects, in this case, I am using the free extension methods provided on any IEnumerable<> Implementation. In this case, the "All()" extension method is acting on the IEnumerable<Char> that represents the string itself, then the lambda expression is executed for each character, where w is the iterating item. the All() function returns true if they all return true, and false otherwise. in this case, it's an extremely useful shorthand for what I would otherwise implement as a foreach, something like this:

    Code: [Select]
    private static bool IsAlpha(String alphastring)
    {
        foreach(char w in alphastring.ToUpper())
        {
             if (!(w >= 65 && w <= 90)) return false;
        }
        return true;
    }


    Which is a version of the original VB6 solution I gave, but that doesn't suck. (I clearly rushed through my original VB6 implementation, as I should be calling UCase$() only a single time before the loop, rather then for every single character. Oh well.
    I was trying to dereference Null Pointers before it was cool.