September 20, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

ActiveX Control Tutorial - Part 3

  • November 20, 2002
  • By Karl Moore
  • Send Email »
  • More Articles »

So far, we've created the skeleton for our new and improved Text Box. We've added a couple of properties, defined enumerations and handled resizing.

Err, but it doesn't exactly work yet. In fact, I saw more work emitting from Queen Elizabeth's left eyebrow, the last time she ordered, "Take over the fort Charlie, I'm off for an all-expenses-paid weekend in Tunisia".

Hmm, think about it. Even if the AcceptType was set to 'Letters', the end user can still enter anything into the Text Box. But hey-ho, in this section, we're going to change all that.

  • Open last week's project
  • Enter the code window of our control
  • Select our Text Box from the Object drop-down list
  • Select the Keypress event from the Procedure drop-down list



Click here for larger image

The Keypress event fires whenever the user types something in the Text Box. An argument is passed with the event — KeyAscii.

Top Tips: KeyAscii is a number that represents a key on the keyboard. To convert KeyAscii to its keyboard letter, use Chr(KeyAscii). To convert a letter to its KeyAscii code, use Asc(Letter). Setting KeyAscii = 0 in the KeyPress event cancels the character.

  • Add the following code to the txtTextBox_KeyPress event:
Private Sub txtTextBox_KeyPress(KeyAscii As Integer)Select Case AcceptType

' Check out the AcceptType propertyCase Numbers' If only accepting numbers, check...If IsNumeric(Chr(KeyAscii)) = False Then' If the passed KeyAscii keyboard equivalent' is NOT numeric, then cancel the character!KeyAscii = 0BeepExit SubEnd If Case Letters' If only accepting letters, check...If Not (KeyAscii > 64 And KeyAscii < 91) And _Not (KeyAscii > 96 And KeyAscii < 123) Then' If the KeyAscii number isn't greater than' 64 and less than 91 (upper case characters)' and it isn't greater than 96 and less than' 123 (lower case characters), then cancel' the character!KeyAscii = 0BeepExit SubEnd IfEnd SelectSelect Case ConvertCase' Check out the ConvertCase propertyCase UpperCase' If text needs to be in upper case,' change the KeyAscii value to' the Ascii code of an upper case' version of the KeyAscii {phew!}' DON'T FORGET - Asc() changes a' letter to it's Ascii value, UCase()' changes a letter to upper case,' Chr() changes an Ascii character to' a normal letter. So here, we're converting' the Ascii to a normal letter, making it' upper case then changing it back again!KeyAscii = Asc(UCase(Chr(KeyAscii)))Case LowerCase' Errm... opposite of the above ;-)KeyAscii = Asc(LCase(Chr(KeyAscii)))End SelectEnd Sub

By observing the comments, you should be able to understand this code. Don't worry if it's seems slightly alien, after all, it was written by me, Karl Moore, distant son of Phwasack "Area 51" Carstolemew and third supreme commander of the planet Clot.

Just bare in mind that you don't need to know about 'KeyAscii codes' in order to create most ActiveX controls. Except this one.

In our control, it's the above chunk of code that does all the work - checks the properties, ensures characters are upper case or lower case, and beeps if a character is invalid.

Try testing our code. Add a new test 'Standard EXE' project and throw our control onto a Form.

Don't set the properties at design time — set them in code behind a Command Button. Then run your application, press the Command Button and attempt to type a few invalid characters.

Does the control work?

<Karl sniggers as he sinfully realises this is a one-way medium>

Note: If you set the control properties at design time, they will disappear when you run the application. That's because we haven't told Visual Basic to save that information. You do this via Property Bags. Remember that — it'll make sense later.

And Another Note: Can you spot the bug? What happens when you choose the 'Letters' option and tap the spacebar? What about the backspace? Can you fix this?





Page 2 of 6



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel