InterviewSolution
| 1. |
Solve : Input of certain character in VB-6 application? |
|
Answer» Sir, 1- How can we restrict a user to input certain character (Alpha) ie only A toby 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 returns true for alphanumeric characters- even space doesn't get through. make adjustments as necessary.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 Quote from: Fleexy on July 15, 2010, 12:25:07 PM This is my favorite way to see if a character is one of a set: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! ).) 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 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. |
|